Skip to content

Commit

Permalink
fix: update mongodb instrumentation with oapi paradigm
Browse files Browse the repository at this point in the history
  • Loading branch information
bduranleau-nr committed Apr 15, 2024
1 parent e0d2d12 commit 5b13b8f
Show file tree
Hide file tree
Showing 3 changed files with 230 additions and 1 deletion.
203 changes: 202 additions & 1 deletion agent/lib_mongodb.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ void nr_mongodb_get_host_and_port_path_or_id(zval* server,
}
}

#if ZEND_MODULE_API_NO < ZEND_8_0_X_API_NO \
|| defined OVERWRITE_ZEND_EXECUTE_DATA
NR_PHP_WRAPPER(nr_mongodb_operation) {
const char* this_klass = "MongoDB\\Operation\\Executable";
zval* collection = NULL;
Expand Down Expand Up @@ -173,7 +175,205 @@ NR_PHP_WRAPPER(nr_mongodb_operation) {
}
NR_PHP_WRAPPER_END

void nr_mongodb_enable(TSRMLS_D) {
#else

NR_PHP_WRAPPER(nr_mongodb_operation_before) {
(void)wraprec;
nr_segment_start(NRPRG(txn), NULL, NULL);
}
NR_PHP_WRAPPER_END

NR_PHP_WRAPPER(nr_mongodb_operation_after) {
const char* this_klass = "MongoDB\\Operation\\Executable";
zval* collection = NULL;
zval* database = NULL;
zval* server = NULL;
zval* this_var = NULL;
nr_segment_t* segment = NULL;
nr_datastore_instance_t instance = {
.host = NULL,
.port_path_or_id = NULL,
.database_name = NULL,
};
nr_segment_datastore_params_t params = {
.datastore = {
.type = NR_DATASTORE_MONGODB,
},
.operation = nr_strdup (wraprec->extra),
.instance = &instance,
.callbacks = {
.backtrace = nr_php_backtrace_callback,
},
};

/*
* We check for the interface all Collection operations extend, rather than
* their specific class. Not all operations have the properties we need but
* the ones we hook do (as of mongo-php-library v.1.1).
*/
this_var = nr_php_scope_get(NR_EXECUTE_ORIG_ARGS);
if (!nr_php_object_instanceof_class(this_var, this_klass)) {
nrl_verbosedebug(NRL_FRAMEWORK, "%s: operation is not %s", __func__,
this_klass);
goto leave;
}

collection
= nr_php_get_zval_object_property(this_var, "collectionName");
if (nr_php_is_zval_valid_string(collection)) {
params.collection = Z_STRVAL_P(collection);
}

database
= nr_php_get_zval_object_property(this_var, "databaseName");
if (nr_php_is_zval_valid_string(database)) {
instance.database_name = Z_STRVAL_P(database);
}

server = nr_php_arg_get(1, NR_EXECUTE_ORIG_ARGS);
nr_mongodb_get_host_and_port_path_or_id(server, &instance.host,
&instance.port_path_or_id);

segment = nr_txn_get_current_segment(NRPRG(txn), NULL);
nr_segment_datastore_end(&segment, &params);

leave:
nr_php_arg_release(&server);
nr_php_scope_release(&this_var);
nr_free(instance.host);
nr_free(instance.port_path_or_id);
nr_free(params.operation);
}
NR_PHP_WRAPPER_END

#endif /* OAPI */

void nr_mongodb_enable() {
#if ZEND_MODULE_API_NO >= ZEND_8_0_X_API_NO \
&& !defined OVERWRITE_ZEND_EXECUTE_DATA

nr_php_wrap_user_function_before_after_clean_extra(
NR_PSTR("MongoDB\\Operation\\Aggregate::execute"),
nr_mongodb_operation_before,
nr_mongodb_operation_after,
NULL,
"aggregate"
);

nr_php_wrap_user_function_before_after_clean_extra(
NR_PSTR("MongoDB\\Operation\\BulkWrite::execute"),
nr_mongodb_operation_before,
nr_mongodb_operation_after,
NULL,
"bulkWrite"
);

nr_php_wrap_user_function_before_after_clean_extra(
NR_PSTR("MongoDB\\Operation\\Count::execute"),
nr_mongodb_operation_before,
nr_mongodb_operation_after,
NULL,
"count"
);

nr_php_wrap_user_function_before_after_clean_extra(
NR_PSTR("MongoDB\\Operation\\CreateIndexes::execute"),
nr_mongodb_operation_before,
nr_mongodb_operation_after,
NULL,
"createIndexes"
);

nr_php_wrap_user_function_before_after_clean_extra(
NR_PSTR("MongoDB\\Operation\\Delete::execute"),
nr_mongodb_operation_before,
nr_mongodb_operation_after,
NULL,
"delete"
);

nr_php_wrap_user_function_before_after_clean_extra(
NR_PSTR("MongoDB\\Operation\\Distinct::execute"),
nr_mongodb_operation_before,
nr_mongodb_operation_after,
NULL,
"distinct"
);

nr_php_wrap_user_function_before_after_clean_extra(
NR_PSTR("MongoDB\\Operation\\DropCollection::execute"),
nr_mongodb_operation_before,
nr_mongodb_operation_after,
NULL,
"dropCollection"
);

nr_php_wrap_user_function_before_after_clean_extra(
NR_PSTR("MongoDB\\Operation\\DropIndexes::execute"),
nr_mongodb_operation_before,
nr_mongodb_operation_after,
NULL,
"dropIndexes"
);

nr_php_wrap_user_function_before_after_clean_extra(
NR_PSTR("MongoDB\\Operation\\Find::execute"),
nr_mongodb_operation_before,
nr_mongodb_operation_after,
NULL,
"find"
);

nr_php_wrap_user_function_before_after_clean_extra(
NR_PSTR("MongoDB\\Operation\\FindAndModify::execute"),
nr_mongodb_operation_before,
nr_mongodb_operation_after,
NULL,
"findAndModify"
);

nr_php_wrap_user_function_before_after_clean_extra(
NR_PSTR("MongoDB\\Operation\\InsertMany::execute"),
nr_mongodb_operation_before,
nr_mongodb_operation_after,
NULL,
"insertMany"
);

nr_php_wrap_user_function_before_after_clean_extra(
NR_PSTR("MongoDB\\Operation\\InsertOne::execute"),
nr_mongodb_operation_before,
nr_mongodb_operation_after,
NULL,
"insertOne"
);

nr_php_wrap_user_function_before_after_clean_extra(
NR_PSTR("MongoDB\\Operation\\ListIndexes::execute"),
nr_mongodb_operation_before,
nr_mongodb_operation_after,
NULL,
"listIndexes"
);

nr_php_wrap_user_function_before_after_clean_extra(
NR_PSTR("MongoDB\\Operation\\Update::execute"),
nr_mongodb_operation_before,
nr_mongodb_operation_after,
NULL,
"update"
);

nr_php_wrap_user_function_before_after_clean_extra(
NR_PSTR("MongoDB\\Operation\\DatabaseCommand::execute"),
nr_mongodb_operation_before,
nr_mongodb_operation_after,
NULL,
"databaseCommand"
);

#else /* Non-OAPI */

/*
* We instrument interesting methods on the MongoDB\Collection class via their
* associated MongoDB\Operation classes.
Expand Down Expand Up @@ -269,4 +469,5 @@ void nr_mongodb_enable(TSRMLS_D) {
nr_txn_add_php_package(NRPRG(txn), "mongodb/mongodb",
PHP_PACKAGE_VERSION_UNKNOWN);
}
#endif /* OAPI */
}
20 changes: 20 additions & 0 deletions agent/php_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,26 @@ nruserfn_t* nr_php_wrap_user_function_before_after_clean(
return wraprec;
}

nruserfn_t* nr_php_wrap_user_function_before_after_clean_extra(
const char* name,
size_t namelen,
nrspecialfn_t before_callback,
nrspecialfn_t after_callback,
nrspecialfn_t clean_callback,
const char *extra) {

nruserfn_t* wraprec = nr_php_add_custom_tracer_named(name, namelen);

wraprec->extra = extra;

nr_php_wraprec_add_before_after_clean_callbacks(name, namelen, wraprec,
before_callback,
after_callback,
clean_callback);

return wraprec;
}

nruserfn_t* nr_php_wrap_callable_before_after_clean(
zend_function* callable,
nrspecialfn_t before_callback,
Expand Down
8 changes: 8 additions & 0 deletions agent/php_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ extern nruserfn_t* nr_php_wrap_callable_before_after_clean(
nrspecialfn_t before_callback,
nrspecialfn_t after_callback,
nrspecialfn_t clean_callback);

extern nruserfn_t* nr_php_wrap_user_function_before_after_clean_extra(
const char* name,
size_t namelen,
nrspecialfn_t before_callback,
nrspecialfn_t after_callback,
nrspecialfn_t clean_callback,
const char* extra);
#endif
extern nruserfn_t* nr_php_wrap_user_function(const char* name,
size_t namelen,
Expand Down

0 comments on commit 5b13b8f

Please sign in to comment.