Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(agent): Utilize OAPI to automatically dispatch to wrappers #886

Draft
wants to merge 23 commits into
base: zjn/oapi/remove-clean
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Build artifacts
agent/configure.ac
agent/newrelic.map
agent/configure.ac
agent/*.dep
axiom/tests/*.tmp
bin/
Expand All @@ -13,3 +13,4 @@ php_agent.log

# Dev artifacts
.vscode
*.log
4 changes: 4 additions & 0 deletions agent/fw_drupal.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,11 @@ NR_PHP_WRAPPER(nr_drupal_http_request_before) {
* this new segment is now at the top of the segment stack.
*/
if (NULL != NRPRG(drupal_http_request_segment)) {
#if ZEND_MODULE_API_NO < ZEND_8_2_X_API_NO
NRPRG(drupal_http_request_segment)->wraprec = auto_segment->wraprec;
#else
NRPRG(drupal_http_request_segment)->execute_data = auto_segment->execute_data;
#endif
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions agent/fw_drupal_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ NR_PHP_WRAPPER(nr_drupal_wrap_module_hook) {
* function such as a_b_c is ambiguous (is the module a or a_b?). Instead,
* we'll see if they're defined in the wraprec.
*/
#if ZEND_MODULE_API_NO >= ZEND_8_2_X_API_NO
wraprec = nr_php_get_wraprec(execute_data->func);
#endif
if ((NULL != wraprec->drupal_hook) && (NULL != wraprec->drupal_module)) {
nr_drupal_create_metric(auto_segment, NR_PSTR(NR_DRUPAL_MODULE_PREFIX),
wraprec->drupal_module, wraprec->drupal_module_len);
Expand Down
70 changes: 68 additions & 2 deletions agent/fw_wordpress.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,60 @@ static char* nr_wordpress_plugin_from_function(zend_function* func TSRMLS_DC) {
return plugin;
}

#if ZEND_MODULE_API_NO >= ZEND_8_2_X_API_NO
NR_PHP_WRAPPER(nr_wordpress_wrap_hook_core) {
NR_UNUSED_SPECIALFN;
(void)wraprec;

/*
* We only want to hook the function being called if this is a WordPress
* function, we're instrumenting hooks, and WordPress is currently executing
* hooks (denoted by the wordpress_tag being set).
*/
NR_PHP_WRAPPER_REQUIRE_FRAMEWORK(NR_FW_WORDPRESS);

char* tag = nr_stack_get_top(&NRPRG(wordpress_tags));
if ((0 == NRINI(wordpress_hooks)) || (NULL == tag)) {
NR_PHP_WRAPPER_LEAVE;
}

if (NRPRG(wordpress_core)) {
nr_wordpress_create_metric(auto_segment, NR_WORDPRESS_HOOK_PREFIX, tag);
}
}
NR_PHP_WRAPPER_END

NR_PHP_WRAPPER(nr_wordpress_wrap_hook_plugin) {
char* plugin = NULL;

NR_UNUSED_SPECIALFN;

/*
* We only want to hook the function being called if this is a WordPress
* function, we're instrumenting hooks, and WordPress is currently executing
* hooks (denoted by the wordpress_tag being set).
*/
NR_PHP_WRAPPER_REQUIRE_FRAMEWORK(NR_FW_WORDPRESS);

char* tag = nr_stack_get_top(&NRPRG(wordpress_tags));
if ((0 == NRINI(wordpress_hooks)) || (NULL == tag)) {
NR_PHP_WRAPPER_LEAVE;
}
// Use optimized wraprec hashmap over plugin hashmap
wraprec = nr_php_get_wraprec(execute_data->func);
plugin = wraprec->wordpress_plugin_theme;
//plugin = nr_wordpress_plugin_from_function(execute_data->func);

if (NULL != plugin) {
nr_wordpress_create_metric(auto_segment, NR_WORDPRESS_HOOK_PREFIX, tag);
nr_wordpress_create_metric(auto_segment, NR_WORDPRESS_PLUGIN_PREFIX,
plugin);
}
}
NR_PHP_WRAPPER_END
#endif

#if ZEND_MODULE_API_NO < ZEND_8_2_X_API_NO
NR_PHP_WRAPPER(nr_wordpress_wrap_hook) {
#if ZEND_MODULE_API_NO < ZEND_7_4_X_API_NO
zend_function* func = NULL;
Expand Down Expand Up @@ -375,6 +429,7 @@ NR_PHP_WRAPPER(nr_wordpress_wrap_hook) {
}
}
NR_PHP_WRAPPER_END
#endif // <PHP 8.2

#if ZEND_MODULE_API_NO < ZEND_8_0_X_API_NO \
|| defined OVERWRITE_ZEND_EXECUTE_DATA
Expand Down Expand Up @@ -685,13 +740,16 @@ NR_PHP_WRAPPER_END
NR_PHP_WRAPPER(nr_wordpress_apply_filters_after) {
/* using nr_php_get_user_func_arg() so that we don't perform another copy
* when all we want to do is check the string length */
(void)wraprec;
zval* tag = nr_php_get_user_func_arg(1, NR_EXECUTE_ORIG_ARGS TSRMLS_CC);
if (1 == nr_php_is_zval_non_empty_string(tag)) {
zval** retval_ptr = NR_GET_RETURN_VALUE_PTR;
nr_wordpress_name_the_wt(tag, retval_ptr TSRMLS_CC);
}

nr_wordpress_handle_tag_stack_after(NR_SPECIALFNPTR_ORIG_ARGS);
if (0 != NRINI(wordpress_hooks)) {
clean_wordpress_tag_stack(auto_segment);
}
}
NR_PHP_WRAPPER_END
#endif /* OAPI */
Expand Down Expand Up @@ -751,7 +809,15 @@ NR_PHP_WRAPPER(nr_wordpress_add_filter) {
if (NULL != zf) {
char* wordpress_plugin_theme = nr_wordpress_plugin_from_function(zf);
if (NULL != wordpress_plugin_theme || NRPRG(wordpress_core)) {
#if ZEND_MODULE_API_NO >= ZEND_8_0_X_API_NO \
#if ZEND_MODULE_API_NO >= ZEND_8_2_X_API_NO
if (NULL != wordpress_plugin_theme) {
callback_wraprec = nr_php_wrap_callable_before_after(
zf, NULL, nr_wordpress_wrap_hook_plugin);
} else {
callback_wraprec = nr_php_wrap_callable_before_after(
zf, NULL, nr_wordpress_wrap_hook_core);
}
#elif ZEND_MODULE_API_NO >= ZEND_8_0_X_API_NO \
&& !defined OVERWRITE_ZEND_EXECUTE_DATA
callback_wraprec = nr_php_wrap_callable_before_after(
zf, NULL, nr_wordpress_wrap_hook);
Expand Down
13 changes: 13 additions & 0 deletions agent/lib_guzzle4.c
Original file line number Diff line number Diff line change
Expand Up @@ -436,22 +436,33 @@ const zend_function_entry nr_guzzle4_subscriber_functions[]
* Purpose : Registers an event subscriber for a newly instantiated
* GuzzleHttp\Client object.
*/

#if ZEND_MODULE_API_NO >= ZEND_8_2_X_API_NO
void nr_guzzle4_client_construct(NR_EXECUTE_PROTO) {
#else
NR_PHP_WRAPPER_START(nr_guzzle4_client_construct) {
#endif
zval* emitter = NULL;
zval* retval = NULL;
zval* subscriber = NULL;
zval* this_var = nr_php_scope_get(NR_EXECUTE_ORIG_ARGS TSRMLS_CC);

#if ZEND_MODULE_API_NO < ZEND_8_2_X_API_NO
(void)wraprec;
#endif
NR_UNUSED_SPECIALFN;

/* This is how we distinguish Guzzle 4/5 from other versions. */
if (0 == nr_guzzle_does_zval_implement_has_emitter(this_var TSRMLS_CC)) {
#if ZEND_MODULE_API_NO < ZEND_8_2_X_API_NO
NR_PHP_WRAPPER_CALL;
#endif
goto end;
}

#if ZEND_MODULE_API_NO < ZEND_8_2_X_API_NO
NR_PHP_WRAPPER_CALL;
#endif

/*
* We can't have newrelic\Guzzle4\Subscriber implement
Expand Down Expand Up @@ -503,7 +514,9 @@ NR_PHP_WRAPPER_START(nr_guzzle4_client_construct) {
nr_php_zval_free(&emitter);
nr_php_zval_free(&subscriber);
}
#if ZEND_MODULE_API_NO < ZEND_8_2_X_API_NO
NR_PHP_WRAPPER_END
#endif

void nr_guzzle4_enable(TSRMLS_D) {
if (0 == NRINI(guzzle_enabled)) {
Expand Down
4 changes: 4 additions & 0 deletions agent/lib_guzzle4.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ extern void nr_guzzle4_rshutdown(TSRMLS_D);
/*
* Purpose : Client::__construct() wrapper for Guzzle 4.
*/
#if ZEND_MODULE_API_NO < ZEND_8_2_X_API_NO
extern NR_PHP_WRAPPER_PROTOTYPE(nr_guzzle4_client_construct);
#else
extern void nr_guzzle4_client_construct(NR_EXECUTE_PROTO);
#endif

#endif /* LIB_GUZZLE4_HDR */
12 changes: 12 additions & 0 deletions agent/lib_guzzle6.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,11 @@ const zend_function_entry nr_guzzle6_requesthandler_functions[]

/* }}} */

#if ZEND_MODULE_API_NO >= ZEND_8_2_X_API_NO
void nr_guzzle6_client_construct(NR_EXECUTE_PROTO) {
#else
NR_PHP_WRAPPER_START(nr_guzzle6_client_construct) {
#endif
zval* config;
zend_class_entry* guzzle_client_ce;
zval* handler_stack;
Expand All @@ -370,16 +374,22 @@ NR_PHP_WRAPPER_START(nr_guzzle6_client_construct) {
version);
nr_free(version);

#if ZEND_MODULE_API_NO < ZEND_8_2_X_API_NO
(void)wraprec;
#endif
NR_UNUSED_SPECIALFN;

/* This is how we distinguish Guzzle 4/5. */
if (nr_guzzle_does_zval_implement_has_emitter(this_var TSRMLS_CC)) {
#if ZEND_MODULE_API_NO < ZEND_8_2_X_API_NO
NR_PHP_WRAPPER_CALL;
#endif
goto end;
}

#if ZEND_MODULE_API_NO < ZEND_8_2_X_API_NO
NR_PHP_WRAPPER_CALL;
#endif

/*
* Get our middleware callable (which is just a string), and make sure it's
Expand Down Expand Up @@ -426,7 +436,9 @@ NR_PHP_WRAPPER_START(nr_guzzle6_client_construct) {
nr_php_zval_free(&middleware);
nr_php_scope_release(&this_var);
}
#if ZEND_MODULE_API_NO < ZEND_8_2_X_API_NO
NR_PHP_WRAPPER_END
#endif

void nr_guzzle6_enable(TSRMLS_D) {
int retval;
Expand Down
4 changes: 4 additions & 0 deletions agent/lib_guzzle6.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ extern void nr_guzzle6_minit(TSRMLS_D);
/*
* Purpose : Client::__construct() wrapper for Guzzle 6.
*/
#if ZEND_MODULE_API_NO < ZEND_8_2_X_API_NO
extern NR_PHP_WRAPPER_PROTOTYPE(nr_guzzle6_client_construct);
#else
extern void nr_guzzle6_client_construct(NR_EXECUTE_PROTO);
#endif

#endif /* LIB_GUZZLE4_HDR */
4 changes: 4 additions & 0 deletions agent/lib_mongodb.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,11 @@ NR_PHP_WRAPPER(nr_mongodb_operation_before) {
nr_segment_t* segment = NULL;
segment = nr_segment_start(NRPRG(txn), NULL, NULL);
if (NULL != segment) {
#if ZEND_MODULE_API_NO < ZEND_8_2_X_API_NO
segment->wraprec = auto_segment->wraprec;
#else
segment->execute_data = auto_segment->execute_data;
#endif
}
}
NR_PHP_WRAPPER_END
Expand Down
4 changes: 4 additions & 0 deletions agent/lib_predis.c
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,11 @@ NR_PHP_WRAPPER(nr_predis_webdisconnection_executeCommand_before) {
nr_segment_t* segment = NULL;
segment = nr_segment_start(NRPRG(txn), NULL, NULL);
if (NULL != segment) {
#if ZEND_MODULE_API_NO >= ZEND_8_2_X_API_NO
segment->execute_data = auto_segment->execute_data;
#else
segment->wraprec = auto_segment->wraprec;
#endif
}
}
NR_PHP_WRAPPER_END
Expand Down
Loading
Loading