-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscout_observer.c
135 lines (113 loc) · 5.54 KB
/
scout_observer.c
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
/*
* Scout APM extension for PHP
*
* Copyright (C) 2021
* For license information, please see the LICENSE file.
*/
#include "zend_scoutapm.h"
#include "scout_extern.h"
#if SCOUTAPM_INSTRUMENT_USING_OBSERVER_API == 1
#include "Zend/zend_observer.h"
#endif
int setup_functions_for_observer_api()
{
#if SCOUTAPM_INSTRUMENT_USING_OBSERVER_API == 1
ADD_MAGIC_FUNCTION_TO_INSTRUMENTATION_SAFE_CATCH("Predis\\Client->__call", "append");
ADD_MAGIC_FUNCTION_TO_INSTRUMENTATION_SAFE_CATCH("Predis\\Client->__call", "decr");
ADD_MAGIC_FUNCTION_TO_INSTRUMENTATION_SAFE_CATCH("Predis\\Client->__call", "decrBy");
ADD_MAGIC_FUNCTION_TO_INSTRUMENTATION_SAFE_CATCH("Predis\\Client->__call", "get");
ADD_MAGIC_FUNCTION_TO_INSTRUMENTATION_SAFE_CATCH("Predis\\Client->__call", "getBit");
ADD_MAGIC_FUNCTION_TO_INSTRUMENTATION_SAFE_CATCH("Predis\\Client->__call", "getRange");
ADD_MAGIC_FUNCTION_TO_INSTRUMENTATION_SAFE_CATCH("Predis\\Client->__call", "getSet");
ADD_MAGIC_FUNCTION_TO_INSTRUMENTATION_SAFE_CATCH("Predis\\Client->__call", "incr");
ADD_MAGIC_FUNCTION_TO_INSTRUMENTATION_SAFE_CATCH("Predis\\Client->__call", "incrBy");
ADD_MAGIC_FUNCTION_TO_INSTRUMENTATION_SAFE_CATCH("Predis\\Client->__call", "mGet");
ADD_MAGIC_FUNCTION_TO_INSTRUMENTATION_SAFE_CATCH("Predis\\Client->__call", "mSet");
ADD_MAGIC_FUNCTION_TO_INSTRUMENTATION_SAFE_CATCH("Predis\\Client->__call", "mSetNx");
ADD_MAGIC_FUNCTION_TO_INSTRUMENTATION_SAFE_CATCH("Predis\\Client->__call", "set");
ADD_MAGIC_FUNCTION_TO_INSTRUMENTATION_SAFE_CATCH("Predis\\Client->__call", "setBit");
ADD_MAGIC_FUNCTION_TO_INSTRUMENTATION_SAFE_CATCH("Predis\\Client->__call", "setEx");
ADD_MAGIC_FUNCTION_TO_INSTRUMENTATION_SAFE_CATCH("Predis\\Client->__call", "pSetEx");
ADD_MAGIC_FUNCTION_TO_INSTRUMENTATION_SAFE_CATCH("Predis\\Client->__call", "setNx");
ADD_MAGIC_FUNCTION_TO_INSTRUMENTATION_SAFE_CATCH("Predis\\Client->__call", "setRange");
ADD_MAGIC_FUNCTION_TO_INSTRUMENTATION_SAFE_CATCH("Predis\\Client->__call", "strlen");
ADD_MAGIC_FUNCTION_TO_INSTRUMENTATION_SAFE_CATCH("Predis\\Client->__call", "del");
ADD_FUNCTION_TO_INSTRUMENTATION_SAFE_CATCH("Elasticsearch\\Client->index");
ADD_FUNCTION_TO_INSTRUMENTATION_SAFE_CATCH("Elasticsearch\\Client->get");
ADD_FUNCTION_TO_INSTRUMENTATION_SAFE_CATCH("Elasticsearch\\Client->search");
ADD_FUNCTION_TO_INSTRUMENTATION_SAFE_CATCH("Elasticsearch\\Client->delete");
ADD_FUNCTION_TO_INSTRUMENTATION_SAFE_CATCH("Elastic\\Elasticsearch\\Client->index");
ADD_FUNCTION_TO_INSTRUMENTATION_SAFE_CATCH("Elastic\\Elasticsearch\\Client->get");
ADD_FUNCTION_TO_INSTRUMENTATION_SAFE_CATCH("Elastic\\Elasticsearch\\Client->search");
ADD_FUNCTION_TO_INSTRUMENTATION_SAFE_CATCH("Elastic\\Elasticsearch\\Client->delete");
#endif
return SUCCESS;
}
#if SCOUTAPM_INSTRUMENT_USING_OBSERVER_API == 1
static void scout_observer_begin(zend_execute_data *execute_data)
{
if (SCOUTAPM_G(all_instrumentation_enabled) != 1 || SCOUTAPM_G(currently_instrumenting)) {
return;
}
SCOUTAPM_G(observer_api_start_time) = scoutapm_microtime();
SCOUTAPM_G(currently_instrumenting) = 1;
}
static void scout_observer_end(zend_execute_data *execute_data, zval *return_value)
{
char *function_name, *magic_function_name;
size_t magic_function_name_len;
int argc;
zval *argv = NULL;
if (SCOUTAPM_G(all_instrumentation_enabled) != 1
|| SCOUTAPM_G(currently_instrumenting) != 1
|| SCOUTAPM_G(observer_api_start_time) <= 0
) {
return; // Possibly a weird situation? Not sure how we could get into this state
}
if (strcasecmp("__call", ZSTR_VAL(execute_data->func->common.function_name)) == 0) {
// @todo would be nice to find a way to "unpack" the array here into argv
ZEND_PARSE_PARAMETERS_START(1, -1)
Z_PARAM_STRING(magic_function_name, magic_function_name_len)
Z_PARAM_VARIADIC(' ', argv, argc)
ZEND_PARSE_PARAMETERS_END();
DYNAMIC_MALLOC_SPRINTF(function_name, magic_function_name_len, "%s->%s",
ZSTR_VAL(execute_data->func->common.scope->name),
magic_function_name
);
} else {
function_name = (char*) determine_function_name(execute_data);
ZEND_PARSE_PARAMETERS_START(0, -1)
Z_PARAM_VARIADIC(' ', argv, argc)
ZEND_PARSE_PARAMETERS_END();
}
record_observed_stack_frame(function_name, SCOUTAPM_G(observer_api_start_time), scoutapm_microtime(), argc, argv);
SCOUTAPM_G(currently_instrumenting) = 0;
SCOUTAPM_G(observer_api_start_time) = 0;
free((void*) function_name);
}
// Note: this is only called FIRST time each function is invoked (better that way)
zend_observer_fcall_handlers scout_observer_api_register(zend_execute_data *execute_data)
{
const char *function_name;
zend_observer_fcall_handlers handlers = {NULL, NULL};
if (execute_data->func == NULL || execute_data->func->common.function_name == NULL) {
return handlers;
}
function_name = determine_function_name(execute_data);
// Can only resolve magic method name at call-time with Observer API, so pass NULL for the magic method name
if (should_be_instrumented(function_name, NULL) == 0) {
free((void*) function_name);
return handlers;
}
free((void*) function_name);
handlers.begin = scout_observer_begin;
handlers.end = scout_observer_end;
return handlers;
}
#endif
void register_scout_observer()
{
#if SCOUTAPM_INSTRUMENT_USING_OBSERVER_API == 1
zend_observer_fcall_register(scout_observer_api_register);
#endif
}