From 6f92924ea75d0da17323405420b9bd426eb47357 Mon Sep 17 00:00:00 2001 From: Michal Nowacki Date: Wed, 6 Aug 2025 18:09:02 -0400 Subject: [PATCH 1/7] fix(agent): fix once-per-process use of composer When Composer runtime API was used to get packages information in once per process configuration, don't generate packages information using legacy methods. --- agent/lib_composer.c | 21 +++++++++------------ agent/php_txn.c | 9 +++++++++ axiom/nr_txn.c | 8 ++++++++ axiom/nr_txn.h | 1 + 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/agent/lib_composer.c b/agent/lib_composer.c index 674bff3c3..43d57b81e 100644 --- a/agent/lib_composer.c +++ b/agent/lib_composer.c @@ -69,7 +69,7 @@ static int nr_execute_handle_autoload_composer_init(const char* vendor_path) { return NR_SUCCESS; } -static void nr_execute_handle_autoload_composer_get_packages_information( +static bool nr_execute_handle_autoload_composer_get_packages_information( const char* vendor_path) { zval retval; // This is used as a return value for zend_eval_string. // It will only be set if the result of the eval is SUCCESS. @@ -79,13 +79,13 @@ static void nr_execute_handle_autoload_composer_get_packages_information( if (nrunlikely(!NRINI(vulnerability_management_package_detection_enabled))) { // do nothing when collecting package information for vulnerability // management is disabled - return; + return false; } // nrunlikely because this should alredy be ensured by the caller if (nrunlikely(!NRINI(vulnerability_management_composer_api_enabled))) { // do nothing when use of composer to collect package info is disabled - return; + return false; } // clang-format off @@ -124,7 +124,7 @@ static void nr_execute_handle_autoload_composer_get_packages_information( "%s - unable to initialize Composer runtime API - package info " "unavailable", __func__); - return; + return false; } nrl_verbosedebug(NRL_INSTRUMENT, "%s - Composer runtime API available", @@ -135,13 +135,7 @@ static void nr_execute_handle_autoload_composer_get_packages_information( if (SUCCESS != result) { nrl_verbosedebug(NRL_INSTRUMENT, "%s - composer_getallrawdata.php failed", __func__); - return; - } - - if (NR_PHP_PROCESS_GLOBALS(composer_api_per_process_detection)) { - // set the per-process flag to true to avoid re-running composer api - // detection when the per-process detection is enabled. - NR_PHP_PROCESS_GLOBALS(composer_packages_detected) = 1; + return false; } if (IS_ARRAY == Z_TYPE(retval)) { @@ -170,6 +164,7 @@ static void nr_execute_handle_autoload_composer_get_packages_information( __func__, NRP_ARGSTR(strbuf)); } zval_dtor(&retval); + return true; } static char* nr_execute_handle_autoload_composer_get_vendor_path( @@ -274,7 +269,9 @@ void nr_composer_handle_autoload(const char* filename) { NRPRG(txn)->composer_info.composer_detected = true; nr_fw_support_add_library_supportability_metric(NRPRG(txn), "Composer"); - nr_execute_handle_autoload_composer_get_packages_information(vendor_path); + NRTXN(composer_info.packages_detected) + = nr_execute_handle_autoload_composer_get_packages_information( + vendor_path); leave: nr_free(vendor_path); } diff --git a/agent/php_txn.c b/agent/php_txn.c index 4b92f40c6..6d87fcb94 100644 --- a/agent/php_txn.c +++ b/agent/php_txn.c @@ -1151,6 +1151,9 @@ nr_status_t nr_php_txn_begin(const char* appnames, } } + NRTXN(composer_info.packages_detected) + = NR_PHP_PROCESS_GLOBALS(composer_packages_detected); + return NR_SUCCESS; } @@ -1340,6 +1343,12 @@ nr_status_t nr_php_txn_end(int ignoretxn, int in_post_deactivate TSRMLS_DC) { if (NR_FAILURE == ret) { nrl_debug(NRL_TXN, "failed to send txn"); } + if (NR_PHP_PROCESS_GLOBALS(composer_api_per_process_detection)) { + // set the per-process flag to true to avoid re-running composer api + // detection when the per-process detection is enabled. + NR_PHP_PROCESS_GLOBALS(composer_packages_detected) + = NRTXN(composer_info.packages_detected); + } } } diff --git a/axiom/nr_txn.c b/axiom/nr_txn.c index 85e7739af..f30e4dd69 100644 --- a/axiom/nr_txn.c +++ b/axiom/nr_txn.c @@ -3546,6 +3546,14 @@ nr_php_package_t* nr_txn_add_php_package_from_source( nr_php_package_t* nr_txn_add_php_package(nrtxn_t* txn, char* package_name, char* package_version) { + if (nrunlikely(NULL == txn)) { + return NULL; + } + if (txn->composer_info.packages_detected) { + // don't add packages from legacy source if packages have been detected + // using composer runtime api + return NULL; + } return nr_txn_add_php_package_from_source(txn, package_name, package_version, NR_PHP_PACKAGE_SOURCE_LEGACY); } diff --git a/axiom/nr_txn.h b/axiom/nr_txn.h index 02d45f65f..769a00abd 100644 --- a/axiom/nr_txn.h +++ b/axiom/nr_txn.h @@ -210,6 +210,7 @@ typedef enum _nr_cpu_usage_t { typedef struct _nr_composer_info_t { bool autoload_detected; bool composer_detected; + bool packages_detected; } nr_composer_info_t; /* From bed0f5ca95052af3d4362aac800c05a0c853b5de Mon Sep 17 00:00:00 2001 From: Michal Nowacki Date: Thu, 7 Aug 2025 11:44:07 -0400 Subject: [PATCH 2/7] make sure legacy package information cannot be added to txn regardless of API used --- axiom/nr_txn.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/axiom/nr_txn.c b/axiom/nr_txn.c index f30e4dd69..afff20b54 100644 --- a/axiom/nr_txn.c +++ b/axiom/nr_txn.c @@ -3539,6 +3539,13 @@ nr_php_package_t* nr_txn_add_php_package_from_source( return NULL; } + if (NR_PHP_PACKAGE_SOURCE_LEGACY == source + && txn->composer_info.packages_detected) { + // don't add packages from legacy source if packages have been detected + // using composer runtime api + return NULL; + } + p = nr_php_package_create_with_source(package_name, package_version, source); return nr_php_packages_add_package(txn->php_packages, p); } @@ -3546,14 +3553,6 @@ nr_php_package_t* nr_txn_add_php_package_from_source( nr_php_package_t* nr_txn_add_php_package(nrtxn_t* txn, char* package_name, char* package_version) { - if (nrunlikely(NULL == txn)) { - return NULL; - } - if (txn->composer_info.packages_detected) { - // don't add packages from legacy source if packages have been detected - // using composer runtime api - return NULL; - } return nr_txn_add_php_package_from_source(txn, package_name, package_version, NR_PHP_PACKAGE_SOURCE_LEGACY); } From 7d9b995bbe7888469db0765797e3aab87e3505a7 Mon Sep 17 00:00:00 2001 From: Michal Nowacki Date: Thu, 7 Aug 2025 11:52:21 -0400 Subject: [PATCH 3/7] fix package info generation when info from Composer API is available Don't generate packages information using legacy methods if Composer API was used to get packages information regardless if it's used once per process or once per request. --- agent/php_execute.c | 2 +- agent/php_globals.h | 7 +++++-- agent/php_txn.c | 4 +++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/agent/php_execute.c b/agent/php_execute.c index 54a7d5a57..8e50d0106 100644 --- a/agent/php_execute.c +++ b/agent/php_execute.c @@ -876,7 +876,7 @@ static void nr_execute_handle_autoload(const char* filename, } if (NR_PHP_PROCESS_GLOBALS(composer_api_per_process_detection) - && NR_PHP_PROCESS_GLOBALS(composer_packages_detected)) { + && NR_PHP_PROCESS_GLOBALS(composer_api_per_process_called)) { // do nothing if per-process detection is enabled and the flag to track // detection is true return; diff --git a/agent/php_globals.h b/agent/php_globals.h index 4d0a2fe24..9167b176b 100644 --- a/agent/php_globals.h +++ b/agent/php_globals.h @@ -78,9 +78,12 @@ typedef struct _nrphpglobals_t { int composer_api_per_process_detection; /* Enables per-process VM package detection when Composer API is also enabled */ - int composer_packages_detected; /* Flag to indicate that Composer package - detection has run. Used in conjunction with + int composer_api_per_process_called; /* Flag to indicate that Composer API was + called by current process to detect packages + and succeeded. Used in conjunction with composer_api_per_process_detection. */ + int composer_packages_detected; /* Flag to indicate that Composer API was + called to detect packages and succeeded. */ char* docker_id; /* 64 byte hex docker ID parsed from /proc/self/mountinfo */ /* Original PHP callback pointer contents */ diff --git a/agent/php_txn.c b/agent/php_txn.c index 6d87fcb94..cb48c2aca 100644 --- a/agent/php_txn.c +++ b/agent/php_txn.c @@ -1346,9 +1346,11 @@ nr_status_t nr_php_txn_end(int ignoretxn, int in_post_deactivate TSRMLS_DC) { if (NR_PHP_PROCESS_GLOBALS(composer_api_per_process_detection)) { // set the per-process flag to true to avoid re-running composer api // detection when the per-process detection is enabled. - NR_PHP_PROCESS_GLOBALS(composer_packages_detected) + NR_PHP_PROCESS_GLOBALS(composer_api_per_process_called) = NRTXN(composer_info.packages_detected); } + NR_PHP_PROCESS_GLOBALS(composer_packages_detected) + = NRTXN(composer_info.packages_detected); } } From aabc78ac1322d94cb8bae9198a6e8d8268bb8dff Mon Sep 17 00:00:00 2001 From: Michal Nowacki Date: Thu, 7 Aug 2025 12:06:19 -0400 Subject: [PATCH 4/7] add unit tests for changed functionality Test that legacy package information is not added after Composer API was successfully used. --- axiom/tests/test_txn.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/axiom/tests/test_txn.c b/axiom/tests/test_txn.c index adac091d2..6acde2a6a 100644 --- a/axiom/tests/test_txn.c +++ b/axiom/tests/test_txn.c @@ -8665,6 +8665,16 @@ static void test_nr_txn_add_php_package(void) { tlib_pass_if_ptr_equal( "same package name, different version, add returns same pointer", p1, p2); nr_txn_destroy(&txn); + + txn = new_txn(0); + // simulate composer api was successfully used + txn->composer_info.packages_detected = true; + p1 = nr_txn_add_php_package(txn, package_name1, package_version1); + tlib_pass_if_null( + "legacy package information not added to transaction after composer api " + "was called successfully", + p1); + nr_txn_destroy(&txn); } static void test_nr_txn_add_php_package_from_source(void) { @@ -8730,6 +8740,29 @@ static void test_nr_txn_add_php_package_from_source(void) { p2->package_version); nr_txn_destroy(&txn); + + txn = new_txn(0); + // simulate composer api was successfully used + txn->composer_info.packages_detected = true; + p1 = nr_txn_add_php_package_from_source(txn, package_name1, package_version1, + NR_PHP_PACKAGE_SOURCE_LEGACY); + tlib_pass_if_null( + "legacy package information not added to transaction after composer api " + "was called successfully", + p1); + p1 = nr_txn_add_php_package_from_source(txn, package_name1, package_version1, + NR_PHP_PACKAGE_SOURCE_SUGGESTION); + tlib_pass_if_not_null( + "suggestion package information added to transaction even after composer " + "api was called successfully", + p1); + p1 = nr_txn_add_php_package_from_source(txn, package_name1, package_version1, + NR_PHP_PACKAGE_SOURCE_COMPOSER); + tlib_pass_if_not_null( + "composer package information added to transaction even after composer " + "api was called successfully", + p1); + nr_txn_destroy(&txn); } static void test_nr_txn_suggest_package_supportability_metric(void) { From fc19fd718cec6390015e2f4afb955e5616a231ae Mon Sep 17 00:00:00 2001 From: Michal Nowacki Date: Thu, 7 Aug 2025 14:03:49 -0400 Subject: [PATCH 5/7] re-use same global state for per-process detection There's no need for additional state to call Composer API only once per process - composer_packages_detected global state can be used for per-process detection as well as per-request. --- agent/php_execute.c | 2 +- agent/php_globals.h | 4 ---- agent/php_txn.c | 6 ------ 3 files changed, 1 insertion(+), 11 deletions(-) diff --git a/agent/php_execute.c b/agent/php_execute.c index 8e50d0106..54a7d5a57 100644 --- a/agent/php_execute.c +++ b/agent/php_execute.c @@ -876,7 +876,7 @@ static void nr_execute_handle_autoload(const char* filename, } if (NR_PHP_PROCESS_GLOBALS(composer_api_per_process_detection) - && NR_PHP_PROCESS_GLOBALS(composer_api_per_process_called)) { + && NR_PHP_PROCESS_GLOBALS(composer_packages_detected)) { // do nothing if per-process detection is enabled and the flag to track // detection is true return; diff --git a/agent/php_globals.h b/agent/php_globals.h index 9167b176b..082420c78 100644 --- a/agent/php_globals.h +++ b/agent/php_globals.h @@ -78,10 +78,6 @@ typedef struct _nrphpglobals_t { int composer_api_per_process_detection; /* Enables per-process VM package detection when Composer API is also enabled */ - int composer_api_per_process_called; /* Flag to indicate that Composer API was - called by current process to detect packages - and succeeded. Used in conjunction with - composer_api_per_process_detection. */ int composer_packages_detected; /* Flag to indicate that Composer API was called to detect packages and succeeded. */ char* docker_id; /* 64 byte hex docker ID parsed from /proc/self/mountinfo */ diff --git a/agent/php_txn.c b/agent/php_txn.c index cb48c2aca..89a0fc45b 100644 --- a/agent/php_txn.c +++ b/agent/php_txn.c @@ -1343,12 +1343,6 @@ nr_status_t nr_php_txn_end(int ignoretxn, int in_post_deactivate TSRMLS_DC) { if (NR_FAILURE == ret) { nrl_debug(NRL_TXN, "failed to send txn"); } - if (NR_PHP_PROCESS_GLOBALS(composer_api_per_process_detection)) { - // set the per-process flag to true to avoid re-running composer api - // detection when the per-process detection is enabled. - NR_PHP_PROCESS_GLOBALS(composer_api_per_process_called) - = NRTXN(composer_info.packages_detected); - } NR_PHP_PROCESS_GLOBALS(composer_packages_detected) = NRTXN(composer_info.packages_detected); } From 38c10489ba349bed541270e23a17afacf718965c Mon Sep 17 00:00:00 2001 From: Michal Nowacki Date: Thu, 7 Aug 2025 14:52:23 -0400 Subject: [PATCH 6/7] add composer api call backoff (sort of :)) When use Composer API once per process is set, prevent calling the API when calling Composer API fails for whatever reason. This also means that legacy package information will be used when Composer API call failed. --- agent/lib_composer.c | 19 ++++++++++++------- agent/php_execute.c | 6 +++--- agent/php_globals.h | 4 ++-- agent/php_minit.c | 3 ++- agent/php_txn.c | 8 ++++---- axiom/nr_txn.c | 3 ++- axiom/nr_txn.h | 11 ++++++++++- axiom/tests/test_txn.c | 6 ++++-- 8 files changed, 39 insertions(+), 21 deletions(-) diff --git a/agent/lib_composer.c b/agent/lib_composer.c index 43d57b81e..0cf87c5ef 100644 --- a/agent/lib_composer.c +++ b/agent/lib_composer.c @@ -69,23 +69,26 @@ static int nr_execute_handle_autoload_composer_init(const char* vendor_path) { return NR_SUCCESS; } -static bool nr_execute_handle_autoload_composer_get_packages_information( +static nr_composer_api_call_result_t +nr_execute_handle_autoload_composer_get_packages_information( const char* vendor_path) { zval retval; // This is used as a return value for zend_eval_string. // It will only be set if the result of the eval is SUCCESS. int result = FAILURE; + nr_composer_api_call_result_t api_call_result + = NR_COMPOSER_API_CALL_RESULT_UNSET; // nrunlikely because this should alredy be ensured by the caller if (nrunlikely(!NRINI(vulnerability_management_package_detection_enabled))) { // do nothing when collecting package information for vulnerability // management is disabled - return false; + return NR_COMPOSER_API_CALL_RESULT_INVALID_USE; } // nrunlikely because this should alredy be ensured by the caller if (nrunlikely(!NRINI(vulnerability_management_composer_api_enabled))) { // do nothing when use of composer to collect package info is disabled - return false; + return NR_COMPOSER_API_CALL_RESULT_INVALID_USE; } // clang-format off @@ -124,7 +127,7 @@ static bool nr_execute_handle_autoload_composer_get_packages_information( "%s - unable to initialize Composer runtime API - package info " "unavailable", __func__); - return false; + return NR_COMPOSER_API_CALL_RESULT_INIT_FAILURE; } nrl_verbosedebug(NRL_INSTRUMENT, "%s - Composer runtime API available", @@ -135,7 +138,7 @@ static bool nr_execute_handle_autoload_composer_get_packages_information( if (SUCCESS != result) { nrl_verbosedebug(NRL_INSTRUMENT, "%s - composer_getallrawdata.php failed", __func__); - return false; + return NR_COMPOSER_API_CALL_RESULT_CALL_FAILURE; } if (IS_ARRAY == Z_TYPE(retval)) { @@ -156,15 +159,17 @@ static bool nr_execute_handle_autoload_composer_get_packages_information( } } ZEND_HASH_FOREACH_END(); + api_call_result = NR_COMPOSER_API_CALL_RESULT_PACKAGES_COLLECTED; } else { char strbuf[80]; nr_format_zval_for_debug(&retval, strbuf, 0, sizeof(strbuf) - 1, 0); nrl_verbosedebug(NRL_INSTRUMENT, "%s - installed packages is: " NRP_FMT ", not an array", __func__, NRP_ARGSTR(strbuf)); + api_call_result = NR_COMPOSER_API_CALL_RESULT_INVALID_RESULT; } zval_dtor(&retval); - return true; + return api_call_result; } static char* nr_execute_handle_autoload_composer_get_vendor_path( @@ -269,7 +274,7 @@ void nr_composer_handle_autoload(const char* filename) { NRPRG(txn)->composer_info.composer_detected = true; nr_fw_support_add_library_supportability_metric(NRPRG(txn), "Composer"); - NRTXN(composer_info.packages_detected) + NRTXN(composer_info.api_call_result) = nr_execute_handle_autoload_composer_get_packages_information( vendor_path); leave: diff --git a/agent/php_execute.c b/agent/php_execute.c index 54a7d5a57..bacf23ff5 100644 --- a/agent/php_execute.c +++ b/agent/php_execute.c @@ -876,9 +876,9 @@ static void nr_execute_handle_autoload(const char* filename, } if (NR_PHP_PROCESS_GLOBALS(composer_api_per_process_detection) - && NR_PHP_PROCESS_GLOBALS(composer_packages_detected)) { - // do nothing if per-process detection is enabled and the flag to track - // detection is true + && NR_COMPOSER_API_CALL_RESULT_UNSET + != NR_PHP_PROCESS_GLOBALS(composer_api_call_result)) { + // do nothing if per-process detection is enabled and api was already called return; } diff --git a/agent/php_globals.h b/agent/php_globals.h index 082420c78..9a24a2084 100644 --- a/agent/php_globals.h +++ b/agent/php_globals.h @@ -78,8 +78,8 @@ typedef struct _nrphpglobals_t { int composer_api_per_process_detection; /* Enables per-process VM package detection when Composer API is also enabled */ - int composer_packages_detected; /* Flag to indicate that Composer API was - called to detect packages and succeeded. */ + nr_composer_api_call_result_t + composer_api_call_result; /* Composer API's call result */ char* docker_id; /* 64 byte hex docker ID parsed from /proc/self/mountinfo */ /* Original PHP callback pointer contents */ diff --git a/agent/php_minit.c b/agent/php_minit.c index 7cd14bc59..f42172f2b 100644 --- a/agent/php_minit.c +++ b/agent/php_minit.c @@ -456,7 +456,8 @@ PHP_MINIT_FUNCTION(newrelic) { = nr_php_check_for_upgrade_license_key(); NR_PHP_PROCESS_GLOBALS(high_security) = 0; NR_PHP_PROCESS_GLOBALS(preload_framework_library_detection) = 1; - NR_PHP_PROCESS_GLOBALS(composer_packages_detected) = 0; + NR_PHP_PROCESS_GLOBALS(composer_api_call_result) + = NR_COMPOSER_API_CALL_RESULT_UNSET; nr_php_populate_apache_process_globals(); nr_php_api_distributed_trace_register_userland_class(TSRMLS_C); /* diff --git a/agent/php_txn.c b/agent/php_txn.c index 89a0fc45b..6457237cc 100644 --- a/agent/php_txn.c +++ b/agent/php_txn.c @@ -1151,8 +1151,8 @@ nr_status_t nr_php_txn_begin(const char* appnames, } } - NRTXN(composer_info.packages_detected) - = NR_PHP_PROCESS_GLOBALS(composer_packages_detected); + NRTXN(composer_info.api_call_result) + = NR_PHP_PROCESS_GLOBALS(composer_api_call_result); return NR_SUCCESS; } @@ -1343,8 +1343,8 @@ nr_status_t nr_php_txn_end(int ignoretxn, int in_post_deactivate TSRMLS_DC) { if (NR_FAILURE == ret) { nrl_debug(NRL_TXN, "failed to send txn"); } - NR_PHP_PROCESS_GLOBALS(composer_packages_detected) - = NRTXN(composer_info.packages_detected); + NR_PHP_PROCESS_GLOBALS(composer_api_call_result) + = NRTXN(composer_info.api_call_result); } } diff --git a/axiom/nr_txn.c b/axiom/nr_txn.c index afff20b54..977b15813 100644 --- a/axiom/nr_txn.c +++ b/axiom/nr_txn.c @@ -3540,7 +3540,8 @@ nr_php_package_t* nr_txn_add_php_package_from_source( } if (NR_PHP_PACKAGE_SOURCE_LEGACY == source - && txn->composer_info.packages_detected) { + && NR_COMPOSER_API_CALL_RESULT_PACKAGES_COLLECTED + == txn->composer_info.api_call_result) { // don't add packages from legacy source if packages have been detected // using composer runtime api return NULL; diff --git a/axiom/nr_txn.h b/axiom/nr_txn.h index 769a00abd..b5e916d43 100644 --- a/axiom/nr_txn.h +++ b/axiom/nr_txn.h @@ -207,10 +207,19 @@ typedef enum _nr_cpu_usage_t { NR_CPU_USAGE_COUNT = 2 } nr_cpu_usage_t; +typedef enum { + NR_COMPOSER_API_CALL_RESULT_UNSET = 0, + NR_COMPOSER_API_CALL_RESULT_INVALID_USE = 1, + NR_COMPOSER_API_CALL_RESULT_INIT_FAILURE = 2, + NR_COMPOSER_API_CALL_RESULT_CALL_FAILURE = 3, + NR_COMPOSER_API_CALL_RESULT_PACKAGES_COLLECTED = 4, + NR_COMPOSER_API_CALL_RESULT_INVALID_RESULT = 5, +} nr_composer_api_call_result_t; + typedef struct _nr_composer_info_t { bool autoload_detected; bool composer_detected; - bool packages_detected; + nr_composer_api_call_result_t api_call_result; } nr_composer_info_t; /* diff --git a/axiom/tests/test_txn.c b/axiom/tests/test_txn.c index 6acde2a6a..39e5d20fb 100644 --- a/axiom/tests/test_txn.c +++ b/axiom/tests/test_txn.c @@ -8668,7 +8668,8 @@ static void test_nr_txn_add_php_package(void) { txn = new_txn(0); // simulate composer api was successfully used - txn->composer_info.packages_detected = true; + txn->composer_info.api_call_result + = NR_COMPOSER_API_CALL_RESULT_PACKAGES_COLLECTED; p1 = nr_txn_add_php_package(txn, package_name1, package_version1); tlib_pass_if_null( "legacy package information not added to transaction after composer api " @@ -8743,7 +8744,8 @@ static void test_nr_txn_add_php_package_from_source(void) { txn = new_txn(0); // simulate composer api was successfully used - txn->composer_info.packages_detected = true; + txn->composer_info.api_call_result + = NR_COMPOSER_API_CALL_RESULT_PACKAGES_COLLECTED; p1 = nr_txn_add_php_package_from_source(txn, package_name1, package_version1, NR_PHP_PACKAGE_SOURCE_LEGACY); tlib_pass_if_null( From 684df0a554f345856510921d6198f66df2bf11c4 Mon Sep 17 00:00:00 2001 From: Michal Nowacki Date: Thu, 7 Aug 2025 15:19:00 -0400 Subject: [PATCH 7/7] refactor: shorten names for composer api stuff --- agent/lib_composer.c | 21 ++++++++++----------- agent/php_execute.c | 9 +++++---- agent/php_globals.h | 3 +-- agent/php_minit.c | 3 +-- agent/php_txn.c | 7 +++---- axiom/nr_txn.c | 4 ++-- axiom/nr_txn.h | 16 ++++++++-------- axiom/tests/test_txn.c | 6 ++---- 8 files changed, 32 insertions(+), 37 deletions(-) diff --git a/agent/lib_composer.c b/agent/lib_composer.c index 0cf87c5ef..7f2c4eca9 100644 --- a/agent/lib_composer.c +++ b/agent/lib_composer.c @@ -69,26 +69,25 @@ static int nr_execute_handle_autoload_composer_init(const char* vendor_path) { return NR_SUCCESS; } -static nr_composer_api_call_result_t +static nr_composer_api_status_t nr_execute_handle_autoload_composer_get_packages_information( const char* vendor_path) { zval retval; // This is used as a return value for zend_eval_string. // It will only be set if the result of the eval is SUCCESS. int result = FAILURE; - nr_composer_api_call_result_t api_call_result - = NR_COMPOSER_API_CALL_RESULT_UNSET; + nr_composer_api_status_t api_status = NR_COMPOSER_API_STATUS_UNSET; // nrunlikely because this should alredy be ensured by the caller if (nrunlikely(!NRINI(vulnerability_management_package_detection_enabled))) { // do nothing when collecting package information for vulnerability // management is disabled - return NR_COMPOSER_API_CALL_RESULT_INVALID_USE; + return NR_COMPOSER_API_STATUS_INVALID_USE; } // nrunlikely because this should alredy be ensured by the caller if (nrunlikely(!NRINI(vulnerability_management_composer_api_enabled))) { // do nothing when use of composer to collect package info is disabled - return NR_COMPOSER_API_CALL_RESULT_INVALID_USE; + return NR_COMPOSER_API_STATUS_INVALID_USE; } // clang-format off @@ -127,7 +126,7 @@ nr_execute_handle_autoload_composer_get_packages_information( "%s - unable to initialize Composer runtime API - package info " "unavailable", __func__); - return NR_COMPOSER_API_CALL_RESULT_INIT_FAILURE; + return NR_COMPOSER_API_STATUS_INIT_FAILURE; } nrl_verbosedebug(NRL_INSTRUMENT, "%s - Composer runtime API available", @@ -138,7 +137,7 @@ nr_execute_handle_autoload_composer_get_packages_information( if (SUCCESS != result) { nrl_verbosedebug(NRL_INSTRUMENT, "%s - composer_getallrawdata.php failed", __func__); - return NR_COMPOSER_API_CALL_RESULT_CALL_FAILURE; + return NR_COMPOSER_API_STATUS_CALL_FAILURE; } if (IS_ARRAY == Z_TYPE(retval)) { @@ -159,17 +158,17 @@ nr_execute_handle_autoload_composer_get_packages_information( } } ZEND_HASH_FOREACH_END(); - api_call_result = NR_COMPOSER_API_CALL_RESULT_PACKAGES_COLLECTED; + api_status = NR_COMPOSER_API_STATUS_PACKAGES_COLLECTED; } else { char strbuf[80]; nr_format_zval_for_debug(&retval, strbuf, 0, sizeof(strbuf) - 1, 0); nrl_verbosedebug(NRL_INSTRUMENT, "%s - installed packages is: " NRP_FMT ", not an array", __func__, NRP_ARGSTR(strbuf)); - api_call_result = NR_COMPOSER_API_CALL_RESULT_INVALID_RESULT; + api_status = NR_COMPOSER_API_STATUS_INVALID_RESULT; } zval_dtor(&retval); - return api_call_result; + return api_status; } static char* nr_execute_handle_autoload_composer_get_vendor_path( @@ -274,7 +273,7 @@ void nr_composer_handle_autoload(const char* filename) { NRPRG(txn)->composer_info.composer_detected = true; nr_fw_support_add_library_supportability_metric(NRPRG(txn), "Composer"); - NRTXN(composer_info.api_call_result) + NRTXN(composer_info.api_status) = nr_execute_handle_autoload_composer_get_packages_information( vendor_path); leave: diff --git a/agent/php_execute.c b/agent/php_execute.c index bacf23ff5..c654402d5 100644 --- a/agent/php_execute.c +++ b/agent/php_execute.c @@ -875,12 +875,13 @@ static void nr_execute_handle_autoload(const char* filename, return; } - if (NR_PHP_PROCESS_GLOBALS(composer_api_per_process_detection) - && NR_COMPOSER_API_CALL_RESULT_UNSET - != NR_PHP_PROCESS_GLOBALS(composer_api_call_result)) { - // do nothing if per-process detection is enabled and api was already called + // clang-format off + if (NR_PHP_PROCESS_GLOBALS(composer_api_per_process_detection) && + NR_COMPOSER_API_STATUS_UNSET != NR_PHP_PROCESS_GLOBALS(composer_api_status)) { + // do nothing if per-process detection is enabled and composer api status is set return; } + // clang-format on if (!nr_striendswith(STR_AND_LEN(filename), AUTOLOAD_MAGIC_FILE, AUTOLOAD_MAGIC_FILE_LEN)) { diff --git a/agent/php_globals.h b/agent/php_globals.h index 9a24a2084..ea345cfa0 100644 --- a/agent/php_globals.h +++ b/agent/php_globals.h @@ -78,8 +78,7 @@ typedef struct _nrphpglobals_t { int composer_api_per_process_detection; /* Enables per-process VM package detection when Composer API is also enabled */ - nr_composer_api_call_result_t - composer_api_call_result; /* Composer API's call result */ + nr_composer_api_status_t composer_api_status; /* Composer API's status */ char* docker_id; /* 64 byte hex docker ID parsed from /proc/self/mountinfo */ /* Original PHP callback pointer contents */ diff --git a/agent/php_minit.c b/agent/php_minit.c index f42172f2b..67561e08d 100644 --- a/agent/php_minit.c +++ b/agent/php_minit.c @@ -456,8 +456,7 @@ PHP_MINIT_FUNCTION(newrelic) { = nr_php_check_for_upgrade_license_key(); NR_PHP_PROCESS_GLOBALS(high_security) = 0; NR_PHP_PROCESS_GLOBALS(preload_framework_library_detection) = 1; - NR_PHP_PROCESS_GLOBALS(composer_api_call_result) - = NR_COMPOSER_API_CALL_RESULT_UNSET; + NR_PHP_PROCESS_GLOBALS(composer_api_status) = NR_COMPOSER_API_STATUS_UNSET; nr_php_populate_apache_process_globals(); nr_php_api_distributed_trace_register_userland_class(TSRMLS_C); /* diff --git a/agent/php_txn.c b/agent/php_txn.c index 6457237cc..800cfb7f2 100644 --- a/agent/php_txn.c +++ b/agent/php_txn.c @@ -1151,8 +1151,7 @@ nr_status_t nr_php_txn_begin(const char* appnames, } } - NRTXN(composer_info.api_call_result) - = NR_PHP_PROCESS_GLOBALS(composer_api_call_result); + NRTXN(composer_info.api_status) = NR_PHP_PROCESS_GLOBALS(composer_api_status); return NR_SUCCESS; } @@ -1343,8 +1342,8 @@ nr_status_t nr_php_txn_end(int ignoretxn, int in_post_deactivate TSRMLS_DC) { if (NR_FAILURE == ret) { nrl_debug(NRL_TXN, "failed to send txn"); } - NR_PHP_PROCESS_GLOBALS(composer_api_call_result) - = NRTXN(composer_info.api_call_result); + NR_PHP_PROCESS_GLOBALS(composer_api_status) + = NRTXN(composer_info.api_status); } } diff --git a/axiom/nr_txn.c b/axiom/nr_txn.c index 977b15813..60e1ea5ee 100644 --- a/axiom/nr_txn.c +++ b/axiom/nr_txn.c @@ -3540,8 +3540,8 @@ nr_php_package_t* nr_txn_add_php_package_from_source( } if (NR_PHP_PACKAGE_SOURCE_LEGACY == source - && NR_COMPOSER_API_CALL_RESULT_PACKAGES_COLLECTED - == txn->composer_info.api_call_result) { + && NR_COMPOSER_API_STATUS_PACKAGES_COLLECTED + == txn->composer_info.api_status) { // don't add packages from legacy source if packages have been detected // using composer runtime api return NULL; diff --git a/axiom/nr_txn.h b/axiom/nr_txn.h index b5e916d43..9a29fd878 100644 --- a/axiom/nr_txn.h +++ b/axiom/nr_txn.h @@ -208,18 +208,18 @@ typedef enum _nr_cpu_usage_t { } nr_cpu_usage_t; typedef enum { - NR_COMPOSER_API_CALL_RESULT_UNSET = 0, - NR_COMPOSER_API_CALL_RESULT_INVALID_USE = 1, - NR_COMPOSER_API_CALL_RESULT_INIT_FAILURE = 2, - NR_COMPOSER_API_CALL_RESULT_CALL_FAILURE = 3, - NR_COMPOSER_API_CALL_RESULT_PACKAGES_COLLECTED = 4, - NR_COMPOSER_API_CALL_RESULT_INVALID_RESULT = 5, -} nr_composer_api_call_result_t; + NR_COMPOSER_API_STATUS_UNSET = 0, + NR_COMPOSER_API_STATUS_INVALID_USE = 1, + NR_COMPOSER_API_STATUS_INIT_FAILURE = 2, + NR_COMPOSER_API_STATUS_CALL_FAILURE = 3, + NR_COMPOSER_API_STATUS_PACKAGES_COLLECTED = 4, + NR_COMPOSER_API_STATUS_INVALID_RESULT = 5, +} nr_composer_api_status_t; typedef struct _nr_composer_info_t { bool autoload_detected; bool composer_detected; - nr_composer_api_call_result_t api_call_result; + nr_composer_api_status_t api_status; } nr_composer_info_t; /* diff --git a/axiom/tests/test_txn.c b/axiom/tests/test_txn.c index 39e5d20fb..493213206 100644 --- a/axiom/tests/test_txn.c +++ b/axiom/tests/test_txn.c @@ -8668,8 +8668,7 @@ static void test_nr_txn_add_php_package(void) { txn = new_txn(0); // simulate composer api was successfully used - txn->composer_info.api_call_result - = NR_COMPOSER_API_CALL_RESULT_PACKAGES_COLLECTED; + txn->composer_info.api_status = NR_COMPOSER_API_STATUS_PACKAGES_COLLECTED; p1 = nr_txn_add_php_package(txn, package_name1, package_version1); tlib_pass_if_null( "legacy package information not added to transaction after composer api " @@ -8744,8 +8743,7 @@ static void test_nr_txn_add_php_package_from_source(void) { txn = new_txn(0); // simulate composer api was successfully used - txn->composer_info.api_call_result - = NR_COMPOSER_API_CALL_RESULT_PACKAGES_COLLECTED; + txn->composer_info.api_status = NR_COMPOSER_API_STATUS_PACKAGES_COLLECTED; p1 = nr_txn_add_php_package_from_source(txn, package_name1, package_version1, NR_PHP_PACKAGE_SOURCE_LEGACY); tlib_pass_if_null(