Skip to content

Commit f56d4e5

Browse files
committed
feat(agent): Moves major version metric creation to R_SHUTDOWN
Changes how the package major number metrics are created so this occurs in R_SHUTDOWN. This allows the use of package versions from ALL sources include the Composer API. Whenever instrumentation for a package detects a package it can create a package suggestion. Initially if a version is not known it is fine to use PHP_PACKAGE_VERSION_UNKNOWN. If latest the instrumentation determines a version from a class constant, etc, then the suggestion can be updated with the version. At the end of the transaction then this list of suggestions is traversed and the actual package data (which could include Composer API data) is referenced and a major number supportability metric is created with the best version available.
1 parent d894dc7 commit f56d4e5

21 files changed

+118
-208
lines changed

agent/fw_drupal.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,8 @@ void nr_drupal_enable(TSRMLS_D) {
885885
if (NRINI(vulnerability_management_package_detection_enabled)) {
886886
nr_txn_add_php_package(NRPRG(txn), PHP_PACKAGE_NAME,
887887
PHP_PACKAGE_VERSION_UNKNOWN);
888-
nr_txn_php_package_set_options(NRPRG(txn), PHP_PACKAGE_NAME,
889-
NR_PHP_PACKAGE_OPTION_MAJOR_METRIC);
890888
}
889+
890+
nr_txn_suggest_package_supportability_metric(NRPRG(txn), PHP_PACKAGE_NAME,
891+
PHP_PACKAGE_VERSION_UNKNOWN);
891892
}

agent/fw_drupal8.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,6 @@ void nr_drupal8_enable(TSRMLS_D) {
761761
PHP_PACKAGE_VERSION_UNKNOWN);
762762
}
763763

764-
nr_txn_php_package_set_options(NRPRG(txn), PHP_PACKAGE_NAME,
765-
NR_PHP_PACKAGE_OPTION_MAJOR_METRIC);
764+
nr_txn_suggest_package_supportability_metric(NRPRG(txn), PHP_PACKAGE_NAME,
765+
PHP_PACKAGE_VERSION_UNKNOWN);
766766
}

agent/fw_laminas3.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ void nr_laminas3_enable(TSRMLS_D) {
167167
if (NRINI(vulnerability_management_package_detection_enabled)) {
168168
nr_txn_add_php_package(NRPRG(txn), PHP_PACKAGE_NAME,
169169
PHP_PACKAGE_VERSION_UNKNOWN);
170-
nr_txn_php_package_set_options(NRPRG(txn), PHP_PACKAGE_NAME,
171-
NR_PHP_PACKAGE_OPTION_MAJOR_METRIC);
172170
}
171+
172+
nr_txn_suggest_package_supportability_metric(NRPRG(txn), PHP_PACKAGE_NAME,
173+
PHP_PACKAGE_VERSION_UNKNOWN);
173174
}

