|
25 | 25 | class NewrelicServiceProvider extends ServiceProvider |
26 | 26 | { |
27 | 27 |
|
28 | | - /** |
29 | | - * Indicates if loading of the provider is deferred. |
30 | | - * |
31 | | - * @var bool |
32 | | - */ |
33 | | - protected $defer = false; |
34 | | - |
35 | | - /** |
36 | | - * Bootstrap the application events. |
37 | | - * |
38 | | - * @return void |
39 | | - */ |
40 | | - public function boot() |
41 | | - { |
42 | | - $config = realpath( __DIR__ . '/../../config/config.php' ); |
43 | | - $this->mergeConfigFrom( $config, 'newrelic' ); |
44 | | - $this->publishes( [ $config => config_path( 'newrelic.php' ) ], 'config' ); |
45 | | - |
46 | | - $this->registerNamedTransactions(); |
47 | | - $this->registerQueueTransactions(); |
48 | | - } |
49 | | - |
50 | | - /** |
51 | | - * Register the service provider. |
52 | | - * |
53 | | - * @return void |
54 | | - */ |
55 | | - public function register() |
56 | | - { |
57 | | - $this->app->singleton( |
58 | | - 'newrelic', |
59 | | - function ( $app ) { |
60 | | - return new Newrelic( $app['config']->get( 'newrelic.throw_if_not_installed' ) ); |
61 | | - } |
62 | | - ); |
63 | | - } |
64 | | - |
65 | | - /** |
66 | | - * Get the services provided by the provider. |
67 | | - * |
68 | | - * @return array |
69 | | - */ |
70 | | - public function provides() |
71 | | - { |
72 | | - return [ 'newrelic' ]; |
73 | | - } |
74 | | - |
75 | | - /** |
76 | | - * Registers the named transactions with the NewRelic PHP agent |
77 | | - */ |
78 | | - protected function registerNamedTransactions() |
79 | | - { |
80 | | - $app = $this->app; |
81 | | - |
82 | | - if ($app['config']->get( 'newrelic.auto_name_transactions' )) { |
83 | | - $app['events']->listen(RouteMatched::class, function (RouteMatched $routeMatched) use ( $app ) { |
84 | | - $app['newrelic']->nameTransaction( $this->getTransactionName() ); |
85 | | - }); |
86 | | - } |
87 | | - } |
88 | | - |
89 | | - /** |
90 | | - * Registers the queue transactions with the NewRelic PHP agent |
91 | | - */ |
92 | | - protected function registerQueueTransactions() |
93 | | - { |
94 | | - $app = $this->app; |
95 | | - |
96 | | - $app['queue']->before(function (JobProcessing $event) use ( $app ) { |
97 | | - $app['newrelic']->backgroundJob( true ); |
98 | | - $app['newrelic']->startTransaction( ini_get('newrelic.appname') ); |
99 | | - if ($app['config']->get( 'newrelic.auto_name_jobs' )) { |
100 | | - $app['newrelic']->nameTransaction( $this->getJobName($event) ); |
101 | | - } |
102 | | - }); |
103 | | - |
104 | | - $app['queue']->after(function (JobProcessed $event) use ( $app ) { |
105 | | - $app['newrelic']->endTransaction(); |
106 | | - }); |
107 | | - } |
108 | | - |
109 | | - /** |
110 | | - * Build the transaction name |
111 | | - * |
112 | | - * @return string |
113 | | - */ |
114 | | - public function getTransactionName() |
115 | | - { |
116 | | - return str_replace( |
117 | | - [ |
118 | | - '{controller}', |
119 | | - '{method}', |
120 | | - '{route}', |
121 | | - '{path}', |
122 | | - '{uri}', |
123 | | - ], |
124 | | - [ |
125 | | - $this->getController(), |
126 | | - $this->getMethod(), |
127 | | - $this->getRoute(), |
128 | | - $this->getPath(), |
129 | | - $this->getUri(), |
130 | | - ], |
131 | | - $this->app['config']->get( 'newrelic.name_provider' ) |
132 | | - ); |
133 | | - } |
134 | | - |
135 | | - /** |
136 | | - * Build the job name |
137 | | - * |
138 | | - * @param JobProcessing $event |
139 | | - * @return string |
140 | | - */ |
141 | | - public function getJobName(JobProcessing $event) |
142 | | - { |
143 | | - return str_replace( |
144 | | - [ |
145 | | - '{connection}', |
146 | | - '{class}', |
147 | | - ], |
148 | | - [ |
149 | | - $event->connectionName, |
150 | | - $event->job->resolveName(), |
151 | | - ], |
152 | | - $this->app['config']->get( 'newrelic.job_name_provider' ) |
153 | | - ); |
154 | | - } |
155 | | - |
156 | | - /** |
157 | | - * Get the request method |
158 | | - * |
159 | | - * @return string |
160 | | - */ |
161 | | - protected function getMethod() |
162 | | - { |
163 | | - return strtoupper( $this->app['router']->getCurrentRequest()->method() ); |
164 | | - } |
165 | | - |
166 | | - /** |
167 | | - * Get the request URI path |
168 | | - * |
169 | | - * @return string |
170 | | - */ |
171 | | - protected function getPath() |
172 | | - { |
173 | | - return ($this->app['router']->current()->uri() == '' ? '/' : $this->app['router']->current()->uri()); |
174 | | - } |
175 | | - |
176 | | - protected function getUri() |
177 | | - { |
178 | | - return $this->app['router']->getCurrentRequest()->path(); |
179 | | - } |
180 | | - |
181 | | - /** |
182 | | - * Get the current controller / action |
183 | | - * |
184 | | - * @return string |
185 | | - */ |
186 | | - protected function getController() |
187 | | - { |
188 | | - $controller = $this->app['router']->current() ? $this->app['router']->current()->getActionName() : 'unknown'; |
189 | | - if ($controller === 'Closure') { |
190 | | - $controller .= '@' . $this->getPath(); |
191 | | - } |
192 | | - |
193 | | - return $controller; |
194 | | - } |
195 | | - |
196 | | - /** |
197 | | - * Get the current route name, or controller if not named |
198 | | - * |
199 | | - * @return string |
200 | | - */ |
201 | | - protected function getRoute() |
202 | | - { |
203 | | - $name = $this->app['router']->currentRouteName(); |
204 | | - if ( !$name ) |
205 | | - { |
206 | | - $name = $this->getController(); |
207 | | - } |
208 | | - |
209 | | - return $name; |
210 | | - } |
| 28 | + /** |
| 29 | + * Indicates if loading of the provider is deferred. |
| 30 | + * |
| 31 | + * @var bool |
| 32 | + */ |
| 33 | + protected $defer = false; |
| 34 | + |
| 35 | + /** |
| 36 | + * Bootstrap the application events. |
| 37 | + * |
| 38 | + * @return void |
| 39 | + */ |
| 40 | + public function boot() |
| 41 | + { |
| 42 | + $config = realpath( __DIR__ . '/../../config/config.php' ); |
| 43 | + $this->mergeConfigFrom( $config, 'newrelic' ); |
| 44 | + $this->publishes( [ $config => config_path( 'newrelic.php' ) ], 'config' ); |
| 45 | + |
| 46 | + $this->registerNamedTransactions(); |
| 47 | + $this->registerQueueTransactions(); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Register the service provider. |
| 52 | + * |
| 53 | + * @return void |
| 54 | + */ |
| 55 | + public function register() |
| 56 | + { |
| 57 | + $this->app->singleton( |
| 58 | + 'newrelic', |
| 59 | + function ( $app ) { |
| 60 | + return new Newrelic( $app['config']->get( 'newrelic.throw_if_not_installed' ) ); |
| 61 | + } |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Get the services provided by the provider. |
| 67 | + * |
| 68 | + * @return array |
| 69 | + */ |
| 70 | + public function provides() |
| 71 | + { |
| 72 | + return [ 'newrelic' ]; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Registers the named transactions with the NewRelic PHP agent |
| 77 | + */ |
| 78 | + protected function registerNamedTransactions() |
| 79 | + { |
| 80 | + $app = $this->app; |
| 81 | + |
| 82 | + if ($app['config']->get( 'newrelic.auto_name_transactions' )) { |
| 83 | + $app['events']->listen(RouteMatched::class, function (RouteMatched $routeMatched) use ( $app ) { |
| 84 | + $app['newrelic']->nameTransaction( $this->getTransactionName() ); |
| 85 | + }); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Registers the queue transactions with the NewRelic PHP agent |
| 91 | + */ |
| 92 | + protected function registerQueueTransactions() |
| 93 | + { |
| 94 | + $app = $this->app; |
| 95 | + |
| 96 | + $app['queue']->before(function (JobProcessing $event) use ( $app ) { |
| 97 | + $app['newrelic']->backgroundJob( true ); |
| 98 | + $app['newrelic']->startTransaction( ini_get('newrelic.appname') ); |
| 99 | + if ($app['config']->get( 'newrelic.auto_name_jobs' )) { |
| 100 | + $app['newrelic']->nameTransaction( $this->getJobName($event) ); |
| 101 | + } |
| 102 | + }); |
| 103 | + |
| 104 | + $app['queue']->after(function (JobProcessed $event) use ( $app ) { |
| 105 | + $app['newrelic']->endTransaction(); |
| 106 | + }); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Build the transaction name |
| 111 | + * |
| 112 | + * @return string |
| 113 | + */ |
| 114 | + public function getTransactionName() |
| 115 | + { |
| 116 | + return str_replace( |
| 117 | + [ |
| 118 | + '{controller}', |
| 119 | + '{method}', |
| 120 | + '{route}', |
| 121 | + '{path}', |
| 122 | + '{uri}', |
| 123 | + ], |
| 124 | + [ |
| 125 | + $this->getController(), |
| 126 | + $this->getMethod(), |
| 127 | + $this->getRoute(), |
| 128 | + $this->getPath(), |
| 129 | + $this->getUri(), |
| 130 | + ], |
| 131 | + $this->app['config']->get( 'newrelic.name_provider' ) |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Build the job name |
| 137 | + * |
| 138 | + * @param JobProcessing $event |
| 139 | + * @return string |
| 140 | + */ |
| 141 | + public function getJobName(JobProcessing $event) |
| 142 | + { |
| 143 | + return str_replace( |
| 144 | + [ |
| 145 | + '{connection}', |
| 146 | + '{class}', |
| 147 | + ], |
| 148 | + [ |
| 149 | + $event->connectionName, |
| 150 | + $event->job->resolveName(), |
| 151 | + ], |
| 152 | + $this->app['config']->get( 'newrelic.job_name_provider' ) |
| 153 | + ); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Get the request method |
| 158 | + * |
| 159 | + * @return string |
| 160 | + */ |
| 161 | + protected function getMethod() |
| 162 | + { |
| 163 | + return strtoupper( $this->app['router']->getCurrentRequest()->method() ); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Get the request URI path |
| 168 | + * |
| 169 | + * @return string |
| 170 | + */ |
| 171 | + protected function getPath() |
| 172 | + { |
| 173 | + return ($this->app['router']->current()->uri() == '' ? '/' : $this->app['router']->current()->uri()); |
| 174 | + } |
| 175 | + |
| 176 | + protected function getUri() |
| 177 | + { |
| 178 | + return $this->app['router']->getCurrentRequest()->path(); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Get the current controller / action |
| 183 | + * |
| 184 | + * @return string |
| 185 | + */ |
| 186 | + protected function getController() |
| 187 | + { |
| 188 | + $controller = $this->app['router']->current() ? $this->app['router']->current()->getActionName() : 'unknown'; |
| 189 | + if ($controller === 'Closure') { |
| 190 | + $controller .= '@' . $this->getPath(); |
| 191 | + } |
| 192 | + |
| 193 | + return $controller; |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Get the current route name, or controller if not named |
| 198 | + * |
| 199 | + * @return string |
| 200 | + */ |
| 201 | + protected function getRoute() |
| 202 | + { |
| 203 | + $name = $this->app['router']->currentRouteName(); |
| 204 | + if ( !$name ) |
| 205 | + { |
| 206 | + $name = $this->getController(); |
| 207 | + } |
| 208 | + |
| 209 | + return $name; |
| 210 | + } |
211 | 211 | } |
0 commit comments