Skip to content

Commit

Permalink
Merge pull request #925 from newrelic/dev
Browse files Browse the repository at this point in the history
Release 10.22
  • Loading branch information
hahuja2 authored Jun 21, 2024
2 parents ddc5230 + 5154931 commit 687e42d
Show file tree
Hide file tree
Showing 21 changed files with 361 additions and 264 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
10.21.0
10.22.0
4 changes: 3 additions & 1 deletion agent/fw_drupal.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,9 @@ NR_PHP_WRAPPER(nr_drupal_http_request_before) {
* fcall_end is able to properly dispatch to the after wrapper, as
* this new segment is now at the top of the segment stack.
*/
NRPRG(drupal_http_request_segment)->wraprec = auto_segment->wraprec;
if (NULL != NRPRG(drupal_http_request_segment)) {
NRPRG(drupal_http_request_segment)->wraprec = auto_segment->wraprec;
}
}
}
NR_PHP_WRAPPER_END
Expand Down
77 changes: 68 additions & 9 deletions agent/fw_slim.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,28 +81,65 @@ NR_PHP_WRAPPER(nr_slim2_route_dispatch) {
NR_PHP_WRAPPER_END

/*
* Wrap the \Slim\Route::run method, which is the happy path for Slim routing.
* Wrap the Slim 3\Slim\Route::run method
* and
* Slim 4 Slim\\Routing\\Route::run
* which are the happy paths for Slim 3/4 routing.
* i.e. The router has succesfully matched the URL and dispatched the request
* to a route.
*
* In this case, `nr_txn_set_path` is called after `NR_PHP_WRAPPER_CALL` with
* `NR_OK_TO_OVERWRITE` and as this corresponds to calling the wrapped function
* in func_end no change is needed to ensure OAPI compatibility as it will use
* the default func_end after callback. This entails that the first wrapped
* function call of this type gets to name the txn.
* In this case, `nr_txn_set_path` is called before `NR_PHP_WRAPPER_CALL` with
* `NR_OK_TO_OVERWRITE` and as this corresponds to calling the last wrapped
* function call of this type gets to name the txn; therefore needs a before
* call for OAPI.
*/
NR_PHP_WRAPPER(nr_slim3_4_route_run) {
zval* this_var = NULL;
char* txn_name = NULL;

(void)wraprec;

NR_PHP_WRAPPER_REQUIRE_FRAMEWORK(NR_FW_SLIM);

this_var = nr_php_scope_get(NR_EXECUTE_ORIG_ARGS TSRMLS_CC);
txn_name = nr_slim_path_from_route(this_var TSRMLS_CC);
nr_php_scope_release(&this_var);

if (txn_name) {
nr_txn_set_path("Slim", NRPRG(txn), txn_name, NR_PATH_TYPE_ACTION,
NR_OK_TO_OVERWRITE);
nr_free(txn_name);
}

NR_PHP_WRAPPER_CALL;
}
NR_PHP_WRAPPER_END

/*
* public function dispatch(string $method, string $uri): RoutingResults
* This is fallback naming mechanism for Slim 4 routing when the Slim 4
* Slim\\Routing\\Route::run does not run due middlware intervening on
* certain errors.
* In this case, `nr_txn_set_path` is called before `NR_PHP_WRAPPER_CALL` with
* `NR_NOT_OK_TO_OVERWRITE` and as this corresponds to calling the first wrapped
* function in func_begin.
*/
NR_PHP_WRAPPER(nr_slim4_route_dispatch) {
char* txn_name = NULL;
zval* route_name = NULL;

(void)wraprec;

NR_PHP_WRAPPER_REQUIRE_FRAMEWORK(NR_FW_SLIM);

/* Get the route name. The first arg is the method, 2nd arg is routename. */
route_name = nr_php_arg_get(2, NR_EXECUTE_ORIG_ARGS);

if (nr_php_is_zval_valid_string(route_name)) {
txn_name = nr_strndup(Z_STRVAL_P(route_name), Z_STRLEN_P(route_name));
}

nr_php_arg_release(&route_name);

if (txn_name) {
nr_txn_set_path("Slim", NRPRG(txn), txn_name, NR_PATH_TYPE_ACTION,
Expand All @@ -120,7 +157,7 @@ NR_PHP_WRAPPER(nr_slim_application_construct) {
(void)wraprec;

version = nr_php_get_object_constant(this_var, "VERSION");

if (NRINI(vulnerability_management_package_detection_enabled)) {
// Add php package to transaction
nr_txn_add_php_package(NRPRG(txn), PHP_PACKAGE_NAME, version);
Expand All @@ -139,12 +176,34 @@ void nr_slim_enable(TSRMLS_D) {

nr_php_wrap_user_function(NR_PSTR("Slim\\Route::dispatch"),
nr_slim2_route_dispatch TSRMLS_CC);

#if ZEND_MODULE_API_NO >= ZEND_8_0_X_API_NO \
&& !defined OVERWRITE_ZEND_EXECUTE_DATA

/* Slim 3 */
nr_php_wrap_user_function(NR_PSTR("Slim\\Route::run"),
nr_slim3_4_route_run TSRMLS_CC);
nr_php_wrap_user_function_before_after_clean(
NR_PSTR("Slim\\Route::run"), nr_slim3_4_route_run, NULL, NULL);

/* Slim 4 */
nr_php_wrap_user_function_before_after_clean(
NR_PSTR("Slim\\Routing\\Route::run"), nr_slim3_4_route_run, NULL, NULL);

/* Slim 4 */
nr_php_wrap_user_function_before_after_clean(
NR_PSTR("Slim\\Routing\\Dispatcher::dispatch"), nr_slim4_route_dispatch,
NULL, NULL);
#else
/* Slim 4*/
nr_php_wrap_user_function(NR_PSTR("Slim\\Routing\\Route::run"),
nr_slim3_4_route_run TSRMLS_CC);
/* Slim 4 */
nr_php_wrap_user_function(NR_PSTR("Slim\\Routing\\Dispatcher::dispatch"),
nr_slim4_route_dispatch TSRMLS_CC);

/* Slim 3 */
nr_php_wrap_user_function(NR_PSTR("Slim\\Route::run"),
nr_slim3_4_route_run TSRMLS_CC);
#endif

/* Slim 2 does not have the same path as Slim 3/4 which is why
we need to separate these*/
Expand Down
12 changes: 9 additions & 3 deletions agent/lib_guzzle6.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,14 +352,20 @@ NR_PHP_WRAPPER_START(nr_guzzle6_client_construct) {
zval* this_var = nr_php_scope_get(NR_EXECUTE_ORIG_ARGS);

char* version = nr_php_get_object_constant(this_var, "VERSION");
if (NULL == version) {
version = nr_php_get_object_constant(this_var, "MAJOR_VERSION");
}

if (NRINI(vulnerability_management_package_detection_enabled)) {
// Add php package to transaction
nr_txn_add_php_package(NRPRG(txn), PHP_PACKAGE_NAME, version);
}

/*
* If we were unable to get the full version before, at least we can extract
* the major version to send to the supportability metric.
* This is relevant to guzzle7+ which no longer supplies full version.
*/
if (NULL == version) {
version = nr_php_get_object_constant(this_var, "MAJOR_VERSION");
}
nr_fw_support_add_package_supportability_metric(NRPRG(txn), PHP_PACKAGE_NAME,
version);
nr_free(version);
Expand Down
173 changes: 172 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,174 @@ 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_t* segment = NULL;
segment = nr_segment_start(NRPRG(txn), NULL, NULL);
if (NULL != segment) {
segment->wraprec = auto_segment->wraprec;
}
}
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;
bool discard_segment = false;
nr_datastore_instance_t instance = {
.host = NULL,
.port_path_or_id = NULL,
.database_name = NULL,
};

// tell the compiler to ignore the cast from const char * to char *
// to save having to do a strdup operation
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-qual"
nr_segment_datastore_params_t params = {
.collection = NULL,
.datastore = {
.type = NR_DATASTORE_MONGODB,
},
.operation = (char *)wraprec->extra,
.instance = &instance,
.callbacks = {
.backtrace = nr_php_backtrace_callback,
},
};
#pragma GCC diagnostic pop
/*
* 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);
discard_segment = true;
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);

leave:
if (discard_segment) {
nr_segment_discard(&auto_segment);
} else {
nr_segment_datastore_end(&auto_segment, &params);
}
nr_php_arg_release(&server);
nr_php_scope_release(&this_var);
nr_free(instance.host);
nr_free(instance.port_path_or_id);
}
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,
nr_mongodb_operation_after, "aggregate");

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

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

nr_php_wrap_user_function_before_after_clean_extra(
NR_PSTR("MongoDB\\Operation\\CountDocuments::execute"),
nr_mongodb_operation_before, nr_mongodb_operation_after,
nr_mongodb_operation_after, "countDocuments");

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

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

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

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

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

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

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

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

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

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

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

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

#else /* Non-OAPI */

/*
* We instrument interesting methods on the MongoDB\Collection class via their
* associated MongoDB\Operation classes.
Expand Down Expand Up @@ -265,6 +434,8 @@ void nr_mongodb_enable(TSRMLS_D) {
NR_PSTR("MongoDB\\Operation\\DatabaseCommand::execute"),
nr_mongodb_operation, "databaseCommand" TSRMLS_CC);

#endif /* OAPI */

if (NRINI(vulnerability_management_package_detection_enabled)) {
nr_txn_add_php_package(NRPRG(txn), "mongodb/mongodb",
PHP_PACKAGE_VERSION_UNKNOWN);
Expand Down
5 changes: 3 additions & 2 deletions agent/lib_predis.c
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,9 @@ NR_PHP_WRAPPER(nr_predis_webdisconnection_executeCommand_before) {

nr_segment_t* segment = NULL;
segment = nr_segment_start(NRPRG(txn), NULL, NULL);
segment->wraprec = auto_segment->wraprec;
if (NULL != segment) {
segment->wraprec = auto_segment->wraprec;
}
}
NR_PHP_WRAPPER_END

Expand Down Expand Up @@ -894,5 +896,4 @@ void nr_predis_enable(TSRMLS_D) {
NR_PSTR("Predis\\Connection\\WebdisConnection::executeCommand"),
nr_predis_webdisconnection_executeCommand TSRMLS_CC);
#endif /* OAPI */

}
Loading

0 comments on commit 687e42d

Please sign in to comment.