-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathelastic_apm_API.cpp
More file actions
263 lines (210 loc) · 9.87 KB
/
Copy pathelastic_apm_API.cpp
File metadata and controls
263 lines (210 loc) · 9.87 KB
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
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include "elastic_apm_API.h"
#include <unistd.h>
#include <php.h>
#include "util.h"
#include "log.h"
#include "Tracer.h"
#include "elastic_apm_alloc.h"
#include "numbered_intercepting_callbacks.h"
#include "tracer_PHP_part.h"
#include "backend_comm.h"
#include "lifecycle.h"
#include "ConfigSnapshot.h"
#define ELASTIC_APM_CURRENT_LOG_CATEGORY ELASTIC_APM_LOG_CATEGORY_EXT_API
ResultCode elasticApmApiEntered( String dbgCalledFromFile, int dbgCalledFromLine, String dbgCalledFromFunction )
{
// We SHOULD NOT log before resetting state if forked because logging might be using thread synchronization
// which might deadlock in forked child
return elasticApmEnterAgentCode( dbgCalledFromFile, dbgCalledFromLine, dbgCalledFromFunction );
}
bool elasticApmIsEnabled()
{
const bool result = getTracerCurrentConfigSnapshot( getGlobalTracer() )->enabled;
ELASTIC_APM_LOG_TRACE( "Result: %s", boolToString( result ) );
return result;
}
ResultCode elasticApmGetConfigOption( String optionName, zval* return_value )
{
ELASTIC_APM_LOG_TRACE_FUNCTION_ENTRY_MSG( "optionName: `%s'", optionName );
char txtOutStreamBuf[ELASTIC_APM_TEXT_OUTPUT_STREAM_ON_STACK_BUFFER_SIZE];
GetConfigManagerOptionValueByNameResult getOptValueByNameRes;
getOptValueByNameRes.txtOutStream = ELASTIC_APM_TEXT_OUTPUT_STREAM_FROM_STATIC_BUFFER( txtOutStreamBuf );
getOptValueByNameRes.streamedParsedValue = NULL;
const ResultCode resultCode = getConfigManagerOptionValueByName(
getGlobalTracer()->configManager
, optionName
, &getOptValueByNameRes );
if ( resultCode == resultSuccess ) *return_value = getOptValueByNameRes.parsedValueAsZval;
ELASTIC_APM_LOG_TRACE_RESULT_CODE_FUNCTION_EXIT_MSG( "Option's: name: `%s', value: %s", optionName, getOptValueByNameRes.streamedParsedValue );
return resultCode;
}
UInt elasticApmGetNumberOfDynamicConfigOptions()
{
ELASTIC_APM_LOG_TRACE_FUNCTION_ENTRY();
const ConfigManager* const cfgManager = getGlobalTracer()->configManager;
UInt result = 0;
ELASTIC_APM_FOR_EACH_OPTION_ID( optId )
{
GetConfigManagerOptionMetadataResult getMetaRes;
getConfigManagerOptionMetadata( cfgManager, optId, &getMetaRes );
if ( getMetaRes.isDynamic ) ++result;
}
ELASTIC_APM_LOG_TRACE_FUNCTION_EXIT_MSG( "result: %d", result );
return result;
}
enum { maxFunctionsToIntercept = numberedInterceptingCallbacksCount };
static uint32_t g_nextFreeFunctionToInterceptId = 0;
struct CallToInterceptData
{
zif_handler originalHandler;
zend_function* funcEntry;
};
typedef struct CallToInterceptData CallToInterceptData;
static CallToInterceptData g_functionsToInterceptData[maxFunctionsToIntercept];
static uint32_t g_interceptedCallInProgressRegistrationId = 0;
static
void internalFunctionCallInterceptingImpl( uint32_t interceptRegistrationId, zend_execute_data* execute_data, zval* return_value )
{
ResultCode resultCode;
// We SHOULD NOT log before resetting state if forked because logging might be using thread synchronization
// which might deadlock in forked child
ELASTIC_APM_CALL_IF_FAILED_GOTO( elasticApmEnterAgentCode( __FILE__, __LINE__, __FUNCTION__ ) );
ELASTIC_APM_LOG_TRACE_FUNCTION_ENTRY_MSG( "interceptRegistrationId: %u", interceptRegistrationId );
bool shouldCallPostHook;
if ( g_interceptedCallInProgressRegistrationId != 0 )
{
ELASTIC_APM_LOG_TRACE_FUNCTION_ENTRY_MSG(
"There's already an intercepted call in progress with interceptRegistrationId: %u."
"Nesting intercepted calls is not supported yet so invoking the original handler directly..."
, g_interceptedCallInProgressRegistrationId );
g_functionsToInterceptData[ interceptRegistrationId ].originalHandler( execute_data, return_value );
return;
}
g_interceptedCallInProgressRegistrationId = interceptRegistrationId;
shouldCallPostHook = tracerPhpPartInternalFuncCallPreHook( interceptRegistrationId, execute_data );
g_functionsToInterceptData[ interceptRegistrationId ].originalHandler( execute_data, return_value );
if ( shouldCallPostHook ) {
tracerPhpPartInternalFuncCallPostHook( interceptRegistrationId, return_value );
}
g_interceptedCallInProgressRegistrationId = 0;
ELASTIC_APM_LOG_TRACE_FUNCTION_EXIT_MSG( "interceptRegistrationId: %u", interceptRegistrationId );
resultCode = resultSuccess;
finally:
return;
failure:
goto finally;
}
void resetCallInterceptionOnRequestShutdown()
{
// We restore original handlers in the reverse order
// so that if the same function is registered for interception more than once
// the original handler will be restored correctly
ELASTIC_APM_FOR_EACH_BACKWARDS( i, g_nextFreeFunctionToInterceptId )
{
CallToInterceptData* data = &( g_functionsToInterceptData[ i ] );
data->funcEntry->internal_function.handler = data->originalHandler;
}
g_nextFreeFunctionToInterceptId = 0;
}
bool addToFunctionsToInterceptData( zend_function* funcEntry, uint32_t* interceptRegistrationId, zif_handler replacementFunc )
{
if ( g_nextFreeFunctionToInterceptId >= maxFunctionsToIntercept )
{
ELASTIC_APM_LOG_ERROR( "Reached maxFunctionsToIntercept."
" maxFunctionsToIntercept: %u. g_nextFreeFunctionToInterceptId: %u."
, maxFunctionsToIntercept, g_nextFreeFunctionToInterceptId );
return false;
}
*interceptRegistrationId = g_nextFreeFunctionToInterceptId ++;
g_functionsToInterceptData[ *interceptRegistrationId ].funcEntry = funcEntry;
g_functionsToInterceptData[ *interceptRegistrationId ].originalHandler = funcEntry->internal_function.handler;
funcEntry->internal_function.handler = ( replacementFunc == NULL ) ? g_numberedInterceptingCallback[ *interceptRegistrationId ] : replacementFunc;
return true;
}
ResultCode elasticApmInterceptCallsToInternalMethod( String className, String methodName, uint32_t* interceptRegistrationId )
{
ELASTIC_APM_LOG_DEBUG_FUNCTION_ENTRY_MSG( "className: `%s'; methodName: `%s'", className, methodName );
ResultCode resultCode;
zend_function *funcEntry;
auto classEntry = static_cast<zend_class_entry *>(zend_hash_str_find_ptr(CG(class_table), className, strlen(className)));
if (!classEntry) {
ELASTIC_APM_LOG_ERROR( "zend_hash_str_find_ptr( CG( class_table ), ... ) failed. className: `%s'", className );
ELASTIC_APM_SET_RESULT_CODE_AND_GOTO_FAILURE();
}
funcEntry = static_cast<zend_function *>(zend_hash_str_find_ptr(&classEntry->function_table, methodName, strlen(methodName)));
if (!funcEntry) {
ELASTIC_APM_LOG_ERROR( "zend_hash_str_find_ptr( &classEntry->function_table, ... ) failed."
" className: `%s'; methodName: `%s'", className, methodName );
ELASTIC_APM_SET_RESULT_CODE_AND_GOTO_FAILURE();
}
if ( ! addToFunctionsToInterceptData( funcEntry, interceptRegistrationId, /* replacementFunc */ NULL) )
{
ELASTIC_APM_SET_RESULT_CODE_AND_GOTO_FAILURE();
}
resultCode = resultSuccess;
finally:
ELASTIC_APM_LOG_DEBUG_RESULT_CODE_FUNCTION_EXIT_MSG( "interceptRegistrationId: %u", *interceptRegistrationId );
return resultCode;
failure:
goto finally;
}
ResultCode elasticApmInterceptCallsToInternalFunctionEx( String functionName, uint32_t* interceptRegistrationId, zif_handler replacementFunc )
{
ELASTIC_APM_LOG_DEBUG_FUNCTION_ENTRY_MSG( "functionName: `%s'", functionName );
ResultCode resultCode;
auto funcEntry = static_cast<zend_function *>(zend_hash_str_find_ptr(CG(function_table), functionName, strlen(functionName)));
if (!funcEntry) {
ELASTIC_APM_LOG_ERROR( "zend_hash_str_find_ptr( CG( function_table ), ... ) failed."
" functionName: `%s'", functionName );
ELASTIC_APM_SET_RESULT_CODE_AND_GOTO_FAILURE();
}
if ( ! addToFunctionsToInterceptData( funcEntry, interceptRegistrationId, replacementFunc ) )
{
ELASTIC_APM_SET_RESULT_CODE_AND_GOTO_FAILURE();
}
resultCode = resultSuccess;
finally:
ELASTIC_APM_LOG_DEBUG_RESULT_CODE_FUNCTION_EXIT_MSG( "interceptRegistrationId: %u", *interceptRegistrationId );
return resultCode;
failure:
goto finally;
}
ResultCode elasticApmInterceptCallsToInternalFunction( String functionName, uint32_t* interceptRegistrationId)
{
return elasticApmInterceptCallsToInternalFunctionEx( functionName, interceptRegistrationId, /* replacementFunc */ NULL );
}
static inline bool longToBool( long longVal )
{
return longVal != 0;
}
ResultCode elasticApmSendToServer( StringView userAgentHttpHeader, StringView serializedEvents )
{
ELASTIC_APM_LOG_DEBUG_FUNCTION_ENTRY();
ResultCode resultCode;
Tracer* const tracer = getGlobalTracer();
ELASTIC_APM_CALL_IF_FAILED_GOTO( sendEventsToApmServer( getTracerCurrentConfigSnapshot( tracer ), userAgentHttpHeader, serializedEvents ) );
resultCode = resultSuccess;
finally:
ELASTIC_APM_LOG_DEBUG_RESULT_CODE_FUNCTION_EXIT();
return resultCode;
failure:
goto finally;
}