Agent::acceptDistributedTracePayloadHttpsafe()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 6
rs 10
c 0
b 0
f 0
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
final class Agent
18
{
19
    /**
20
     * Extension status.
21
     *
22
     * @var bool
23
     */
24
    private $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($name = null, string $license = null)
37
    {
38
        $this->loaded = extension_loaded('newrelic');
39
40
        if (isset($name, $license)) {
41
            $this->setAppname($name, $license);
42
        } elseif (isset($name)) {
43
            $this->setAppname($name);
44
        }
45
    }
46
47
    /**
48
     * Check if the New Relic extension is loaded.
49
     *
50
     * @return bool
51
     */
52 1
    public function isLoaded(): bool
53
    {
54 1
        return $this->loaded;
55
    }
56
57
    /**
58
     * Accepts a distributed trace payload.
59
     *
60
     * Agent version 8.4 or higher.
61
     *
62
     * @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelicacceptdistributedtracepayload-php-agent-api
63
     * @see self::createDistributedTracePayload
64
     *
65
     * @param string $payload JSON formatted string created by using Agent::addCustomParameter
66
     *
67
     * @since 2.1.0
68
     */
69
    public function acceptDistributedTracePayload(string $payload)
70
    {
71
        if ($this->isLoaded()) {
72
            newrelic_accept_distributed_trace_payload($payload);
0 ignored issues
show
Bug introduced by
The function newrelic_accept_distributed_trace_payload was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
            /** @scrutinizer ignore-call */ 
73
            newrelic_accept_distributed_trace_payload($payload);
Loading history...
73
        }
74
    }
75
76
77
    /**
78
     * Accepts a distributed trace payload.
79
     *
80
     * Agent version 8.4 or higher.
81
     *
82
     * @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelicacceptdistributedtracepayloadhttpsafe-php-agent-api
83
     * @see self::createDistributedTracePayload
84
     *
85
     * @param string $payload JSON formatted string created by using self::createDistributedTracePayload
86
     * @param string $type    (optional) A string overriding the default transport type.
87
     *
88
     * @return bool
89
     *
90
     * @since 2.1.0
91
     */
92
    public function acceptDistributedTracePayloadHttpsafe(string $payload, string $type = 'HTTP'): bool
93
    {
94
        if (!$this->isLoaded()) {
95
            return false;
96
        }
97
98
        return newrelic_accept_distributed_trace_payload_httpsafe($payload, $type);
0 ignored issues
show
Bug introduced by
The function newrelic_accept_distributed_trace_payload_httpsafe was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

98
        return /** @scrutinizer ignore-call */ newrelic_accept_distributed_trace_payload_httpsafe($payload, $type);
Loading history...
99
    }
100
101
    /**
102
     * Attaches a custom attribute (key/value pair) to the current transaction.
103
     *
104
     * Agent version 4.4.5.35 or higher.
105
     *
106
     * @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_add_custom_parameter
107
     *
108
     * @param string $key   The name of the custom attribute. Only the first 255 characters are retained
109
     * @param mixed  $value The value to associate with this custom attribute
110
     *
111
     * @return bool
112
     */
113
    public function addCustomParameter(string $key, $value): bool
114
    {
115
        if (!$this->isLoaded()) {
116
            return false;
117
        }
118
119
        return newrelic_add_custom_parameter($key, $value);
120
    }
121
122
    /**
123
     * Specify functions or methods for the agent to instrument with custom instrumentation.
124
     *
125
     * Compatible with all agent versions.
126
     *
127
     * @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_add_custom_tracer
128
     *
129
     * @param string $callable The name can be formatted either as function_name for procedural functions,
130
     *                         or as "ClassName::method" for methods
131
     *
132
     * @return bool
133
     */
134
    public function addCustomTracer(string $callable): bool
135
    {
136
        if (!$this->isLoaded()) {
137
            return false;
138
        }
139
140
        return newrelic_add_custom_tracer($callable);
141
    }
142
143
    /**
144
     * Manually specify that a transaction is a background job or a web transaction.
145
     *
146
     * Compatible with all agent versions.
147
     *
148
     * @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_background_job
149
     *
150
     * @param bool $flag (optional) Defaults to true
151
     */
152
    public function backgroundJob(bool $flag = true)
153
    {
154
        if ($this->isLoaded()) {
155
            newrelic_background_job($flag);
156
        }
157
    }
158
159
    /**
160
     * Enable or disable the capture of URL parameters.
161
     *
162
     * Compatible with all agent versions.
163
     *
164
     * @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_capture_params
165
     *
166
     * @param bool $enable (optional) Defaults to true
167
     */
168
    public function captureParams(bool $enable = true)
169
    {
170
        if ($this->isLoaded()) {
171
            newrelic_capture_params($enable);
172
        }
173
    }
174
175
    /**
176
     * Creates a distributed trace payload.
177
     *
178
     * Agent version 8.4 or higher.
179
     *
180
     * @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newreliccreatedistributedtracepayload-php-agent-api
181
     *
182
     * @return \newrelic\DistributedTracePayload
183
     *
184
     * @since 2.1.0
185
     */
186
    public function createDistributedTracePayload(): ?\newrelic\DistributedTracePayload
0 ignored issues
show
Bug introduced by
The type newrelic\DistributedTracePayload was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
187
    {
188
        if ($this->isLoaded()) {
189
            return newrelic_create_distributed_trace_payload();
0 ignored issues
show
Bug introduced by
The function newrelic_create_distributed_trace_payload was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

189
            return /** @scrutinizer ignore-call */ newrelic_create_distributed_trace_payload();
Loading history...
190
        }
191
    }
192
193
194
    /**
195
     * Add a custom metric (in milliseconds) to time a component of your app not captured by default.
196
     *
197
     * Compatible with all agent versions.
198
     *
199
     * @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newreliccustommetric-php-agent-api
200
     *
201
     * @param string $metric Name your custom metrics with a Custom/ prefix (for example, Custom/MyMetric)
202
     * @param float  $value  Records timing in milliseconds. For example: a value of 4 is stored as .004 seconds
203
     *                       in New Relic's systems
204
     *
205
     * @return bool
206
     */
207
    public function customMetric(string $metric, float $value): bool
208
    {
209
        if (!$this->isLoaded()) {
210
            return false;
211
        }
212
213
        return newrelic_custom_metric($metric, $value);
214
    }
215
216
    /**
217
     * Disable automatic injection of the New Relic Browser snippet on particular pages.
218
     *
219
     * Agent version 2.6.5.41 or higher.
220
     *
221
     * @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_disable_autorum
222
     * @see self::getBrowserTimingFooter
223
     * @see self::getBrowserTimingHeader
224
     *
225
     * @return bool
226
     */
227
    public function disableAutorum(): bool
228
    {
229
        if (!$this->isLoaded()) {
230
            return false;
231
        }
232
233
        return newrelic_disable_autorum();
234
    }
235
236
    /**
237
     * Stop timing the current transaction, but continue instrumenting it.
238
     *
239
     * Compatible with all agent versions.
240
     *
241
     * @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_end_of_transaction
242
     * @see self::endTransaction
243
     */
244
    public function endOfTransaction()
245
    {
246
        if ($this->isLoaded()) {
247
            newrelic_end_of_transaction();
248
        }
249
    }
250
251
    /**
252
     * Stop instrumenting the current transaction immediately.
253
     *
254
     * Agent version 3.0.5.95 or higher.
255
     *
256
     * @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_end_transaction
257
     * @see self::endOfTransaction
258
     * @see self::startTransaction
259
     *
260
     * @param bool $ignore (optional) Defaults to false
261
     *
262
     * @return bool
263
     */
264
    public function endTransaction(bool $ignore = false): bool
265
    {
266
        if (!$this->isLoaded()) {
267
            return false;
268
        }
269
270
        return newrelic_end_transaction($ignore);
271
    }
272
273
    /**
274
     * Returns a New Relic Browser snippet to inject at the end of the HTML output.
275
     *
276
     * Compatible with all agent versions.
277
     *
278
     * @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_get_browser_timing_footer
279
     * @see self::disableAutorum
280
     * @see self::getBrowserTimingHeader
281
     *
282
     * @param bool $tags (optional) Defaults to true. If true or omitted, the string is enclosed in a <script>
283
     *                   element for easy inclusion in the page's HTML
284
     *
285
     * @return string
286
     */
287
    public function getBrowserTimingFooter(bool $tags = true): string
288
    {
289
        if (!$this->isLoaded()) {
290
            return '';
291
        }
292
293
        return newrelic_get_browser_timing_footer($tags);
294
    }
295
296
    /**
297
     * Returns a New Relic Browser snippet to inject in the head of your HTML output.
298
     *
299
     * Compatible with all agent versions.
300
     *
301
     * @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_get_browser_timing_header
302
     * @see self::disableAutorum
303
     * @see self::getBrowserTimingFooter
304
     *
305
     * @param bool $tags (optional) Defaults to true. If true or omitted, the string is enclosed in a <script>
306
     *                   element for easy inclusion in the page's HTML.
307
     *
308
     * @return string
309
     */
310
    public function getBrowserTimingHeader(bool $tags = true): string
311
    {
312
        if (!$this->isLoaded()) {
313
            return '';
314
        }
315
316
        return newrelic_get_browser_timing_header($tags);
317
    }
318
319
    /**
320
     * Ignore the current transaction when calculating Apdex.
321
     *
322
     * Compatible with all agent versions.
323
     *
324
     * @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_ignore_apdex
325
     */
326
    public function ignoreApdex()
327
    {
328
        if ($this->isLoaded()) {
329
            newrelic_ignore_apdex();
330
        }
331
    }
332
333
    /**
334
     * Do not instrument the current transaction.
335
     *
336
     * Compatible with all agent versions.
337
     *
338
     * @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_ignore_transaction
339
     */
340
    public function ignoreTransaction()
341
    {
342
        if ($this->isLoaded()) {
343
            newrelic_ignore_transaction();
344
        }
345
    }
346
347
    /**
348
     * Set custom name for current transaction.
349
     *
350
     * Compatible with all agent versions.
351
     *
352
     * @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_name_transaction
353
     *
354
     * @param string $name Name of the transaction
355
     *
356
     * @return bool
357
     */
358
    public function nameTransaction(string $name): bool
359
    {
360
        if (!$this->isLoaded()) {
361
            return false;
362
        }
363
364
        return newrelic_name_transaction($name);
365
    }
366
367
    /**
368
     * Use these calls to collect errors that the PHP agent does not collect automatically and to set the callback
369
     * for your own error and exception handler.
370
     *
371
     * Agent version 2.6 or higher.
372
     *
373
     * @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_notice_error
374
     *
375
     * @param string|\Throwable|\Exception|int $message|$e|$e|$errno Provide an error message that will be meaningful
376
     *                                                               to you when it displays in error traces.
377
     * @param \Throwable|\Exception|string     $e|$e|$errstr         (optional)
378
     * @param string                           $errfile              (optional)
379
     * @param int                              $errline              (optional)
380
     * @param string                           $errcontext           (optional)
381
     */
382
    public function noticeError(...$arguments)
383
    {
384
        if ($this->isLoaded()) {
385
            newrelic_notice_error(...$arguments);
0 ignored issues
show
Bug introduced by
$arguments is expanded, but the parameter $messageOrUnused of newrelic_notice_error() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

385
            newrelic_notice_error(/** @scrutinizer ignore-type */ ...$arguments);
Loading history...
386
        }
387
    }
388
389
    /**
390
     * Record a custom event with the given name and attributes.
391
     *
392
     * Agent version 4.18.0.89 or higher.
393
     *
394
     * @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_record_custom_event
395
     *
396
     * @param string $name       Name of the custom event
397
     * @param array  $attributes Supply custom attributes as an associative array
398
     *
399
     * @since 2.0.0
400
     */
401
    public function recordCustomEvent(string $name, array $attributes)
402
    {
403
        if ($this->isLoaded()) {
404
            newrelic_record_custom_event($name, $attributes);
405
        }
406
    }
407
408
    /**
409
     * Records a datastore segment.
410
     *
411
     * Agent version 7.5.0.199 or higher.
412
     *
413
     * @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_record_datastore_segment
414
     *
415
     * @param callable $callback   The function that should be timed to create the datastore segment
416
     * @param array    $parameters An associative array of parameters describing the datastore call
417
     *
418
     * @return mixed The return value of $callback is returned. If an error occurs, false is returned, and an error
419
     *               at the E_WARNING level will be triggered
420
     *
421
     * @since 2.1.0
422
     */
423
    public function recordDatastoreSegment(callable $callback, array $parameters)
424
    {
425
        if (!$this->isLoaded()) {
426
            return false;
427
        }
428
429
        return newrelic_record_datastore_segment($callback, $parameters);
0 ignored issues
show
Bug introduced by
The function newrelic_record_datastore_segment was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

429
        return /** @scrutinizer ignore-call */ newrelic_record_datastore_segment($callback, $parameters);
Loading history...
430
    }
431
432
    /**
433
     * Sets the New Relic app name, which controls data rollup.
434
     *
435
     * Agent version 3.1.5.111 or higher.
436
     *
437
     * @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_set_appname
438
     *
439
     * @param string|array $name    Name(s) of app metrics should be reported under in New Relic user interface
440
     * @param string       $license (optional) Specify a different license key to report metrics to a different
441
     *                              New Relic account
442
     * @param bool         $xmit    (optional) Defaults to false
443
     *
444
     * @return bool
445
     */
446
    public function setAppname($name, string $license = null, bool $xmit = false): bool
447
    {
448
        if (!$this->isLoaded()) {
449
            return false;
450
        }
451
452
        $name = is_array($name) ? implode(';', $name) : $name;
453
454
        if (isset($license)) {
455
            return newrelic_set_appname($name, $license, $xmit);
456
        }
457
458
        return newrelic_set_appname($name);
459
    }
460
461
    /**
462
     * Create user-related custom attributes. newrelic_add_custom_parameter is more flexible.
463
     *
464
     * Agent version 3.1.5.111 or higher.
465
     *
466
     * @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_set_user_attributes
467
     * @see self::addCustomParameter
468
     *
469
     * @param string $user    Specify a name or username to associate with this page view
470
     * @param string $account Specify the name of a user account to associate with this page view
471
     * @param string $product Specify the name of a product to associate with this page view
472
     *
473
     * @return bool
474
     */
475
    public function setUserAttributes(string $user, string $account, string $product): bool
476
    {
477
        if (!$this->isLoaded()) {
478
            return false;
479
        }
480
481
        return newrelic_set_user_attributes($user, $account, $product);
482
    }
483
484
    /**
485
     * Starts a new transaction, usually after manually ending a transaction.
486
     *
487
     * Agent version 3.0.5.95 or higher.
488
     *
489
     * @see https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_start_transaction
490
     * @see self::endTransaction
491
     *
492
     * @param string $appname The application name to associate with data from this transaction
493
     * @param string $license (optional) Provide a different license key if you want the transaction to report
494
     *                        to a different New Relic account
495
     *
496
     * @return bool
497
     */
498
    public function startTransaction(string $appname, string $license = null): bool
499
    {
500
        if (!$this->isLoaded()) {
501
            return false;
502
        }
503
504
        if (isset($license)) {
505
            return newrelic_start_transaction($appname, $license);
506
        }
507
508
        return newrelic_start_transaction($appname);
509
    }
510
}
511