This repository has been archived by the owner on Feb 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathext_newrelic.php
456 lines (383 loc) · 15.1 KB
/
ext_newrelic.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
<?hh
//not implemented yet
function newrelic_set_appname(string $name, string $key, bool $xmit): mixed {}
//The same as newrelic_add_attribute, but like in the officical NewRelic PHP API
function newrelic_add_custom_parameter(string $name, string $value) {
newrelic_add_attribute_intern($name, $value);
}
//not implemented yet
function newrelic_disable_autorum() {}
function newrelic_notice_error(?string $error_message, \Exception $e = null) {
if ($e) {
if (!$error_message) {
$error_message = $e->getMessage();
}
$exception_type = get_class($e);
$stack_trace = $e->getTraceAsString();
} else {
$exception_type = "";
$stack_trace = NewRelicExtensionHelper::debug_backtrace_string();
}
$stack_frame_delimiter = "\n";
newrelic_notice_error_intern( $exception_type, $error_message, $stack_trace, $stack_frame_delimiter);
}
//not implemented yet
function newrelic_background_job(bool $true) {}
function newrelic_start_transaction(string $appname = null, string $license = null): int {
return newrelic_start_transaction_intern();
}
function newrelic_name_transaction(string $name): int {
return newrelic_name_transaction_intern($name);
}
//not implemented yet
function newrelic_ignore_transaction() {}
//not implemented yet
function newrelic_ignore_apdex() {}
function newrelic_profiling_enable(int $level) {
if (newrelic_has_native_hotprofiler()) {
newrelic_set_external_profiler($level);
xhprof_enable(0x400);
} else {
NewRelicExtensionHelper::setMaxDepth($level);
fb_setprofile(array("NewRelicExtensionHelper","profile"));
newrelic_add_custom_parameter("HotProfiler","emulated");
}
}
function newrelic_profiling_disable() {
if (newrelic_has_native_hotprofiler()) {
xhprof_disable();
} else {
NewRelicExtensionHelper::endAll();
fb_setprofile(null);
}
}
function newrelic_has_native_hotprofiler() {
return (defined("EXTERNAL_HOTPROFILER_FIX") || version_compare(HHVM_VERSION, "3.6", ">="));
}
//not implemented yet
function newrelic_capture_params($enable) {}
//not implemented yet
function newrelic_get_browser_timing_header($flag) {}
//not implemented yet
function newrelic_get_browser_timing_footer($flag) {}
//not implemented yet
function newrelic_set_user_attributes($user, $account, $product) {}
class NewRelicExtensionHelper {
protected static Vector<int> $stack = Vector {};
// there's an issue with the depth, if you enable/disable it at different depths... have to figure something out
protected static int $depth = 0;
protected static int $maxdepth = 7;
static function profile (string $mode, string $name, array $options = null): void {
if ($name) {
if ($mode == 'enter') {
if (self::$depth < self::$maxdepth) {
self::$stack->add(newrelic_segment_generic_begin($name));
} else {
//self::$stack->add(0);
}
self::$depth++;
} else {
if (self::$depth < self::$maxdepth + 1) {
try {
$id = self::$stack->pop();
if ($id) {
newrelic_segment_end($id);
}
} catch (Exception $e) {}
}
self::$depth--;
}
}
}
static function setMaxDepth(int $depth): void {
self::$maxdepth = $depth;
}
static function endAll(): void {
while (self::$stack->count()) {
$id = self::$stack->pop();
if ($id) {
newrelic_segment_end($id);
}
}
self::$depth = 0;
}
static function errorCallback($type, $message, $c) {
$errno = $errno & error_reporting();
if($errno == 0) return false;
$exception_type = self::friendlyErrorType($type);
$error_message = $message;
$stack_trace = self::debug_backtrace_string();
$stack_frame_delimiter = "\n";
newrelic_notice_error_intern( $exception_type, $error_message, $stack_trace, $stack_frame_delimiter);
return false;
}
static function exceptionCallback($e) {
$exception_type = get_class($e);
$error_message = $e->getMessage();
$stack_trace = $e->getTraceAsString();
$stack_frame_delimiter = "\n";
newrelic_notice_error_intern( $exception_type, $error_message, $stack_trace, $stack_frame_delimiter);
}
static function debug_backtrace_string() {
$stack = '';
$i = 1;
$trace = debug_backtrace();
unset($trace[0]); //Remove call to this function from stack trace
foreach($trace as $key => $node) {
$stack .= "#$i ";
if (isset($node['file'])) {
$stack .= $node['file'] ."(" .$node['line']."): ";
}
if ($key > 1) {
if(isset($node['class'])) {
$stack .= $node['class'] . "->";
}
$stack .= $node['function'] . "()" . PHP_EOL;
} else {
$stack .= PHP_EOL;
}
$i++;
}
return $stack;
}
static function friendlyErrorType($type)
{
switch($type)
{
case E_ERROR: // 1 //
return 'E_ERROR';
case E_WARNING: // 2 //
return 'E_WARNING';
case E_PARSE: // 4 //
return 'E_PARSE';
case E_NOTICE: // 8 //
return 'E_NOTICE';
case E_CORE_ERROR: // 16 //
return 'E_CORE_ERROR';
case E_CORE_WARNING: // 32 //
return 'E_CORE_WARNING';
case E_CORE_ERROR: // 64 //
return 'E_COMPILE_ERROR';
case E_CORE_WARNING: // 128 //
return 'E_COMPILE_WARNING';
case E_USER_ERROR: // 256 //
return 'E_USER_ERROR';
case E_USER_WARNING: // 512 //
return 'E_USER_WARNING';
case E_USER_NOTICE: // 1024 //
return 'E_USER_NOTICE';
case E_STRICT: // 2048 //
return 'E_STRICT';
case E_RECOVERABLE_ERROR: // 4096 //
return 'E_RECOVERABLE_ERROR';
case E_DEPRECATED: // 8192 //
return 'E_DEPRECATED';
case E_USER_DEPRECATED: // 16384 //
return 'E_USER_DEPRECATED';
}
return "UNKNOWN ERROR TYPE";
}
}
<<__Native>>
function newrelic_start_transaction_intern(): int;
<<__Native>>
function newrelic_name_transaction_intern(string $name): int;
<<__Native>>
function newrelic_transaction_set_request_url(string $name): int;
<<__Native>>
function newrelic_transaction_set_threshold(int $threshold): int;
<<__Native>>
function newrelic_end_transaction(): int;
<<__Native>>
function newrelic_segment_generic_begin(string $name): int;
<<__Native>>
function newrelic_segment_datastore_begin(string $table, string $operation, string $sql = "", string $sql_trace_rollup_name = ""): int;
<<__Native>>
function newrelic_segment_external_begin(string $host, string $name): int;
<<__Native>>
function newrelic_segment_end(int $id): int;
<<__Native>>
function newrelic_get_scoped_generic_segment(string $name): mixed;
<<__Native>>
function newrelic_get_scoped_database_segment(string $table, string $operation, string $sql = "", string $sql_trace_rollup_name = ""): mixed;
<<__Native>>
function newrelic_get_scoped_external_segment(string $host, string $name): mixed;
<<__Native>>
function newrelic_transaction_set_max_trace_segments(int $threshold): int;
<<__Native>>
function newrelic_notice_error_intern(string $exception_type, string $error_message, string $stack_trace, string $stack_frame_delimiter): int;
<<__Native>>
function newrelic_add_attribute_intern(string $name, string $value): int;
<<__Native>>
function newrelic_set_external_profiler(int $maxdepth = 7): void;
<<__Native>>
function newrelic_custom_metric(string $name, float $value): int;
/**
* Core Overrides
*/
// sql helper/parser
function _newrelic_parse_query($query): array {
if (preg_match( '/^\s*SELECT/i', $query)) {
if (preg_match('/\s+FROM\s+[`\'"]?([a-z\d_\.]+)[`\'"]?/i', $query, $match)) {
return ['select', $match[1]];
} else if (preg_match('/\s+[`\'"]?([a-z\d_\.]+)[`\'"]?/i', $query, $match)) {
return ['select', $match[1]];
} else {
return ['select', 'unknown'];
}
} else if (preg_match( '/^\s*INSERT/i', $query)) {
if (preg_match('/\s+INTO\s+[`\'"]?([a-z\d_\.]+)[`\'"]?/i', $query, $match)) {
return ['insert', $match[1]];
} else {
return ['insert', 'unknown'];
}
} else if (preg_match( '/^\s*UPDATE/i', $query)) {
if (preg_match('/UPDATE\s+[`\'"]?([a-z\d_\.]+)[`\'"]?/i', $query, $match)) {
return ['update', $match[1]];
} else {
return ['update', 'unknown'];
}
} else if (preg_match( '/^\s*DELETE/i', $query)) {
if (preg_match('/DELETE\s+[`\'"]?([a-z\d_\.]+)[`\'"]?/i', $query, $match)) {
return ['delete', $match[1]];
} else {
return ['delete', 'unknown'];
}
} else if (preg_match( '/^\s*SHOW/i', $query)) {
if (preg_match('/^\s*?(SHOW\s+[a-z\d_]+)/i', $query, $match)) {
return ['select', $match[1]];
} else {
return ['select', 'SHOW'];
}
} else if (preg_match( '/^\s*BEGIN/i', $query)) {
return ['select', 'BEGIN'];
} else if (preg_match( '/^\s*COMMIT/i', $query)) {
return ['select', 'COMMIT'];
} else if (preg_match( '/^\s*ROLLBACK/i', $query)) {
return ['select', 'ROLLBACK'];
}
return ['select', 'undefined'];
}
// PDO intercepts
function newrelic_pdo_intercept() {
// PDO::exec and PDO::query will be harder due to lifecycle of objects
//fb_intercept('PDO::exec', function ($name, $obj, $args, $data, &$done) { $done=false;});
//fb_intercept('PDO::query', function ($name, $obj, $args, $data, &$done) { $done=false;});
fb_intercept('PDOStatement::execute', function ($name, $obj, $args, $data, &$done) {
$query = $obj->queryString;
$a = _newrelic_parse_query($query);
$obj->_newrelic_segment = newrelic_get_scoped_database_segment($a[1], $a[0], $query);
$done=false;
});
}
// mysqli
function newrelic_mysqli_intercept() {
fb_intercept('mysqli::hh_real_query', function ($name, $obj, $args, $data, &$done) {
if (isset($obj->_newrelic_segment) && $obj->_newrelic_segment) {
newrelic_segment_end($obj->_newrelic_segment);
}
$query = $args[0];
$a = _newrelic_parse_query($query);
$obj->_newrelic_segment = newrelic_segment_datastore_begin($a[1], $a[0], $query);
$done = false;
});
fb_intercept('mysqli::store_result', '_newrelic_mysqli_segment_end');
fb_intercept('mysqli::use_result', '_newrelic_mysqli_segment_end');
}
function _newrelic_mysqli_segment_end($name, $obj, $args, $data, &$done) {
if (isset($obj->_newrelic_segment) && $obj->_newrelic_segment) {
newrelic_segment_end($obj->_newrelic_segment);
}
$obj->_newrelic_segment = 0;
$done = false;
}
// file_get_contents (e.g. solr)
function newrelic_file_get_contents(string $filename, bool $use_include_path = false, resource $context = null, int $offset = -1, int $maxlen = -1) {
$seg = newrelic_segment_external_begin($filename, 'file_get_contents');
$resp = @obs_file_get_contents($filename, $use_include_path, $context, $offset, $maxlen);
newrelic_segment_end($seg);
return $resp;
}
function newrelic_file_get_contents_intercept() {
fb_rename_function('file_get_contents', 'obs_file_get_contents');
fb_rename_function('newrelic_file_get_contents', 'file_get_contents');
}
// fread and fwrite (e.g. Redis)
function newrelic_fread(resource $handle, int $length) {
if (stream_get_meta_data($handle)['wrapper_type'] != 'plainfile') {
$seg = newrelic_segment_external_begin('sock_read[' . stream_socket_get_name($handle,true) . ']', 'fread');
} else {
$seg = newrelic_segment_external_begin('file', 'fread');
}
$resp = @obs_fread($handle, $length);
newrelic_segment_end($seg);
return $resp;
}
function newrelic_fwrite( resource $handle, string $string, int $length = -1 ) {
if (stream_get_meta_data($handle)['wrapper_type'] != 'plainfile') {
$seg = newrelic_segment_external_begin('sock_write[' . stream_socket_get_name($handle,true) . ']', 'fwrite');
} else {
$seg = newrelic_segment_external_begin('file', 'fwrite');
}
$resp = @obs_fwrite($handle, $string, $length);
newrelic_segment_end($seg);
return $resp;
}
function newrelic_fread_fwrite_intercept() {
fb_rename_function('fread', 'obs_fread');
fb_rename_function('newrelic_fread', 'fread');
fb_rename_function('fwrite', 'obs_fwrite');
fb_rename_function('newrelic_fwrite', 'fwrite');
}
// curl
function newrelic_curl_exec( resource $ch ) {
$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
$url = 'curl[' . substr($url, 0, strpos($url, '/', 8)) . ']';
$seg = newrelic_segment_external_begin($url, 'curl');
$resp = @obs_curl_exec($ch);
newrelic_segment_end($seg);
return $resp;
}
function newrelic_curl_intercept() {
fb_rename_function('curl_exec', 'obs_curl_exec');
fb_rename_function('newrelic_curl_exec', 'curl_exec');
}
// socket_read and socket_write (e.g. MongoDB (mongofill))
function newrelic_socket_read(resource $socket, int $length, int $type = PHP_BINARY_READ) {
if (stream_get_meta_data($socket)['wrapper_type'] != 'plainfile') {
$seg = newrelic_segment_external_begin('sock_read[' . stream_socket_get_name($socket,true) . ']', 'socket_read');
} else {
$seg = newrelic_segment_external_begin('file', 'socket_read');
}
$resp = @obs_socket_read($socket, $length. $type);
newrelic_segment_end($seg);
return $resp;
}
function newrelic_socket_write( resource $socket, string $string, int $length = 0 ) {
if (stream_get_meta_data($socket)['wrapper_type'] != 'plainfile') {
$seg = newrelic_segment_external_begin('sock_write[' . stream_socket_get_name($socket,true) . ']', 'socket_write');
} else {
$seg = newrelic_segment_external_begin('file', 'socket_write');
}
$resp = @obs_socket_write($socket, $string, $length);
newrelic_segment_end($seg);
return $resp;
}
function newrelic_socket_recv( resource $socket , &$buf , int $len , int $flags ) {
if (stream_get_meta_data($socket)['wrapper_type'] != 'plainfile') {
$seg = newrelic_segment_external_begin('sock_read[' . stream_socket_get_name($socket,true) . ']', 'socket_read');
} else {
$seg = newrelic_segment_external_begin('file', 'socket_read');
}
$resp = obs_socket_recv($socket, $buf, $len, $flags);
newrelic_segment_end($seg);
return $resp;
}
function newrelic_socket_read_write_intercept() {
fb_rename_function('socket_read', 'obs_socket_read');
fb_rename_function('newrelic_socket_read', 'socket_read');
fb_rename_function('socket_write', 'obs_socket_write');
fb_rename_function('newrelic_socket_write', 'socket_write');
fb_rename_function('socket_recv', 'obs_socket_recv');
fb_rename_function('newrelic_socket_recv', 'socket_recv');
}