Skip to content

Commit cbbb74f

Browse files
committed
optimize library/framework detection for PHPs 8.0+
Take advantage of observer API and new method of storing wraprecs and don't install fcall_begin/fcall_end handlers for file execution - detect library/ framework and install wraprecs right in fcall_init!
1 parent 4c04db2 commit cbbb74f

File tree

4 files changed

+22
-19
lines changed

4 files changed

+22
-19
lines changed

agent/php_execute.c

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ static void nr_php_user_instrumentation_from_file(const char* filename,
987987
*/
988988
#define METRIC_NAME_MAX_LEN 512
989989

990-
static void nr_php_execute_file(const zend_op_array* op_array,
990+
void nr_php_execute_file(const zend_op_array* op_array,
991991
NR_EXECUTE_PROTO TSRMLS_DC) {
992992
const char* filename = nr_php_op_array_file_name(op_array);
993993
size_t filename_len = nr_php_op_array_file_name_len(op_array);
@@ -1012,7 +1012,9 @@ static void nr_php_execute_file(const zend_op_array* op_array,
10121012
return;
10131013
}
10141014

1015+
#if ZEND_MODULE_API_NO < ZEND_8_0_X_API_NO
10151016
nr_php_add_user_instrumentation(TSRMLS_C);
1017+
#endif
10161018
}
10171019

10181020
/*
@@ -1910,16 +1912,7 @@ static void nr_php_instrument_func_begin(NR_EXECUTE_PROTO) {
19101912

19111913
NRTXNGLOBAL(execute_count) += 1;
19121914
txn_start_time = nr_txn_start_time(NRPRG(txn));
1913-
/*
1914-
* Handle here, but be aware the classes might not be loaded yet.
1915-
*/
1916-
if (nrunlikely(OP_ARRAY_IS_A_FILE(NR_OP_ARRAY))) {
1917-
const char* filename = nr_php_op_array_file_name(NR_OP_ARRAY);
1918-
size_t filename_len = nr_php_op_array_file_name_len(NR_OP_ARRAY);
1919-
nr_execute_handle_framework(all_frameworks, num_all_frameworks,
1920-
filename, filename_len TSRMLS_CC);
1921-
return;
1922-
}
1915+
19231916
if (NULL != NRPRG(cufa_callback) && NRPRG(check_cufa)) {
19241917
/*
19251918
* For PHP 7+, call_user_func_array() is flattened into an inline by
@@ -2005,14 +1998,6 @@ static void nr_php_instrument_func_end(NR_EXECUTE_PROTO) {
20051998
}
20061999
txn_start_time = nr_txn_start_time(NRPRG(txn));
20072000

2008-
/*
2009-
* Let's get the framework info.
2010-
*/
2011-
if (nrunlikely(OP_ARRAY_IS_A_FILE(NR_OP_ARRAY))) {
2012-
nr_php_execute_file(NR_OP_ARRAY, NR_EXECUTE_ORIG_ARGS TSRMLS_CC);
2013-
return;
2014-
}
2015-
20162001
/*
20172002
* Get the current segment and return if null.
20182003
*/

agent/php_execute.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
#define OP_ARRAY_IS_METHOD(OP, FNAME) \
2828
(0 == nr_strcmp(nr_php_op_array_function_name(OP), (FNAME)))
2929

30+
extern void nr_php_execute_file(const zend_op_array* op_array,
31+
NR_EXECUTE_PROTO TSRMLS_DC);
32+
3033
/*
3134
* Purpose: Log information about the execute data in a given execution
3235
* context - either 'execute' (zend_execute) or 'observe' (fcall_init).

agent/php_observer.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ static zend_observer_fcall_handlers nr_php_fcall_register_handlers(
9090
nr_php_show_exec("observe", execute_data, NULL);
9191
}
9292

93+
if (OP_ARRAY_IS_A_FILE(NR_OP_ARRAY)) {
94+
/*
95+
* Let's get the framework info.
96+
*/
97+
nr_php_execute_file(NR_OP_ARRAY, execute_data, NULL TSRMLS_CC);
98+
return handlers;
99+
}
100+
93101
if (OP_ARRAY_IS_A_METHOD(&execute_data->func->op_array)) {
94102
scope_name = execute_data->func->op_array.scope->name;
95103
func_name = execute_data->func->op_array.function_name;

agent/tests/test_txn.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,14 @@ static void test_handle_fpm_error(TSRMLS_D) {
5050

5151
nr_txn_set_path(NULL, NRPRG(txn), "foo", NR_PATH_TYPE_URI,
5252
NR_NOT_OK_TO_OVERWRITE);
53+
#if ZEND_MODULE_API_NO >= ZEND_8_0_X_API_NO /* PHP8+ */
54+
// file execution no longer increments execute_count on PHPs 8.0+
55+
// only user function calls do increment execute_count:
56+
tlib_php_request_eval("function f() {$a = 1 + 1;}\n"
57+
"f(); // create a PHP call frame" TSRMLS_CC);
58+
#else
5359
tlib_php_request_eval("$a = 1 + 1; // create a PHP call frame" TSRMLS_CC);
60+
#endif
5461
nr_php_txn_handle_fpm_error(NRPRG(txn) TSRMLS_CC);
5562
tlib_pass_if_str_equal("transaction path should be unchanged", "foo",
5663
NRTXN(path));

0 commit comments

Comments
 (0)