Skip to content

Commit 37ad626

Browse files
committed
feat: add per process composer package sampling
1 parent 5fcf9c7 commit 37ad626

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

agent/lib_composer.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "fw_hooks.h"
88
#include "fw_support.h"
99
#include "nr_txn.h"
10+
#include "php_globals.h"
1011
#include "util_logging.h"
1112
#include "util_memory.h"
1213
#include "util_syscalls.h"
@@ -136,6 +137,13 @@ static void nr_execute_handle_autoload_composer_get_packages_information(
136137
__func__);
137138
return;
138139
}
140+
141+
if (NR_PHP_PROCESS_GLOBALS(composer_api_per_process_detection)) {
142+
// set the per-process flag to true to avoid re-running composer api
143+
// detection when the per-process detection is enabled.
144+
NR_PHP_PROCESS_GLOBALS(composer_packages_detected) = 1;
145+
}
146+
139147
if (IS_ARRAY == Z_TYPE(retval)) {
140148
zend_string* package_name = NULL;
141149
zval* package_version = NULL;

agent/php_globals.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ typedef struct _nrphpglobals_t {
7575
int apache_threaded; /* 1 if a threaded MPM is in use, 0 otherwise */
7676
int preload_framework_library_detection; /* Enables preloading framework and
7777
library detection */
78+
int composer_api_per_process_detection; /* Enables per-process VM package
79+
detection when Composer API is also
80+
enabled */
81+
int composer_packages_detected; /* Flag to indicate that Composer package
82+
detection has run. Used in conjunction with
83+
composer_api_per_process_detection. */
7884
char* docker_id; /* 64 byte hex docker ID parsed from /proc/self/mountinfo */
7985

8086
/* Original PHP callback pointer contents */

agent/php_minit.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ PHP_MINIT_FUNCTION(newrelic) {
456456
= nr_php_check_for_upgrade_license_key();
457457
NR_PHP_PROCESS_GLOBALS(high_security) = 0;
458458
NR_PHP_PROCESS_GLOBALS(preload_framework_library_detection) = 1;
459+
NR_PHP_PROCESS_GLOBALS(composer_packages_detected) = 0;
459460
nr_php_populate_apache_process_globals();
460461
nr_php_api_distributed_trace_register_userland_class(TSRMLS_C);
461462
/*

agent/php_nrini.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,28 @@ static PHP_INI_MH(nr_preload_framework_library_detection_mh) {
537537
return SUCCESS;
538538
}
539539

540+
static PHP_INI_MH(nr_composer_per_process_detection_mh) {
541+
int val;
542+
543+
(void)entry;
544+
(void)NEW_VALUE_LEN;
545+
(void)mh_arg1;
546+
(void)mh_arg2;
547+
(void)mh_arg3;
548+
(void)stage;
549+
NR_UNUSED_TSRMLS;
550+
551+
val = nr_bool_from_str(NEW_VALUE);
552+
553+
if (-1 == val) {
554+
return FAILURE;
555+
}
556+
557+
NR_PHP_PROCESS_GLOBALS(composer_api_per_process_detection) = val ? 1 : 0;
558+
559+
return SUCCESS;
560+
}
561+
540562
static PHP_INI_MH(nr_loglevel_mh) {
541563
nr_status_t rv;
542564

@@ -2055,6 +2077,17 @@ PHP_INI_ENTRY_EX("newrelic.preload_framework_library_detection",
20552077
nr_preload_framework_library_detection_mh,
20562078
0)
20572079

2080+
/*
2081+
* Enables per-process Composer API package detection and reporting. Depends on
2082+
* newrelic.vulnerability_management.composer_api.enabled.
2083+
*/
2084+
PHP_INI_ENTRY_EX(
2085+
"newrelic.vulnerability_management.composer_api.per_process_detection",
2086+
"1",
2087+
NR_PHP_SYSTEM,
2088+
nr_composer_per_process_detection_mh,
2089+
0)
2090+
20582091
/*
20592092
* Daemon
20602093
*/

0 commit comments

Comments
 (0)