agent/fw_laravel.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -962,10 +962,11 @@ NR_PHP_WRAPPER(nr_laravel_application_construct) {
962962
if (NRINI(vulnerability_management_package_detection_enabled)) {
963963
// Add php package to transaction
964964
nr_txn_add_php_package(NRPRG(txn), PHP_PACKAGE_NAME, version);
965-
nr_txn_php_package_set_options(NRPRG(txn), PHP_PACKAGE_NAME,
966-
NR_PHP_PACKAGE_OPTION_MAJOR_METRIC);
967965
}
968966

967+
nr_txn_suggest_package_supportability_metric(NRPRG(txn), PHP_PACKAGE_NAME,
968+
version);
969+
969970
if (version) {
970971
nrl_debug(NRL_FRAMEWORK, "Laravel version is " NRP_FMT, NRP_PHP(version));
971972

agent/fw_lumen.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ void nr_lumen_enable(TSRMLS_D) {
237237
if (NRINI(vulnerability_management_package_detection_enabled)) {
238238
nr_txn_add_php_package(NRPRG(txn), PHP_PACKAGE_NAME,
239239
PHP_PACKAGE_VERSION_UNKNOWN);
240-
nr_txn_php_package_set_options(NRPRG(txn), PHP_PACKAGE_NAME,
241-
NR_PHP_PACKAGE_OPTION_MAJOR_METRIC);
242240
}
241+
242+
nr_txn_suggest_package_supportability_metric(NRPRG(txn), PHP_PACKAGE_NAME,
243+
PHP_PACKAGE_VERSION_UNKNOWN);
243244
}

agent/fw_slim.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,11 @@ NR_PHP_WRAPPER(nr_slim_application_construct) {
161161
if (NRINI(vulnerability_management_package_detection_enabled)) {
162162
// Add php package to transaction
163163
nr_txn_add_php_package(NRPRG(txn), PHP_PACKAGE_NAME, version);
164-
nr_txn_php_package_set_options(NRPRG(txn), PHP_PACKAGE_NAME,
165-
NR_PHP_PACKAGE_OPTION_MAJOR_METRIC);
166164
}
167165

166+
nr_txn_suggest_package_supportability_metric(NRPRG(txn), PHP_PACKAGE_NAME,
167+
version);
168+
168169
nr_free(version);
169170
nr_php_scope_release(&this_var);
170171
}

agent/fw_symfony4.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,8 @@ void nr_symfony4_enable(TSRMLS_D) {
281281
if (NRINI(vulnerability_management_package_detection_enabled)) {
282282
nr_txn_add_php_package(NRPRG(txn), PHP_PACKAGE_NAME,
283283
PHP_PACKAGE_VERSION_UNKNOWN);
284-
nr_txn_php_package_set_options(NRPRG(txn), PHP_PACKAGE_NAME,
285-
NR_PHP_PACKAGE_OPTION_MAJOR_METRIC);
286284
}
285+
286+
nr_txn_suggest_package_supportability_metric(NRPRG(txn), PHP_PACKAGE_NAME,
287+
PHP_PACKAGE_VERSION_UNKNOWN);
287288
}

agent/fw_wordpress.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -811,9 +811,10 @@ void nr_wordpress_version() {
811811
char* version = Z_STRVAL(retval);
812812
if (NRINI(vulnerability_management_package_detection_enabled)) {
813813
nr_txn_add_php_package(NRPRG(txn), PHP_PACKAGE_NAME, version);
814-
nr_txn_php_package_set_options(NRPRG(txn), PHP_PACKAGE_NAME,
815-
NR_PHP_PACKAGE_OPTION_MAJOR_METRIC);
816814
}
815+
816+
nr_txn_suggest_package_supportability_metric(NRPRG(txn), PHP_PACKAGE_NAME,
817+
version);
817818
}
818819
zval_dtor(&retval);
819820
}

agent/fw_yii.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ void nr_yii2_enable(TSRMLS_D) {
226226
if (NRINI(vulnerability_management_package_detection_enabled)) {
227227
nr_txn_add_php_package(NRPRG(txn), PHP_PACKAGE_NAME,
228228
PHP_PACKAGE_VERSION_UNKNOWN);
229-
nr_txn_php_package_set_options(NRPRG(txn), PHP_PACKAGE_NAME,
230-
NR_PHP_PACKAGE_OPTION_MAJOR_METRIC);
231229
}
230+
231+
nr_txn_suggest_package_supportability_metric(NRPRG(txn), PHP_PACKAGE_NAME,
232+
PHP_PACKAGE_VERSION_UNKNOWN);
232233
}

agent/lib_aws_sdk_php.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ void nr_lib_aws_sdk_php_handle_version() {
6868
if (NRINI(vulnerability_management_package_detection_enabled)) {
6969
/* Add php package to transaction */
7070
nr_txn_add_php_package(NRPRG(txn), PHP_PACKAGE_NAME, version);
71-
nr_txn_php_package_set_options(NRPRG(txn), PHP_PACKAGE_NAME,
72-
NR_PHP_PACKAGE_OPTION_MAJOR_METRIC);
7371
}
7472

73+
nr_txn_suggest_package_supportability_metric(NRPRG(txn), PHP_PACKAGE_NAME,
74+
version);
75+
7576
nr_php_zval_free(&zval_version);
7677
}
7778

agent/lib_doctrine2.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ void nr_doctrine2_enable(TSRMLS_D) {
110110
if (NRINI(vulnerability_management_package_detection_enabled)) {
111111
nr_txn_add_php_package(NRPRG(txn), PHP_PACKAGE_NAME,
112112
PHP_PACKAGE_VERSION_UNKNOWN);
113-
nr_txn_php_package_set_options(NRPRG(txn), PHP_PACKAGE_NAME,
114-
NR_PHP_PACKAGE_OPTION_MAJOR_METRIC);
115113
}
114+
115+
nr_txn_suggest_package_supportability_metric(NRPRG(txn), PHP_PACKAGE_NAME,
116+
PHP_PACKAGE_VERSION_UNKNOWN);
116117
}

agent/lib_guzzle4.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -524,9 +524,10 @@ void nr_guzzle4_enable(TSRMLS_D) {
524524
if (NRINI(vulnerability_management_package_detection_enabled)) {
525525
nr_txn_add_php_package(NRPRG(txn), PHP_PACKAGE_NAME,
526526
PHP_PACKAGE_VERSION_UNKNOWN);
527-
nr_txn_php_package_set_options(NRPRG(txn), PHP_PACKAGE_NAME,
528-
NR_PHP_PACKAGE_OPTION_MAJOR_METRIC);
529527
}
528+
529+
nr_txn_suggest_package_supportability_metric(NRPRG(txn), PHP_PACKAGE_NAME,
530+
PHP_PACKAGE_VERSION_UNKNOWN);
530531
}
531532

532533
void nr_guzzle4_minit(TSRMLS_D) {

agent/lib_guzzle6.c

+8-12
Original file line numberDiff line numberDiff line change
@@ -352,19 +352,10 @@ NR_PHP_WRAPPER_START(nr_guzzle6_client_construct) {
352352
zval* this_var = nr_php_scope_get(NR_EXECUTE_ORIG_ARGS);
353353

354354
char* version = nr_php_get_object_constant(this_var, "VERSION");
355-
nr_php_package_t* p = NULL;
356355

357356
if (NRINI(vulnerability_management_package_detection_enabled)) {
358357
// Add php package to transaction
359-
p = nr_txn_add_php_package(NRPRG(txn), PHP_PACKAGE_NAME, version);
360-
361-
/* mark package for package major metric creation later if
362-
* a full version was detected
363-
*/
364-
if (NULL != version) {
365-
nr_txn_php_package_set_options(NRPRG(txn), PHP_PACKAGE_NAME,
366-
NR_PHP_PACKAGE_OPTION_MAJOR_METRIC);
367-
}
358+
nr_txn_add_php_package(NRPRG(txn), PHP_PACKAGE_NAME, version);
368359
}
369360

370361
/*
@@ -378,10 +369,15 @@ NR_PHP_WRAPPER_START(nr_guzzle6_client_construct) {
378369
*/
379370
if (NULL == version) {
380371
version = nr_php_get_object_constant(this_var, "MAJOR_VERSION");
381-
nr_fw_support_add_package_supportability_metric(
382-
NRPRG(txn), PHP_PACKAGE_NAME, version, p);
383372
}
384373

374+
/* if version is still NULL that is OK this next call will accept
375+
* that value and when supportability metrics are made for
376+
* packages if another method has determined the package version
377+
* (composer api for example) then it will be filled in at that time
378+
*/
379+
nr_txn_suggest_package_supportability_metric(NRPRG(txn), PHP_PACKAGE_NAME,
380+
version);
385381
nr_free(version);
386382

387383
(void)wraprec;

agent/lib_mongodb.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,8 @@ void nr_mongodb_enable() {
449449
if (NRINI(vulnerability_management_package_detection_enabled)) {
450450
nr_txn_add_php_package(NRPRG(txn), PHP_PACKAGE_NAME,
451451
PHP_PACKAGE_VERSION_UNKNOWN);
452-
nr_txn_php_package_set_options(NRPRG(txn), PHP_PACKAGE_NAME,
453-
NR_PHP_PACKAGE_OPTION_MAJOR_METRIC);
454452
}
453+
454+
nr_txn_suggest_package_supportability_metric(NRPRG(txn), PHP_PACKAGE_NAME,
455+
PHP_PACKAGE_VERSION_UNKNOWN);
455456
}

agent/lib_monolog.c

+5-13
Original file line numberDiff line numberDiff line change
@@ -378,10 +378,8 @@ NR_PHP_WRAPPER(nr_monolog_logger_addrecord) {
378378
= nr_monolog_get_timestamp(api, argc, NR_EXECUTE_ORIG_ARGS TSRMLS_CC);
379379
char version[MAJOR_VERSION_LENGTH];
380380
snprintf(version, sizeof(version), "%d", api);
381-
nr_fw_support_add_package_supportability_metric(
382-
NRPRG(txn), PHP_PACKAGE_NAME, version,
383-
nr_php_packages_get_package(NRPRG(txn)->php_packages,
384-
PHP_PACKAGE_NAME));
381+
nr_txn_suggest_package_supportability_metric(NRPRG(txn), PHP_PACKAGE_NAME,
382+
version);
385383
}
386384

387385
/* Record the log event */
@@ -524,14 +522,8 @@ void nr_monolog_enable(TSRMLS_D) {
524522
if (NRINI(vulnerability_management_package_detection_enabled)) {
525523
nr_txn_add_php_package(NRPRG(txn), PHP_PACKAGE_NAME,
526524
PHP_PACKAGE_VERSION_UNKNOWN);
527-
/* Usually we would set the package major metric option here, but legacy
528-
* VM detection will get the version from the logger API constant
529-
* and we create the package major metric then as it will work even
530-
* if the composer API is not being used for VM.
531-
*
532-
* The version detection is done in the addRecord wrapper and only
533-
* returns a major number, so this value is not stored in the PHP
534-
* package record for monolog. This is different than most packages.
535-
*/
536525
}
526+
527+
nr_txn_suggest_package_supportability_metric(NRPRG(txn), PHP_PACKAGE_NAME,
528+
PHP_PACKAGE_VERSION_UNKNOWN);
537529
}

agent/lib_predis.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -653,10 +653,11 @@ NR_PHP_WRAPPER(nr_predis_client_construct) {
653653
if (NRINI(vulnerability_management_package_detection_enabled)) {
654654
// Add php package to transaction
655655
nr_txn_add_php_package(NRPRG(txn), PHP_PACKAGE_NAME, version);
656-
nr_txn_php_package_set_options(NRPRG(txn), PHP_PACKAGE_NAME,
657-
NR_PHP_PACKAGE_OPTION_MAJOR_METRIC);
658656
}
659657

658+
nr_txn_suggest_package_supportability_metric(NRPRG(txn), PHP_PACKAGE_NAME,
659+
version);
660+
660661
nr_free(version);
661662

662663
/*

agent/php_txn.c

+18-12
Original file line numberDiff line numberDiff line change
@@ -718,32 +718,38 @@ static void nr_php_txn_php_package_create_major_metric(void* value,
718718
size_t key_len,
719719
void* user_data) {
720720
nrtxn_t* txn = (nrtxn_t*)user_data;
721-
nr_php_package_t* package = value;
721+
nr_php_package_t* suggested = value;
722+
nr_php_package_t* actual = NULL;
722723

723724
(void)key;
724725
(void)key_len;
725726

726-
if (NULL == package) {
727+
if (NULL == suggested) {
727728
return;
728729
}
729730

730-
if (package->options & NR_PHP_PACKAGE_OPTION_MAJOR_METRIC) {
731-
nrl_verbosedebug(NRL_INSTRUMENT,
732-
"Creating PHP Package Supportability Metric for package "
733-
"'%s', version '%s'",
734-
NRSAFESTR(package->package_name),
735-
NRSAFESTR(package->package_version));
736-
nr_fw_support_add_package_supportability_metric(
737-
txn, package->package_name, package->package_version, NULL);
738-
}
731+
/* see if the actual packages has a version we can use over the
732+
* one provided with the suggested package
733+
*/
734+
actual
735+
= nr_php_packages_get_package(txn->php_packages, suggested->package_name);
736+
737+
nrl_verbosedebug(NRL_INSTRUMENT,
738+
"Creating PHP Package Supportability Metric for package "
739+
"'%s', suggested version '%s', actual version '%s'",
740+
NRSAFESTR(suggested->package_name),
741+
NRSAFESTR(suggested->package_version),
742+
NRSAFESTR(actual ? actual->package_version : ""));
743+
nr_fw_support_add_package_supportability_metric(
744+
txn, suggested->package_name, suggested->package_version, actual);
739745
}
740746

741747
void nr_php_txn_create_packages_major_metrics(nrtxn_t* txn) {
742748
if (NULL == txn) {
743749
return;
744750
}
745751

746-
nr_php_packages_iterate(txn->php_packages,
752+
nr_php_packages_iterate(txn->php_package_suggestions,
747753
nr_php_txn_php_package_create_major_metric, txn);
748754
}
749755

axiom/nr_php_packages.c

+7-59
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ typedef struct {
2525

2626
static inline const char* nr_php_package_source_priority_to_string(const nr_php_package_source_priority_t source_priority) {
2727
switch (source_priority) {
28+
case NR_PHP_PACKAGE_SOURCE_SUGGESTION:
29+
return "suggestion";
2830
case NR_PHP_PACKAGE_SOURCE_LEGACY:
2931
return "legacy";
3032
case NR_PHP_PACKAGE_SOURCE_COMPOSER:
@@ -34,7 +36,10 @@ static inline const char* nr_php_package_source_priority_to_string(const nr_php_
3436
}
3537
}
3638

37-
nr_php_package_t* nr_php_package_create_with_source(char* name, char* version, const nr_php_package_source_priority_t source_priority) {
39+
nr_php_package_t* nr_php_package_create_with_source(
40+
const char* name,
41+
const char* version,
42+
const nr_php_package_source_priority_t source_priority) {
3843
nr_php_package_t* p = NULL;
3944

4045
if (NULL == name) {
@@ -55,14 +60,13 @@ nr_php_package_t* nr_php_package_create_with_source(char* name, char* version, c
5560
// string with a space according to spec
5661
}
5762
p->source_priority = source_priority;
58-
p->options = 0;
5963

6064
nrl_verbosedebug(NRL_INSTRUMENT, "Creating PHP Package '%s', version '%s', source %s",
6165
p->package_name, p->package_version, nr_php_package_source_priority_to_string(source_priority));
6266
return p;
6367
}
6468

65-
nr_php_package_t* nr_php_package_create(char* name, char* version) {
69+
nr_php_package_t* nr_php_package_create(const char* name, const char* version) {
6670
return nr_php_package_create_with_source(name, version, NR_PHP_PACKAGE_SOURCE_LEGACY);
6771
}
6872

@@ -74,20 +78,6 @@ void nr_php_package_destroy(nr_php_package_t* p) {
7478
}
7579
}
7680

77-
void nr_php_package_set_options(nr_php_package_t* p,
78-
nr_php_package_options_t options) {
79-
if (NULL != p) {
80-
p->options = options;
81-
}
82-
}
83-
84-
nr_php_package_options_t nr_php_package_get_options(nr_php_package_t* p) {
85-
if (NULL == p) {
86-
return 0;
87-
}
88-
return p->options;
89-
}
90-
9181
nr_php_packages_t* nr_php_packages_create() {
9282
nr_php_packages_t* h = NULL;
9383
h = (nr_php_packages_t*)nr_malloc(sizeof(nr_php_packages_t));
@@ -131,48 +121,6 @@ nr_php_package_t* nr_php_packages_add_package(nr_php_packages_t* h,
131121
return p;
132122
}
133123

134-
void nr_php_packages_set_package_options(nr_php_packages_t* h,
135-
const char* package_name,
136-
nr_php_package_options_t options) {
137-
nr_php_package_t* package;
138-
139-
if (NULL == h) {
140-
return;
141-
}
142-
143-
if (nr_strempty(package_name)) {
144-
return;
145-
}
146-
147-
package = nr_php_packages_get_package(h, package_name);
148-
if (NULL == package) {
149-
return;
150-
}
151-
152-
nr_php_package_set_options(package, options);
153-
}
154-
155-
nr_php_package_options_t nr_php_packages_get_package_options(
156-
nr_php_packages_t* h,
157-
const char* package_name) {
158-
nr_php_package_t* package;
159-
160-
if (NULL == h) {
161-
return 0;
162-
}
163-
164-
if (nr_strempty(package_name)) {
165-
return 0;
166-
}
167-
168-
package = nr_php_packages_get_package(h, package_name);
169-
if (NULL == package) {
170-
return 0;
171-
}
172-
173-
return nr_php_package_get_options(package);
174-
}
175-
176124
void nr_php_packages_iterate(nr_php_packages_t* packages,
177125
nr_php_packages_iter_t callback,
178126
void* userdata) {

0 commit comments

Comments
 (0)