Skip to content

Commit 898065c

Browse files
committed
fix: update mongodb instrumentation with oapi paradigm
1 parent ac8271c commit 898065c

File tree

3 files changed

+230
-1
lines changed

3 files changed

+230
-1
lines changed

agent/lib_mongodb.c

Lines changed: 202 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ void nr_mongodb_get_host_and_port_path_or_id(zval* server,
108108
}
109109
}
110110

111+
#if ZEND_MODULE_API_NO < ZEND_8_0_X_API_NO \
112+
|| defined OVERWRITE_ZEND_EXECUTE_DATA
111113
NR_PHP_WRAPPER(nr_mongodb_operation) {
112114
const char* this_klass = "MongoDB\\Operation\\Executable";
113115
zval* collection = NULL;
@@ -173,7 +175,205 @@ NR_PHP_WRAPPER(nr_mongodb_operation) {
173175
}
174176
NR_PHP_WRAPPER_END
175177

176-
void nr_mongodb_enable(TSRMLS_D) {
178+
#else
179+
180+
NR_PHP_WRAPPER(nr_mongodb_operation_before) {
181+
(void)wraprec;
182+
nr_segment_start(NRPRG(txn), NULL, NULL);
183+
}
184+
NR_PHP_WRAPPER_END
185+
186+
NR_PHP_WRAPPER(nr_mongodb_operation_after) {
187+
const char* this_klass = "MongoDB\\Operation\\Executable";
188+
zval* collection = NULL;
189+
zval* database = NULL;
190+
zval* server = NULL;
191+
zval* this_var = NULL;
192+
nr_segment_t* segment = NULL;
193+
nr_datastore_instance_t instance = {
194+
.host = NULL,
195+
.port_path_or_id = NULL,
196+
.database_name = NULL,
197+
};
198+
nr_segment_datastore_params_t params = {
199+
.datastore = {
200+
.type = NR_DATASTORE_MONGODB,
201+
},
202+
.operation = nr_strdup (wraprec->extra),
203+
.instance = &instance,
204+
.callbacks = {
205+
.backtrace = nr_php_backtrace_callback,
206+
},
207+
};
208+
209+
/*
210+
* We check for the interface all Collection operations extend, rather than
211+
* their specific class. Not all operations have the properties we need but
212+
* the ones we hook do (as of mongo-php-library v.1.1).
213+
*/
214+
this_var = nr_php_scope_get(NR_EXECUTE_ORIG_ARGS);
215+
if (!nr_php_object_instanceof_class(this_var, this_klass)) {
216+
nrl_verbosedebug(NRL_FRAMEWORK, "%s: operation is not %s", __func__,
217+
this_klass);
218+
goto leave;
219+
}
220+
221+
collection
222+
= nr_php_get_zval_object_property(this_var, "collectionName");
223+
if (nr_php_is_zval_valid_string(collection)) {
224+
params.collection = Z_STRVAL_P(collection);
225+
}
226+
227+
database
228+
= nr_php_get_zval_object_property(this_var, "databaseName");
229+
if (nr_php_is_zval_valid_string(database)) {
230+
instance.database_name = Z_STRVAL_P(database);
231+
}
232+
233+
server = nr_php_arg_get(1, NR_EXECUTE_ORIG_ARGS);
234+
nr_mongodb_get_host_and_port_path_or_id(server, &instance.host,
235+
&instance.port_path_or_id);
236+
237+
segment = nr_txn_get_current_segment(NRPRG(txn), NULL);
238+
nr_segment_datastore_end(&segment, &params);
239+
240+
leave:
241+
nr_php_arg_release(&server);
242+
nr_php_scope_release(&this_var);
243+
nr_free(instance.host);
244+
nr_free(instance.port_path_or_id);
245+
nr_free(params.operation);
246+
}
247+
NR_PHP_WRAPPER_END
248+
249+
#endif /* OAPI */
250+
251+
void nr_mongodb_enable() {
252+
#if ZEND_MODULE_API_NO >= ZEND_8_0_X_API_NO \
253+
&& !defined OVERWRITE_ZEND_EXECUTE_DATA
254+
255+
nr_php_wrap_user_function_before_after_clean_extra(
256+
NR_PSTR("MongoDB\\Operation\\Aggregate::execute"),
257+
nr_mongodb_operation_before,
258+
nr_mongodb_operation_after,
259+
NULL,
260+
"aggregate"
261+
);
262+
263+
nr_php_wrap_user_function_before_after_clean_extra(
264+
NR_PSTR("MongoDB\\Operation\\BulkWrite::execute"),
265+
nr_mongodb_operation_before,
266+
nr_mongodb_operation_after,
267+
NULL,
268+
"bulkWrite"
269+
);
270+
271+
nr_php_wrap_user_function_before_after_clean_extra(
272+
NR_PSTR("MongoDB\\Operation\\Count::execute"),
273+
nr_mongodb_operation_before,
274+
nr_mongodb_operation_after,
275+
NULL,
276+
"count"
277+
);
278+
279+
nr_php_wrap_user_function_before_after_clean_extra(
280+
NR_PSTR("MongoDB\\Operation\\CreateIndexes::execute"),
281+
nr_mongodb_operation_before,
282+
nr_mongodb_operation_after,
283+
NULL,
284+
"createIndexes"
285+
);
286+
287+
nr_php_wrap_user_function_before_after_clean_extra(
288+
NR_PSTR("MongoDB\\Operation\\Delete::execute"),
289+
nr_mongodb_operation_before,
290+
nr_mongodb_operation_after,
291+
NULL,
292+
"delete"
293+
);
294+
295+
nr_php_wrap_user_function_before_after_clean_extra(
296+
NR_PSTR("MongoDB\\Operation\\Distinct::execute"),
297+
nr_mongodb_operation_before,
298+
nr_mongodb_operation_after,
299+
NULL,
300+
"distinct"
301+
);
302+
303+
nr_php_wrap_user_function_before_after_clean_extra(
304+
NR_PSTR("MongoDB\\Operation\\DropCollection::execute"),
305+
nr_mongodb_operation_before,
306+
nr_mongodb_operation_after,
307+
NULL,
308+
"dropCollection"
309+
);
310+
311+
nr_php_wrap_user_function_before_after_clean_extra(
312+
NR_PSTR("MongoDB\\Operation\\DropIndexes::execute"),
313+
nr_mongodb_operation_before,
314+
nr_mongodb_operation_after,
315+
NULL,
316+
"dropIndexes"
317+
);
318+
319+
nr_php_wrap_user_function_before_after_clean_extra(
320+
NR_PSTR("MongoDB\\Operation\\Find::execute"),
321+
nr_mongodb_operation_before,
322+
nr_mongodb_operation_after,
323+
NULL,
324+
"find"
325+
);
326+
327+
nr_php_wrap_user_function_before_after_clean_extra(
328+
NR_PSTR("MongoDB\\Operation\\FindAndModify::execute"),
329+
nr_mongodb_operation_before,
330+
nr_mongodb_operation_after,
331+
NULL,
332+
"findAndModify"
333+
);
334+
335+
nr_php_wrap_user_function_before_after_clean_extra(
336+
NR_PSTR("MongoDB\\Operation\\InsertMany::execute"),
337+
nr_mongodb_operation_before,
338+
nr_mongodb_operation_after,
339+
NULL,
340+
"insertMany"
341+
);
342+
343+
nr_php_wrap_user_function_before_after_clean_extra(
344+
NR_PSTR("MongoDB\\Operation\\InsertOne::execute"),
345+
nr_mongodb_operation_before,
346+
nr_mongodb_operation_after,
347+
NULL,
348+
"insertOne"
349+
);
350+
351+
nr_php_wrap_user_function_before_after_clean_extra(
352+
NR_PSTR("MongoDB\\Operation\\ListIndexes::execute"),
353+
nr_mongodb_operation_before,
354+
nr_mongodb_operation_after,
355+
NULL,
356+
"listIndexes"
357+
);
358+
359+
nr_php_wrap_user_function_before_after_clean_extra(
360+
NR_PSTR("MongoDB\\Operation\\Update::execute"),
361+
nr_mongodb_operation_before,
362+
nr_mongodb_operation_after,
363+
NULL,
364+
"update"
365+
);
366+
367+
nr_php_wrap_user_function_before_after_clean_extra(
368+
NR_PSTR("MongoDB\\Operation\\DatabaseCommand::execute"),
369+
nr_mongodb_operation_before,
370+
nr_mongodb_operation_after,
371+
NULL,
372+
"databaseCommand"
373+
);
374+
375+
#else /* Non-OAPI */
376+
177377
/*
178378
* We instrument interesting methods on the MongoDB\Collection class via their
179379
* associated MongoDB\Operation classes.
@@ -269,4 +469,5 @@ void nr_mongodb_enable(TSRMLS_D) {
269469
nr_txn_add_php_package(NRPRG(txn), "mongodb/mongodb",
270470
PHP_PACKAGE_VERSION_UNKNOWN);
271471
}
472+
#endif /* OAPI */
272473
}

agent/php_wrapper.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,26 @@ nruserfn_t* nr_php_wrap_user_function_before_after_clean(
7373
return wraprec;
7474
}
7575

76+
nruserfn_t* nr_php_wrap_user_function_before_after_clean_extra(
77+
const char* name,
78+
size_t namelen,
79+
nrspecialfn_t before_callback,
80+
nrspecialfn_t after_callback,
81+
nrspecialfn_t clean_callback,
82+
const char *extra) {
83+
84+
nruserfn_t* wraprec = nr_php_add_custom_tracer_named(name, namelen);
85+
86+
wraprec->extra = extra;
87+
88+
nr_php_wraprec_add_before_after_clean_callbacks(name, namelen, wraprec,
89+
before_callback,
90+
after_callback,
91+
clean_callback);
92+
93+
return wraprec;
94+
}
95+
7696
nruserfn_t* nr_php_wrap_callable_before_after_clean(
7797
zend_function* callable,
7898
nrspecialfn_t before_callback,

agent/php_wrapper.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@ extern nruserfn_t* nr_php_wrap_callable_before_after_clean(
150150
nrspecialfn_t before_callback,
151151
nrspecialfn_t after_callback,
152152
nrspecialfn_t clean_callback);
153+
154+
extern nruserfn_t* nr_php_wrap_user_function_before_after_clean_extra(
155+
const char* name,
156+
size_t namelen,
157+
nrspecialfn_t before_callback,
158+
nrspecialfn_t after_callback,
159+
nrspecialfn_t clean_callback,
160+
const char* extra);
153161
#endif
154162
extern nruserfn_t* nr_php_wrap_user_function(const char* name,
155163
size_t namelen,

0 commit comments

Comments
 (0)