|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the PHP New Relic package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Alex Soban <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace SobanVuex\NewRelic; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* OOP Wrapper for the New Relic PHP Agent. |
|
16
|
|
|
*/ |
|
17
|
|
|
class Agent |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Extension status. |
|
21
|
|
|
* |
|
22
|
|
|
* @var bool |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $loaded; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Instantiate the agent wrapper. |
|
28
|
|
|
* |
|
29
|
|
|
* Passing an application name will call the agent api right away. |
|
30
|
|
|
* |
|
31
|
|
|
* @param string|array $name (optional) Name(s) of app metrics should be reported under in New Relic |
|
|
|
|
|
|
32
|
|
|
* user interface |
|
33
|
|
|
* @param string $license (optional) Specify a different license key to report metrics to a different |
|
34
|
|
|
* New Relic account |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct($appname = null, string $license = null) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->loaded = extension_loaded('newrelic'); |
|
39
|
|
|
|
|
40
|
|
|
if (isset($appname, $license)) { |
|
41
|
|
|
$this->setAppname($appname, $license); |
|
42
|
|
|
} elseif (isset($appname)) { |
|
43
|
|
|
$this->setAppname($appname); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Check if the New Relic extension is loaded. |
|
49
|
|
|
* |
|
50
|
|
|
* @return bool |
|
51
|
|
|
*/ |
|
52
|
|
|
public function isLoaded(): bool |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->loaded; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Attaches a custom attribute (key/value pair) to the current transaction. |
|
59
|
|
|
* |
|
60
|
|
|
* Agent version 4.4.5.35 or higher. |
|
61
|
|
|
* |
|
62
|
|
|
* @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_add_custom_parameter |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $key The name of the custom attribute. Only the first 255 characters are retained |
|
65
|
|
|
* @param mixed $value The value to associate with this custom attribute |
|
66
|
|
|
* |
|
67
|
|
|
* @return bool |
|
68
|
|
|
*/ |
|
69
|
|
|
public function addCustomParameter(string $key, $value): bool |
|
70
|
|
|
{ |
|
71
|
|
|
if (!$this->isLoaded()) { |
|
72
|
|
|
return false; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return newrelic_add_custom_parameter($key, $value); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Specify functions or methods for the agent to instrument with custom instrumentation. |
|
80
|
|
|
* |
|
81
|
|
|
* Compatible with all agent versions. |
|
82
|
|
|
* |
|
83
|
|
|
* @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_add_custom_tracer |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $callable The name can be formatted either as function_name for procedural functions, |
|
86
|
|
|
* or as "ClassName::method" for methods |
|
87
|
|
|
* |
|
88
|
|
|
* @return bool |
|
89
|
|
|
*/ |
|
90
|
|
|
public function addCustomTracer(string $callable): bool |
|
91
|
|
|
{ |
|
92
|
|
|
if (!$this->isLoaded()) { |
|
93
|
|
|
return false; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return newrelic_add_custom_tracer($callable); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Manually specify that a transaction is a background job or a web transaction. |
|
101
|
|
|
* |
|
102
|
|
|
* Compatible with all agent versions. |
|
103
|
|
|
* |
|
104
|
|
|
* @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_background_job |
|
105
|
|
|
* |
|
106
|
|
|
* @param bool $flag (optional) Defaults to true |
|
107
|
|
|
*/ |
|
108
|
|
|
public function backgroundJob(bool $flag = true) |
|
109
|
|
|
{ |
|
110
|
|
|
if ($this->isLoaded()) { |
|
111
|
|
|
newrelic_background_job($flag); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Enable or disable the capture of URL parameters. |
|
117
|
|
|
* |
|
118
|
|
|
* Compatible with all agent versions. |
|
119
|
|
|
* |
|
120
|
|
|
* @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_capture_params |
|
121
|
|
|
* |
|
122
|
|
|
* @param bool $enable (optional) Defaults to true |
|
123
|
|
|
*/ |
|
124
|
|
|
public function captureParams(bool $enable = true) |
|
125
|
|
|
{ |
|
126
|
|
|
if ($this->isLoaded()) { |
|
127
|
|
|
newrelic_capture_params($enable); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Add a custom metric (in milliseconds) to time a component of your app not captured by default. |
|
133
|
|
|
* |
|
134
|
|
|
* Compatible with all agent versions. |
|
135
|
|
|
* |
|
136
|
|
|
* @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newreliccustommetric-php-agent-api |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $metric Name your custom metrics with a Custom/ prefix (for example, Custom/MyMetric) |
|
139
|
|
|
* @param float $value Records timing in milliseconds. For example: a value of 4 is stored as .004 seconds |
|
140
|
|
|
* in New Relic's systems |
|
141
|
|
|
* |
|
142
|
|
|
* @return bool |
|
143
|
|
|
*/ |
|
144
|
|
|
public function customMetric(string $metric, float $value): bool |
|
145
|
|
|
{ |
|
146
|
|
|
if (!$this->isLoaded()) { |
|
147
|
|
|
return false; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return newrelic_custom_metric($metric, $value); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Disable automatic injection of the New Relic Browser snippet on particular pages. |
|
155
|
|
|
* |
|
156
|
|
|
* Agent version 2.6.5.41 or higher. |
|
157
|
|
|
* |
|
158
|
|
|
* @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_disable_autorum |
|
159
|
|
|
* @see self::getBrowserTimingFooter |
|
160
|
|
|
* @see self::getBrowserTimingHeader |
|
161
|
|
|
* |
|
162
|
|
|
* @return bool |
|
163
|
|
|
*/ |
|
164
|
|
|
public function disableAutorum(): bool |
|
165
|
|
|
{ |
|
166
|
|
|
if (!$this->isLoaded()) { |
|
167
|
|
|
return false; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
return newrelic_disable_autorum(); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Stop timing the current transaction, but continue instrumenting it. |
|
175
|
|
|
* |
|
176
|
|
|
* Compatible with all agent versions. |
|
177
|
|
|
* |
|
178
|
|
|
* @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_end_of_transaction |
|
179
|
|
|
* @see self::endTransaction |
|
180
|
|
|
*/ |
|
181
|
|
|
public function endOfTransaction() |
|
182
|
|
|
{ |
|
183
|
|
|
if ($this->isLoaded()) { |
|
184
|
|
|
newrelic_end_of_transaction(); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Stop instrumenting the current transaction immediately. |
|
190
|
|
|
* |
|
191
|
|
|
* Agent version 3.0.5.95 or higher. |
|
192
|
|
|
* |
|
193
|
|
|
* @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_end_transaction |
|
194
|
|
|
* @see self::endOfTransaction |
|
195
|
|
|
* @see self::startTransaction |
|
196
|
|
|
* |
|
197
|
|
|
* @param bool $ignore (optional) Defaults to false |
|
198
|
|
|
* |
|
199
|
|
|
* @return bool |
|
200
|
|
|
*/ |
|
201
|
|
|
public function endTransaction(bool $ignore = false): bool |
|
202
|
|
|
{ |
|
203
|
|
|
if (!$this->isLoaded()) { |
|
204
|
|
|
return false; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
return newrelic_end_transaction($ignore); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Returns a New Relic Browser snippet to inject at the end of the HTML output. |
|
212
|
|
|
* |
|
213
|
|
|
* Compatible with all agent versions. |
|
214
|
|
|
* |
|
215
|
|
|
* @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_get_browser_timing_footer |
|
216
|
|
|
* @see self::disableAutorum |
|
217
|
|
|
* @see self::getBrowserTimingHeader |
|
218
|
|
|
* |
|
219
|
|
|
* @param bool $tags (optional) Defaults to true. If true or omitted, the string is enclosed in a <script> |
|
220
|
|
|
* element for easy inclusion in the page's HTML |
|
221
|
|
|
* |
|
222
|
|
|
* @return string|null |
|
223
|
|
|
*/ |
|
224
|
|
|
public function getBrowserTimingFooter(bool $tags = true): ?string |
|
225
|
|
|
{ |
|
226
|
|
|
if (!$this->isLoaded()) { |
|
227
|
|
|
return null; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
return newrelic_get_browser_timing_footer($tags); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Returns a New Relic Browser snippet to inject in the head of your HTML output. |
|
235
|
|
|
* |
|
236
|
|
|
* Compatible with all agent versions. |
|
237
|
|
|
* |
|
238
|
|
|
* @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_get_browser_timing_header |
|
239
|
|
|
* @see self::disableAutorum |
|
240
|
|
|
* @see self::getBrowserTimingFooter |
|
241
|
|
|
* |
|
242
|
|
|
* @param bool $tags (optional) Defaults to true. If true or omitted, the string is enclosed in a <script> |
|
243
|
|
|
* element for easy inclusion in the page's HTML. |
|
244
|
|
|
* |
|
245
|
|
|
* @return string|null |
|
246
|
|
|
*/ |
|
247
|
|
|
public function getBrowserTimingHeader(bool $tags = true): ?string |
|
248
|
|
|
{ |
|
249
|
|
|
if (!$this->isLoaded()) { |
|
250
|
|
|
return null; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
return newrelic_get_browser_timing_header($tags); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Ignore the current transaction when calculating Apdex. |
|
258
|
|
|
* |
|
259
|
|
|
* Compatible with all agent versions. |
|
260
|
|
|
* |
|
261
|
|
|
* @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_ignore_apdex |
|
262
|
|
|
*/ |
|
263
|
|
|
public function ignoreApdex() |
|
264
|
|
|
{ |
|
265
|
|
|
if ($this->isLoaded()) { |
|
266
|
|
|
newrelic_ignore_apdex(); |
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* Do not instrument the current transaction. |
|
272
|
|
|
* |
|
273
|
|
|
* Compatible with all agent versions. |
|
274
|
|
|
* |
|
275
|
|
|
* @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_ignore_transaction |
|
276
|
|
|
*/ |
|
277
|
|
|
public function ignoreTransaction() |
|
278
|
|
|
{ |
|
279
|
|
|
if ($this->isLoaded()) { |
|
280
|
|
|
newrelic_ignore_transaction(); |
|
281
|
|
|
} |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* Set custom name for current transaction. |
|
286
|
|
|
* |
|
287
|
|
|
* Compatible with all agent versions. |
|
288
|
|
|
* |
|
289
|
|
|
* @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_name_transaction |
|
290
|
|
|
* |
|
291
|
|
|
* @param string $name Name of the transaction |
|
292
|
|
|
* |
|
293
|
|
|
* @return bool |
|
294
|
|
|
*/ |
|
295
|
|
|
public function nameTransaction(string $name): bool |
|
296
|
|
|
{ |
|
297
|
|
|
if (!$this->isLoaded()) { |
|
298
|
|
|
return false; |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
return newrelic_name_transaction($name); |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* Use these calls to collect errors that the PHP agent does not collect automatically and to set the callback |
|
306
|
|
|
* for your own error and exception handler. |
|
307
|
|
|
* |
|
308
|
|
|
* Agent version 2.6 or higher. |
|
309
|
|
|
* |
|
310
|
|
|
* @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_notice_error |
|
311
|
|
|
* |
|
312
|
|
|
* @param string|\Throwable|\Exception|int $message|$e|$e|$errno |
|
|
|
|
|
|
313
|
|
|
* @param \Throwable|\Exception|sring $e|$e|$errstr (optional) |
|
|
|
|
|
|
314
|
|
|
* @param string $errfile (optional) |
|
|
|
|
|
|
315
|
|
|
* @param int $errline (optional) |
|
|
|
|
|
|
316
|
|
|
* @param string $errcontext (optional) |
|
|
|
|
|
|
317
|
|
|
*/ |
|
318
|
|
|
public function noticeError(...$arguments) |
|
319
|
|
|
{ |
|
320
|
|
|
if ($this->isLoaded()) { |
|
321
|
|
|
newrelic_notice_error(...$arguments); |
|
322
|
|
|
} |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
/** |
|
326
|
|
|
* Record a custom event with the given name and attributes. |
|
327
|
|
|
* |
|
328
|
|
|
* Agent version 4.18.0.89 or higher. |
|
329
|
|
|
* |
|
330
|
|
|
* @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_record_custom_event |
|
331
|
|
|
* |
|
332
|
|
|
* @param string $name Name of the custom event |
|
333
|
|
|
* @param array $attributes Supply custom attributes as an associative array |
|
334
|
|
|
* |
|
335
|
|
|
* @since 2.0.0 |
|
336
|
|
|
*/ |
|
337
|
|
|
public function recordCustomEvent(string $name, array $attributes) |
|
338
|
|
|
{ |
|
339
|
|
|
if ($this->isLoaded()) { |
|
340
|
|
|
newrelic_record_custom_event($name, $attributes); |
|
341
|
|
|
} |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
/** |
|
345
|
|
|
* Records a datastore segment. |
|
346
|
|
|
* |
|
347
|
|
|
* Agent version 7.5.0.199 or higher. |
|
348
|
|
|
* |
|
349
|
|
|
* @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_record_datastore_segment |
|
350
|
|
|
* |
|
351
|
|
|
* @param callable $callback The function that should be timed to create the datastore segment |
|
352
|
|
|
* @param array $parameters An associative array of parameters describing the datastore call |
|
353
|
|
|
* |
|
354
|
|
|
* @return mixed The return value of $callback is returned. If an error occurs, false is returned, and an error |
|
355
|
|
|
* at the E_WARNING level will be triggered |
|
356
|
|
|
* |
|
357
|
|
|
* @since 3.0.0 |
|
358
|
|
|
*/ |
|
359
|
|
|
public function recordDatastoreSegment(callable $callback, array $parameters) |
|
360
|
|
|
{ |
|
361
|
|
|
if (!$this->isLoaded()) { |
|
362
|
|
|
return false; |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
|
|
return newrelic_record_datastore_segment($callback, $parameters); |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
/** |
|
369
|
|
|
* Sets the New Relic app name, which controls data rollup. |
|
370
|
|
|
* |
|
371
|
|
|
* Agent version 3.1.5.111 or higher. |
|
372
|
|
|
* |
|
373
|
|
|
* @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_set_appname |
|
374
|
|
|
* |
|
375
|
|
|
* @param string|array $name Name(s) of app metrics should be reported under in New Relic user interface |
|
376
|
|
|
* @param string $license (optional) Specify a different license key to report metrics to a different |
|
377
|
|
|
* New Relic account |
|
378
|
|
|
* @param bool $xmit (optional) Defaults to false |
|
379
|
|
|
* |
|
380
|
|
|
* @return bool |
|
381
|
|
|
*/ |
|
382
|
|
|
public function setAppname($name, string $license = null, bool $xmit = false): bool |
|
383
|
|
|
{ |
|
384
|
|
|
if (!$this->isLoaded()) { |
|
385
|
|
|
return false; |
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
|
|
$name = is_array($name) ? implode(';', $name) : $name; |
|
389
|
|
|
|
|
390
|
|
|
if (isset($license)) { |
|
391
|
|
|
return newrelic_set_appname($name, $license, $xmit); |
|
392
|
|
|
} |
|
393
|
|
|
|
|
394
|
|
|
return newrelic_set_appname($name); |
|
395
|
|
|
} |
|
396
|
|
|
|
|
397
|
|
|
/** |
|
398
|
|
|
* Create user-related custom attributes. newrelic_add_custom_parameter is more flexible. |
|
399
|
|
|
* |
|
400
|
|
|
* Agent version 3.1.5.111 or higher. |
|
401
|
|
|
* |
|
402
|
|
|
* @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_set_user_attributes |
|
403
|
|
|
* @see self::addCustomParameter |
|
404
|
|
|
* |
|
405
|
|
|
* @param string $user Specify a name or username to associate with this page view |
|
406
|
|
|
* @param string $account Specify the name of a user account to associate with this page view |
|
407
|
|
|
* @param string $product Specify the name of a product to associate with this page view |
|
408
|
|
|
* |
|
409
|
|
|
* @return bool |
|
410
|
|
|
*/ |
|
411
|
|
|
public function setUserAttributes(string $user, string $account, string $product): bool |
|
412
|
|
|
{ |
|
413
|
|
|
if (!$this->isLoaded()) { |
|
414
|
|
|
return false; |
|
415
|
|
|
} |
|
416
|
|
|
|
|
417
|
|
|
return newrelic_set_user_attributes($user, $account, $product); |
|
418
|
|
|
} |
|
419
|
|
|
|
|
420
|
|
|
/** |
|
421
|
|
|
* Starts a new transaction, usually after manually ending a transaction. |
|
422
|
|
|
* |
|
423
|
|
|
* Agent version 3.0.5.95 or higher. |
|
424
|
|
|
* |
|
425
|
|
|
* @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_start_transaction |
|
426
|
|
|
* @see self::endTransaction |
|
427
|
|
|
* |
|
428
|
|
|
* @param string $appname The application name to associate with data from this transaction |
|
429
|
|
|
* @param string $license (optional) Provide a different license key if you want the transaction to report |
|
430
|
|
|
* to a different New Relic account |
|
431
|
|
|
* |
|
432
|
|
|
* @return bool |
|
433
|
|
|
*/ |
|
434
|
|
|
public function startTransaction(string $appname, string $license = null): bool |
|
435
|
|
|
{ |
|
436
|
|
|
if (!$this->isLoaded()) { |
|
437
|
|
|
return false; |
|
438
|
|
|
} |
|
439
|
|
|
|
|
440
|
|
|
if (isset($license)) { |
|
441
|
|
|
return newrelic_start_transaction($appname, $license); |
|
442
|
|
|
} |
|
443
|
|
|
|
|
444
|
|
|
return newrelic_start_transaction($name); |
|
|
|
|
|
|
445
|
|
|
} |
|
446
|
|
|
} |
|
447
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.