From b3ea0a529f7c0c32e51179574bba5738a6b33197 Mon Sep 17 00:00:00 2001 From: Aliaksandr lptn Date: Sun, 3 Feb 2019 21:57:23 +0300 Subject: [PATCH 1/4] Fix queue transaction listeners to work with Laravel 5.7 --- README.md | 1 + .../NewrelicServiceProvider.php | 372 +++++++++--------- src/config/config.php | 3 - 3 files changed, 185 insertions(+), 191 deletions(-) diff --git a/README.md b/README.md index 00003a8..3c5c055 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ | Laravel Version | Package Tag | Supported | |-----------------|-------------|-----------| +| 5.7.x | ? | yes | | 5.4.x | 2.2.x | yes | | 5.2.x | 2.1.x | yes | | 5.1.x | 2.0.x | yes | diff --git a/src/Intouch/LaravelNewrelic/NewrelicServiceProvider.php b/src/Intouch/LaravelNewrelic/NewrelicServiceProvider.php index a0e3641..793236f 100644 --- a/src/Intouch/LaravelNewrelic/NewrelicServiceProvider.php +++ b/src/Intouch/LaravelNewrelic/NewrelicServiceProvider.php @@ -17,6 +17,7 @@ namespace Intouch\LaravelNewrelic; use Illuminate\Queue\Events\JobProcessed; +use Illuminate\Queue\Events\JobProcessing; use Illuminate\Routing\Events\RouteMatched; use Illuminate\Support\ServiceProvider; use Intouch\Newrelic\Newrelic; @@ -24,192 +25,187 @@ class NewrelicServiceProvider extends ServiceProvider { - /** - * Indicates if loading of the provider is deferred. - * - * @var bool - */ - protected $defer = false; - - /** - * Bootstrap the application events. - * - * @return void - */ - public function boot() - { - $config = realpath( __DIR__ . '/../../config/config.php' ); - $this->mergeConfigFrom( $config, 'newrelic' ); - $this->publishes( [ $config => config_path( 'newrelic.php' ) ], 'config' ); - - $this->registerNamedTransactions(); - $this->registerQueueTransactions(); - } - - /** - * Register the service provider. - * - * @return void - */ - public function register() - { - $this->app->singleton( - 'newrelic', - function ( $app ) { - return new Newrelic( $app['config']->get( 'newrelic.throw_if_not_installed' ) ); - } - ); - } - - /** - * Get the services provided by the provider. - * - * @return array - */ - public function provides() - { - return [ 'newrelic' ]; - } - - /** - * Registers the named transactions with the NewRelic PHP agent - */ - protected function registerNamedTransactions() - { - $app = $this->app; - - if ($app['config']->get( 'newrelic.auto_name_transactions' )) { - $app['events']->listen(RouteMatched::class, function (RouteMatched $routeMatched) use ( $app ) { - $app['newrelic']->nameTransaction( $this->getTransactionName() ); - }); - } - } - - /** - * Registers the queue transactions with the NewRelic PHP agent - */ - protected function registerQueueTransactions() - { - $app = $this->app; - - $app['queue']->before(function (JobProcessed $event) use ( $app ) { - $app['newrelic']->backgroundJob( true ); - $app['newrelic']->startTransaction( ini_get('newrelic.appname') ); - if ($app['config']->get( 'newrelic.auto_name_jobs' )) { - $app['newrelic']->nameTransaction( $this->getJobName($event) ); - } - }); - - $app['queue']->after(function (JobProcessed $event) use ( $app ) { - $app['newrelic']->endTransaction(); - }); - } - - /** - * Build the transaction name - * - * @return string - */ - public function getTransactionName() - { - return str_replace( - [ - '{controller}', - '{method}', - '{route}', - '{path}', - '{uri}', - ], - [ - $this->getController(), - $this->getMethod(), - $this->getRoute(), - $this->getPath(), - $this->getUri(), - ], - $this->app['config']->get( 'newrelic.name_provider' ) - ); - } - - /** - * Build the job name - * - * @return string - */ - public function getJobName(JobProcessed $event) - { - return str_replace( - [ - '{connection}', - '{class}', - '{data}', - '{args}', - '{input}', - ], - [ - $event->connectionName, - get_class($event->job), - json_encode($event->data), - implode(', ', array_keys($event->data)), - implode(', ', array_values($event->data)), - ], - $this->app['config']->get( 'newrelic.job_name_provider' ) - ); - } - - /** - * Get the request method - * - * @return string - */ - protected function getMethod() - { - return strtoupper( $this->app['router']->getCurrentRequest()->method() ); - } - - /** - * Get the request URI path - * - * @return string - */ - protected function getPath() - { - return ($this->app['router']->current()->uri() == '' ? '/' : $this->app['router']->current()->uri()); - } - - protected function getUri() - { - return $this->app['router']->getCurrentRequest()->path(); - } - - /** - * Get the current controller / action - * - * @return string - */ - protected function getController() - { - $controller = $this->app['router']->current() ? $this->app['router']->current()->getActionName() : 'unknown'; - if ($controller === 'Closure') { - $controller .= '@' . $this->getPath(); - } - - return $controller; - } - - /** - * Get the current route name, or controller if not named - * - * @return string - */ - protected function getRoute() - { - $name = $this->app['router']->currentRouteName(); - if ( !$name ) - { - $name = $this->getController(); - } - - return $name; - } + /** + * Indicates if loading of the provider is deferred. + * + * @var bool + */ + protected $defer = false; + + /** + * Bootstrap the application events. + * + * @return void + */ + public function boot() + { + $config = realpath( __DIR__ . '/../../config/config.php' ); + $this->mergeConfigFrom( $config, 'newrelic' ); + $this->publishes( [ $config => config_path( 'newrelic.php' ) ], 'config' ); + + $this->registerNamedTransactions(); + $this->registerQueueTransactions(); + } + + /** + * Register the service provider. + * + * @return void + */ + public function register() + { + $this->app->singleton( + 'newrelic', + function ( $app ) { + return new Newrelic( $app['config']->get( 'newrelic.throw_if_not_installed' ) ); + } + ); + } + + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() + { + return [ 'newrelic' ]; + } + + /** + * Registers the named transactions with the NewRelic PHP agent + */ + protected function registerNamedTransactions() + { + $app = $this->app; + + if ($app['config']->get( 'newrelic.auto_name_transactions' )) { + $app['events']->listen(RouteMatched::class, function (RouteMatched $routeMatched) use ( $app ) { + $app['newrelic']->nameTransaction( $this->getTransactionName() ); + }); + } + } + + /** + * Registers the queue transactions with the NewRelic PHP agent + */ + protected function registerQueueTransactions() + { + $app = $this->app; + + $app['queue']->before(function (JobProcessing $event) use ( $app ) { + $app['newrelic']->backgroundJob( true ); + $app['newrelic']->startTransaction( ini_get('newrelic.appname') ); + if ($app['config']->get( 'newrelic.auto_name_jobs' )) { + $app['newrelic']->nameTransaction( $this->getJobName($event) ); + } + }); + + $app['queue']->after(function (JobProcessed $event) use ( $app ) { + $app['newrelic']->endTransaction(); + }); + } + + /** + * Build the transaction name + * + * @return string + */ + public function getTransactionName() + { + return str_replace( + [ + '{controller}', + '{method}', + '{route}', + '{path}', + '{uri}', + ], + [ + $this->getController(), + $this->getMethod(), + $this->getRoute(), + $this->getPath(), + $this->getUri(), + ], + $this->app['config']->get( 'newrelic.name_provider' ) + ); + } + + /** + * Build the job name + * + * @param JobProcessing $event + * @return string + */ + public function getJobName(JobProcessing $event) + { + return str_replace( + [ + '{connection}', + '{class}', + ], + [ + $event->connectionName, + $event->job->resolveName(), + ], + $this->app['config']->get( 'newrelic.job_name_provider' ) + ); + } + + /** + * Get the request method + * + * @return string + */ + protected function getMethod() + { + return strtoupper( $this->app['router']->getCurrentRequest()->method() ); + } + + /** + * Get the request URI path + * + * @return string + */ + protected function getPath() + { + return ($this->app['router']->current()->uri() == '' ? '/' : $this->app['router']->current()->uri()); + } + + protected function getUri() + { + return $this->app['router']->getCurrentRequest()->path(); + } + + /** + * Get the current controller / action + * + * @return string + */ + protected function getController() + { + $controller = $this->app['router']->current() ? $this->app['router']->current()->getActionName() : 'unknown'; + if ($controller === 'Closure') { + $controller .= '@' . $this->getPath(); + } + + return $controller; + } + + /** + * Get the current route name, or controller if not named + * + * @return string + */ + protected function getRoute() + { + $name = $this->app['router']->currentRouteName(); + if ( !$name ) + { + $name = $this->getController(); + } + + return $name; + } } diff --git a/src/config/config.php b/src/config/config.php index d146327..fb7a348 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -48,9 +48,6 @@ * a pattern you define yourself, available tokens: * {connection} = The name of the queue connection * {class} = The name of the job class - * {data} = JSON string with all data passed to the job - * {args} = List of variable names passed to the job - * {input} = List of values passed to the job * anything that is not a matched token will remain a string literal * example: * Given a job named App\MyJob, with data {"subject":"hello","to":"world"}, From b7bb5f603e4051ab72a7f4b005bc254b7210f5ce Mon Sep 17 00:00:00 2001 From: Aliaksandr lptn Date: Sun, 3 Feb 2019 22:00:10 +0300 Subject: [PATCH 2/4] Fix queue transaction listeners to work with Lumen 5.7 --- .../LumenNewrelicServiceProvider.php | 33 ++++++++----------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/src/Intouch/LaravelNewrelic/LumenNewrelicServiceProvider.php b/src/Intouch/LaravelNewrelic/LumenNewrelicServiceProvider.php index 70923d7..65d730c 100644 --- a/src/Intouch/LaravelNewrelic/LumenNewrelicServiceProvider.php +++ b/src/Intouch/LaravelNewrelic/LumenNewrelicServiceProvider.php @@ -3,6 +3,7 @@ namespace Intouch\LaravelNewrelic; use Illuminate\Queue\Events\JobProcessed; +use Illuminate\Queue\Events\JobProcessing; use Illuminate\Support\ServiceProvider; use Intouch\Newrelic\Newrelic; @@ -23,7 +24,7 @@ function ( $app ) { } ); - app('queue')->before(function (JobProcessed $event) { + app('queue')->before(function (JobProcessing $event) { app('newrelic')->backgroundJob( true ); app('newrelic')->startTransaction( ini_get('newrelic.appname') ); if (app('config')->get( 'newrelic.auto_name_jobs' )) { @@ -41,24 +42,18 @@ function ( $app ) { * * @return string */ - public function getJobName(JobProcessed $event) + public function getJobName(JobProcessing $event) { - return str_replace( - [ - '{connection}', - '{class}', - '{data}', - '{args}', - '{input}', - ], - [ - $event->connectionName, - get_class($event->job), - json_encode($event->data), - implode(', ', array_keys($event->data)), - implode(', ', array_values($event->data)), - ], - $this->app['config']->get( 'newrelic.job_name_provider' ) - ); + return str_replace( + [ + '{connection}', + '{class}', + ], + [ + $event->connectionName, + $event->job->resolveName(), + ], + $this->app['config']->get( 'newrelic.job_name_provider' ) + ); } } From 46db8b83370b1aa889a53ba27811b86a27df5eb0 Mon Sep 17 00:00:00 2001 From: Aliaksandr lptn Date: Mon, 4 Feb 2019 12:41:07 +0300 Subject: [PATCH 3/4] Use tabs as separator to reduces changes and simplify PR review --- .../NewrelicServiceProvider.php | 366 +++++++++--------- 1 file changed, 183 insertions(+), 183 deletions(-) diff --git a/src/Intouch/LaravelNewrelic/NewrelicServiceProvider.php b/src/Intouch/LaravelNewrelic/NewrelicServiceProvider.php index 793236f..1ff448a 100644 --- a/src/Intouch/LaravelNewrelic/NewrelicServiceProvider.php +++ b/src/Intouch/LaravelNewrelic/NewrelicServiceProvider.php @@ -25,187 +25,187 @@ class NewrelicServiceProvider extends ServiceProvider { - /** - * Indicates if loading of the provider is deferred. - * - * @var bool - */ - protected $defer = false; - - /** - * Bootstrap the application events. - * - * @return void - */ - public function boot() - { - $config = realpath( __DIR__ . '/../../config/config.php' ); - $this->mergeConfigFrom( $config, 'newrelic' ); - $this->publishes( [ $config => config_path( 'newrelic.php' ) ], 'config' ); - - $this->registerNamedTransactions(); - $this->registerQueueTransactions(); - } - - /** - * Register the service provider. - * - * @return void - */ - public function register() - { - $this->app->singleton( - 'newrelic', - function ( $app ) { - return new Newrelic( $app['config']->get( 'newrelic.throw_if_not_installed' ) ); - } - ); - } - - /** - * Get the services provided by the provider. - * - * @return array - */ - public function provides() - { - return [ 'newrelic' ]; - } - - /** - * Registers the named transactions with the NewRelic PHP agent - */ - protected function registerNamedTransactions() - { - $app = $this->app; - - if ($app['config']->get( 'newrelic.auto_name_transactions' )) { - $app['events']->listen(RouteMatched::class, function (RouteMatched $routeMatched) use ( $app ) { - $app['newrelic']->nameTransaction( $this->getTransactionName() ); - }); - } - } - - /** - * Registers the queue transactions with the NewRelic PHP agent - */ - protected function registerQueueTransactions() - { - $app = $this->app; - - $app['queue']->before(function (JobProcessing $event) use ( $app ) { - $app['newrelic']->backgroundJob( true ); - $app['newrelic']->startTransaction( ini_get('newrelic.appname') ); - if ($app['config']->get( 'newrelic.auto_name_jobs' )) { - $app['newrelic']->nameTransaction( $this->getJobName($event) ); - } - }); - - $app['queue']->after(function (JobProcessed $event) use ( $app ) { - $app['newrelic']->endTransaction(); - }); - } - - /** - * Build the transaction name - * - * @return string - */ - public function getTransactionName() - { - return str_replace( - [ - '{controller}', - '{method}', - '{route}', - '{path}', - '{uri}', - ], - [ - $this->getController(), - $this->getMethod(), - $this->getRoute(), - $this->getPath(), - $this->getUri(), - ], - $this->app['config']->get( 'newrelic.name_provider' ) - ); - } - - /** - * Build the job name - * - * @param JobProcessing $event - * @return string - */ - public function getJobName(JobProcessing $event) - { - return str_replace( - [ - '{connection}', - '{class}', - ], - [ - $event->connectionName, - $event->job->resolveName(), - ], - $this->app['config']->get( 'newrelic.job_name_provider' ) - ); - } - - /** - * Get the request method - * - * @return string - */ - protected function getMethod() - { - return strtoupper( $this->app['router']->getCurrentRequest()->method() ); - } - - /** - * Get the request URI path - * - * @return string - */ - protected function getPath() - { - return ($this->app['router']->current()->uri() == '' ? '/' : $this->app['router']->current()->uri()); - } - - protected function getUri() - { - return $this->app['router']->getCurrentRequest()->path(); - } - - /** - * Get the current controller / action - * - * @return string - */ - protected function getController() - { - $controller = $this->app['router']->current() ? $this->app['router']->current()->getActionName() : 'unknown'; - if ($controller === 'Closure') { - $controller .= '@' . $this->getPath(); - } - - return $controller; - } - - /** - * Get the current route name, or controller if not named - * - * @return string - */ - protected function getRoute() - { - $name = $this->app['router']->currentRouteName(); - if ( !$name ) - { - $name = $this->getController(); - } - - return $name; - } + /** + * Indicates if loading of the provider is deferred. + * + * @var bool + */ + protected $defer = false; + + /** + * Bootstrap the application events. + * + * @return void + */ + public function boot() + { + $config = realpath( __DIR__ . '/../../config/config.php' ); + $this->mergeConfigFrom( $config, 'newrelic' ); + $this->publishes( [ $config => config_path( 'newrelic.php' ) ], 'config' ); + + $this->registerNamedTransactions(); + $this->registerQueueTransactions(); + } + + /** + * Register the service provider. + * + * @return void + */ + public function register() + { + $this->app->singleton( + 'newrelic', + function ( $app ) { + return new Newrelic( $app['config']->get( 'newrelic.throw_if_not_installed' ) ); + } + ); + } + + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() + { + return [ 'newrelic' ]; + } + + /** + * Registers the named transactions with the NewRelic PHP agent + */ + protected function registerNamedTransactions() + { + $app = $this->app; + + if ($app['config']->get( 'newrelic.auto_name_transactions' )) { + $app['events']->listen(RouteMatched::class, function (RouteMatched $routeMatched) use ( $app ) { + $app['newrelic']->nameTransaction( $this->getTransactionName() ); + }); + } + } + + /** + * Registers the queue transactions with the NewRelic PHP agent + */ + protected function registerQueueTransactions() + { + $app = $this->app; + + $app['queue']->before(function (JobProcessing $event) use ( $app ) { + $app['newrelic']->backgroundJob( true ); + $app['newrelic']->startTransaction( ini_get('newrelic.appname') ); + if ($app['config']->get( 'newrelic.auto_name_jobs' )) { + $app['newrelic']->nameTransaction( $this->getJobName($event) ); + } + }); + + $app['queue']->after(function (JobProcessed $event) use ( $app ) { + $app['newrelic']->endTransaction(); + }); + } + + /** + * Build the transaction name + * + * @return string + */ + public function getTransactionName() + { + return str_replace( + [ + '{controller}', + '{method}', + '{route}', + '{path}', + '{uri}', + ], + [ + $this->getController(), + $this->getMethod(), + $this->getRoute(), + $this->getPath(), + $this->getUri(), + ], + $this->app['config']->get( 'newrelic.name_provider' ) + ); + } + + /** + * Build the job name + * + * @param JobProcessing $event + * @return string + */ + public function getJobName(JobProcessing $event) + { + return str_replace( + [ + '{connection}', + '{class}', + ], + [ + $event->connectionName, + $event->job->resolveName(), + ], + $this->app['config']->get( 'newrelic.job_name_provider' ) + ); + } + + /** + * Get the request method + * + * @return string + */ + protected function getMethod() + { + return strtoupper( $this->app['router']->getCurrentRequest()->method() ); + } + + /** + * Get the request URI path + * + * @return string + */ + protected function getPath() + { + return ($this->app['router']->current()->uri() == '' ? '/' : $this->app['router']->current()->uri()); + } + + protected function getUri() + { + return $this->app['router']->getCurrentRequest()->path(); + } + + /** + * Get the current controller / action + * + * @return string + */ + protected function getController() + { + $controller = $this->app['router']->current() ? $this->app['router']->current()->getActionName() : 'unknown'; + if ($controller === 'Closure') { + $controller .= '@' . $this->getPath(); + } + + return $controller; + } + + /** + * Get the current route name, or controller if not named + * + * @return string + */ + protected function getRoute() + { + $name = $this->app['router']->currentRouteName(); + if ( !$name ) + { + $name = $this->getController(); + } + + return $name; + } } From 40d63de1b24395f3d58c2dc4bc7932070fb1b2fe Mon Sep 17 00:00:00 2001 From: Aliaksandr lptn Date: Mon, 4 Feb 2019 13:19:15 +0300 Subject: [PATCH 4/4] Clarify supported Laravel versions --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 3c5c055..1e952b5 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,7 @@ | Laravel Version | Package Tag | Supported | |-----------------|-------------|-----------| -| 5.7.x | ? | yes | -| 5.4.x | 2.2.x | yes | +| 5.x.x | 2.2.x | yes | | 5.2.x | 2.1.x | yes | | 5.1.x | 2.0.x | yes | | 5.0.x | 2.0.x | no |