Skip to content

Commit d6a1277

Browse files
committed
refactor: cleanup the code a bit
1 parent 7915d67 commit d6a1277

File tree

3 files changed

+60
-40
lines changed

3 files changed

+60
-40
lines changed

agent/fw_hooks.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ extern void nr_fw_zend2_enable(TSRMLS_D);
4646

4747
/* Libraries. */
4848
extern void nr_aws_sdk_php_enable();
49-
extern void nr_composer_detected();
5049
extern void nr_doctrine2_enable(TSRMLS_D);
5150
extern void nr_guzzle3_enable(TSRMLS_D);
5251
extern void nr_guzzle4_enable(TSRMLS_D);

agent/php_execute.c

Lines changed: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,6 @@ static nr_library_table_t libraries[] = {
508508

509509
{"Predis", NR_PSTR("predis/src/client.php"), nr_predis_enable},
510510
{"Predis", NR_PSTR("predis/client.php"), nr_predis_enable},
511-
{"PHP_Package", NR_PSTR("vendor/autoload.php"), nr_composer_detected},
512511

513512
/*
514513
* Allow Zend Framework 1.x to be detected as a library as well as a
@@ -933,28 +932,11 @@ static void nr_execute_handle_library(const char* filename,
933932
}
934933
}
935934

936-
void nr_composer_detected() {
937-
if (NULL != NRPRG(txn) && !NRPRG(txn)->detection_status.composer_detected) {
938-
const char* magic_file_1
939-
= "vendor/composer/autoload_real.php";
940-
const char* composer_file = "vendor/composer/installed.php";
941-
// the below should be using the absolute path
942-
if (0 == nr_access(magic_file_1, F_OK | R_OK)) {
943-
if (0 == nr_access(composer_file, F_OK | R_OK)) {
944-
NRPRG(txn)->detection_status.composer_detected = 1;
945-
NRPRG(txn)->detection_status.file_exists = 1;
946-
NRPRG(txn)->detection_status.api_called = 0;
947-
NRPRG(txn)->detection_status.inside_eval_string = 0;
948-
}
949-
}
950-
}
951-
}
952-
953935
static void nr_get_composer_package_information() {
954936
zval retval;
955937
int result = -1;
956938

957-
if (NRPRG(txn)->detection_status.inside_eval_string) {
939+
if (NRPRG(txn)->composer_info.inside_eval_string) {
958940
return;
959941
}
960942

@@ -998,14 +980,14 @@ static void nr_get_composer_package_information() {
998980
" }"
999981
"})();";
1000982

1001-
NRPRG(txn)->detection_status.inside_eval_string = 1;
983+
NRPRG(txn)->composer_info.inside_eval_string = 1;
1002984
result = zend_eval_string(check, &retval, "check if class and methods exist" TSRMLS_CC);
1003-
NRPRG(txn)->detection_status.inside_eval_string = 0;
985+
NRPRG(txn)->composer_info.inside_eval_string = 0;
1004986

1005987
if (result == SUCCESS) {
1006988
if (Z_TYPE(retval) == IS_NULL) {
1007989
if (NULL != NRPRG(txn)) {
1008-
NRPRG(txn)->detection_status.api_called = 1;
990+
NRPRG(txn)->composer_info.api_called = 1;
1009991
}
1010992
nrl_verbosedebug(NRL_TXN, "IS_NULL");
1011993
return;
@@ -1018,10 +1000,10 @@ static void nr_get_composer_package_information() {
10181000
}
10191001
}
10201002
zval_dtor(&retval);
1021-
NRPRG(txn)->detection_status.inside_eval_string = 1;
1003+
NRPRG(txn)->composer_info.inside_eval_string = 1;
10221004
result = zend_eval_string(getpackagename, &retval,
10231005
"get installed packages by name" TSRMLS_CC);
1024-
NRPRG(txn)->detection_status.inside_eval_string = 0;
1006+
NRPRG(txn)->composer_info.inside_eval_string = 0;
10251007
if (result == SUCCESS) {
10261008
if (Z_TYPE(retval) == IS_ARRAY) {
10271009
zval* value;
@@ -1033,9 +1015,9 @@ static void nr_get_composer_package_information() {
10331015
ZEND_HASH_FOREACH_VAL(Z_ARRVAL(retval), value) {
10341016
if (Z_TYPE_P(value) == IS_STRING) {
10351017
buf = nr_formatf(getversion, Z_STRVAL_P(value));
1036-
NRPRG(txn)->detection_status.inside_eval_string = 1;
1018+
NRPRG(txn)->composer_info.inside_eval_string = 1;
10371019
result2 = zend_eval_string(buf, &retval2, "retrieve version for packages");
1038-
NRPRG(txn)->detection_status.inside_eval_string = 0;
1020+
NRPRG(txn)->composer_info.inside_eval_string = 0;
10391021
nr_free(buf);
10401022
if (SUCCESS == result2) {
10411023
if (nr_php_is_zval_valid_string(&retval2)) {
@@ -1050,21 +1032,60 @@ static void nr_get_composer_package_information() {
10501032
ZEND_HASH_FOREACH_END();
10511033
} else {
10521034
if (NULL != NRPRG(txn)) {
1053-
NRPRG(txn)->detection_status.api_called = 1;
1035+
NRPRG(txn)->composer_info.api_called = 1;
10541036
}
10551037
zval_dtor(&retval);
10561038
return;
10571039
}
10581040
zval_dtor(&retval);
1059-
NRPRG(txn)->detection_status.api_called = 1;
1041+
NRPRG(txn)->composer_info.api_called = 1;
10601042
}
10611043
}
10621044

1063-
static void nr_execute_handle_composer() {
1064-
if (NRPRG(txn)->detection_status.file_exists) {
1065-
if (!NRPRG(txn)->detection_status.api_called) {
1066-
nr_get_composer_package_information();
1067-
}
1045+
static bool nr_execute_autoload_is_composer(const char* filename, const size_t filename_len) {
1046+
#define COMPOSER_MAGIC_FILE "composer/autoload_real.php"
1047+
char* vendor_path = NULL; // result of dirname(filename)
1048+
char* composer_magic_file = NULL; // vendor_path + COMPOSER_MAGIC_FILE
1049+
char* cp = NULL;
1050+
bool composer_detected = false;
1051+
1052+
// vendor_path = dirname(filename):
1053+
// 1. allocate space on stack
1054+
vendor_path = nr_alloca(filename_len+1);
1055+
// 2. copy filename to vendor_path
1056+
nr_strcpy(vendor_path, filename);
1057+
// 3. // find last occurence of '/' in vendor_path
1058+
cp = nr_strrchr(vendor_path, '/');
1059+
// 4. replace '/' with '\0' to get the directory path
1060+
*cp = '\0';
1061+
1062+
// composer_magic_file = vendor_path + "/composer/autoload_real.php"
1063+
composer_magic_file = nr_str_append(vendor_path, COMPOSER_MAGIC_FILE, "/");
1064+
1065+
if (0 == nr_access(composer_magic_file, F_OK | R_OK)) {
1066+
nrl_debug(NRL_INSTRUMENT, "detected composer with %s", composer_magic_file);
1067+
composer_detected = true;
1068+
nr_fw_support_add_library_supportability_metric(NRPRG(txn), "Composer");
1069+
}
1070+
1071+
nr_free(composer_magic_file);
1072+
return composer_detected;
1073+
}
1074+
1075+
static void nr_execute_handle_autoload(const char* filename, const size_t filename_len) {
1076+
#define AUTOLOAD_MAGIC_FILE "vendor/autoload.php"
1077+
#define AUTOLOAD_MAGIC_FILE_LEN (sizeof(AUTOLOAD_MAGIC_FILE) - 1)
1078+
1079+
if (NRPRG(txn)->composer_info.autoload_detected) {
1080+
return;
1081+
}
1082+
1083+
if (nr_striendswith(STR_AND_LEN(filename), AUTOLOAD_MAGIC_FILE, AUTOLOAD_MAGIC_FILE_LEN)) {
1084+
nrl_debug(NRL_INSTRUMENT, "detected autoload with %s, which ends with %s", filename, AUTOLOAD_MAGIC_FILE);
1085+
NRPRG(txn)->composer_info.autoload_detected = true;
1086+
nr_fw_support_add_library_supportability_metric(NRPRG(txn), "Autoloader");
1087+
1088+
NRPRG(txn)->composer_info.composer_detected = nr_execute_autoload_is_composer(filename, filename_len);
10681089
}
10691090
}
10701091

@@ -1136,7 +1157,7 @@ static void nr_php_user_instrumentation_from_file(const char* filename,
11361157
nr_execute_handle_framework(all_frameworks, num_all_frameworks, filename,
11371158
filename_len TSRMLS_CC);
11381159
nr_execute_handle_library(filename, filename_len TSRMLS_CC);
1139-
nr_execute_handle_composer(filename, filename_len);
1160+
nr_execute_handle_autoload(filename, filename_len);
11401161
nr_execute_handle_logging_framework(filename, filename_len TSRMLS_CC);
11411162
if (NRINI(vulnerability_management_package_detection_enabled)) {
11421163
nr_execute_handle_package(filename);
@@ -1175,8 +1196,8 @@ static void nr_php_execute_file(const zend_op_array* op_array,
11751196

11761197
nr_php_add_user_instrumentation(TSRMLS_C);
11771198

1178-
if (NRPRG(txn)->detection_status.composer_detected && !NRPRG(txn)->detection_status.api_called) {
1179-
nr_execute_handle_composer(filename, filename_len);
1199+
if (NRPRG(txn)->composer_info.composer_detected && !NRPRG(txn)->composer_info.api_called) {
1200+
nr_get_composer_package_information();
11801201
}
11811202
}
11821203

axiom/nr_txn.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,9 @@ typedef enum _nr_cpu_usage_t {
204204
} nr_cpu_usage_t;
205205

206206
typedef struct _nr_composer_info_t {
207-
int composer_detected;
207+
bool autoload_detected;
208+
bool composer_detected;
208209
int api_called;
209-
int file_exists;
210210
int inside_eval_string;
211211
} nr_composer_info_t;
212212

@@ -309,7 +309,7 @@ typedef struct _nrtxn_t {
309309
nr_distributed_trace_t*
310310
distributed_trace; /* distributed tracing metadata for the transaction */
311311
nr_span_queue_t* span_queue; /* span queue when 8T is enabled */
312-
nr_composer_info_t detection_status;
312+
nr_composer_info_t composer_info;
313313

314314
/*
315315
* flag to indicate if one time (per transaction) logging metrics

0 commit comments

Comments
 (0)