-
Notifications
You must be signed in to change notification settings - Fork 13
/
ProductionAgent.php
335 lines (302 loc) · 9.16 KB
/
ProductionAgent.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?php
declare(strict_types=1);
namespace Contributte\NewRelic\Agent;
/**
* @link https://docs.newrelic.com/docs/agents/php-agent/php-agent-api
*/
final class ProductionAgent implements Agent
{
/**
* @var bool
*/
private $enabled;
public function __construct(bool $enabled)
{
$this->enabled = $enabled && (bool) ini_get('newrelic.enabled');
}
public function isEnabled(): bool
{
return $this->enabled;
}
/**
* Accepts an array of distributed trace headers.
*
* @link https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelicacceptdistributedtraceheaders
*/
public function acceptDistributedTraceHeaders(array $headers, string $transportType): bool
{
return $this->enabled
? newrelic_accept_distributed_trace_headers ($headers, $transportType)
: false;
}
/**
* Inserts W3C Trace Context headers and New Relic Distributed Tracing headers into an outbound array of headers.
*
* @link https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelicinsertdistributedtraceheaders
*/
public function insertDistributedTraceHeaders(array $headers): bool
{
return $this->enabled
? newrelic_insert_distributed_trace_headers($headers)
: false;
}
/**
* Attaches a custom attribute (key/value pair) to the current transaction and the current span (if enabled).
*
* @link https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_add_custom_parameter
*
* @param bool|float|int|string $value
*/
public function addCustomParameter(string $key, $value): bool
{
return $this->enabled
? newrelic_add_custom_parameter($key, $value)
: false;
}
/**
* Attaches a custom attribute (key/value pair) to the current span.
*
* @link https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelicaddcustomspanparameter-php-agent-api
*
* @param bool|float|int|string $value
*/
public function addCustomSpanParameter(string $key, $value): bool
{
return $this->enabled
? newrelic_add_custom_span_parameter($key, $value)
: false;
}
/**
* Specify functions or methods for the agent to instrument with custom instrumentation.
*
* @link https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_add_custom_tracer
*/
public function addCustomTracer(string $functionName): bool
{
return $this->enabled
? newrelic_add_custom_tracer($functionName)
: false;
}
/**
* Manually specify that a transaction is a background job or a web transaction.
*
* @link https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_background_job
*/
public function backgroundJob(bool $flag = true): void
{
newrelic_background_job($flag);
}
/**
* Enable or disable the capture of URL parameters.
*
* @link https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_capture_params
*/
public function captureParams(bool $flag = true): void
{
newrelic_capture_params($flag);
}
/**
* Add a custom metric (in milliseconds) to time a component of your app not captured by default.
*
* @link https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newreliccustommetric-php-agent-api
*/
public function customMetric(string $metricName, float $value): bool
{
return $this->enabled
? newrelic_custom_metric('Custom/' . $metricName, $value)
: false;
}
/**
* Disable automatic injection of the browser monitoring snippet on particular pages.
*
* @link https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_disable_autorum
*/
public function disableAutorum(): void
{
newrelic_disable_autorum();
}
/**
* Stop timing the current transaction, but continue instrumenting it.
*
* @link https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_end_of_transaction
*/
public function endOfTransaction(): void
{
newrelic_end_of_transaction();
}
/**
* Stop instrumenting the current transaction immediately.
*
* @link https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_end_transaction
*/
public function endTransaction(bool $ignore = false): bool
{
return $this->enabled
? newrelic_end_transaction($ignore)
: false;
}
/**
* Returns a browser monitoring snippet to inject at the end of the HTML output.
*
* @link https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_get_browser_timing_footer
*/
public function getBrowserTimingFooter(bool $includeTags = true): string
{
return $this->enabled
? newrelic_get_browser_timing_footer($includeTags)
: '';
}
/**
* Returns a browser monitoring snippet to inject in the head of your HTML output.
*
* @link https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_get_browser_timing_header
*/
public function getBrowserTimingHeader(bool $includeTags = true): string
{
return $this->enabled
? newrelic_get_browser_timing_header($includeTags)
: '';
}
/**
* Returns a collection of metadata necessary for linking data to a trace or an entity.
*
* @link https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelicgetlinkingmetadata
*/
public function getLinkingMetadata(): array
{
return $this->enabled
? newrelic_get_linking_metadata()
: [];
}
/**
* Returns an associative array containing the identifiers of the current trace and the parent span.
*
* @link https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelicgettracemetadata
*/
public function getTraceMetadata(): array
{
return $this->enabled
? newrelic_get_trace_metadata()
: [];
}
/**
* Ignore the current transaction when calculating Apdex.
*
* @link https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_ignore_apdex
*/
public function ignoreApdex(): void
{
newrelic_ignore_apdex();
}
/**
* Do not instrument the current transaction.
*
* @link https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_ignore_transaction
*/
public function ignoreTransaction(): void
{
newrelic_ignore_transaction();
}
/**
* Returns a value indicating whether or not the current transaction is marked as sampled.
*
* @link https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelicissampled
*/
public function isSampled(): bool
{
return $this->enabled
? newrelic_is_sampled()
: false;
}
/**
* Set custom name for current transaction.
*
* @link https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_name_transaction
*/
public function nameTransaction(string $name): bool
{
return $this->enabled
? newrelic_name_transaction($name)
: false;
}
/**
* Use these calls to collect errors that the PHP agent does not collect automatically and to set the callback
* for your own error and exception handler.
*
* @link https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_notice_error
*/
public function noticeError(string $message, ?\Throwable $e = null): void
{
newrelic_notice_error($message, $e);
}
/**
* Use these calls to collect errors that the PHP agent does not collect automatically and to set the callback
* for your own error and exception handler.
*
* @link https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_notice_error
*/
public function noticeFullError(
int $errno,
string $errstr,
?string $errfile = null,
?int $errline = null,
?string $errcontext = null
): void {
newrelic_notice_error($errno, $errstr, $errfile, $errline, $errcontext);
}
/**
* Record a custom event with the given name and attributes.
*
* @link https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_record_custom_event
*/
public function recordCustomEvent(string $name, array $attributes): void
{
newrelic_record_custom_event($name, $attributes);
}
/**
* Records a datastore segment.
*
* @link https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_record_datastore_segment
*
* @return mixed
*/
public function recordDatastoreSegment(callable $func, array $parameters)
{
return $this->enabled
? newrelic_record_datastore_segment($func, $parameters)
: false;
}
/**
* Sets the New Relic app name, which controls data rollup.
*
* @link https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_set_appname
*/
public function setAppName(string $name, string $license = '', bool $xmit = false): bool
{
return $this->enabled
? newrelic_set_appname($name, $license, $xmit)
: false;
}
/**
* Create user-related custom attributes. newrelic_add_custom_parameter is more flexible.
*
* @link https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_set_user_attributes
*/
public function setUserAttributes(string $userValue, string $accountValue, string $productValue): bool
{
return $this->enabled
? newrelic_set_user_attributes($userValue, $accountValue, $productValue)
: false;
}
/**
* Starts a new transaction, usually after manually ending a transaction.
*
* @link https://docs.newrelic.com/docs/agents/php-agent/php-agent-api/newrelic_start_transaction
*/
public function startTransaction(string $appName, string $license = ''): bool
{
return $this->enabled
? newrelic_start_transaction($appName, $license)
: false;
}
}