Skip to content

Commit afc6d35

Browse files
bduranleau-nrmfulb
andauthored
feat(agent): composer api package detection on by default (#1055)
Changes the default configuration value for composer-based package detection to `true`. An additional configuration value, `newrelic.vulnerability_management.composer_api.per_process_detection`, has also been added to control the frequency at which composer sampling occurs. By default, this setting will be enabled, only sampling Composer API data once per php-fpm process. This improves baseline performance to make the enablement of Composer API package detection a negligible overhead impact. --------- Co-authored-by: Michael Fulbright <[email protected]>
1 parent ca08b5a commit afc6d35

25 files changed

+104
-4
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_execute.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,13 @@ static void nr_execute_handle_autoload(const char* filename,
904904
return;
905905
}
906906

907+
if (NR_PHP_PROCESS_GLOBALS(composer_api_per_process_detection)
908+
&& NR_PHP_PROCESS_GLOBALS(composer_packages_detected)) {
909+
// do nothing if per-process detection is enabled and the flag to track
910+
// detection is true
911+
return;
912+
}
913+
907914
if (!nr_striendswith(STR_AND_LEN(filename), AUTOLOAD_MAGIC_FILE,
908915
AUTOLOAD_MAGIC_FILE_LEN)) {
909916
// not an autoload file

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: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,33 @@ 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+
nrl_warning(NRL_INIT,
555+
"The value \"%s\" is not valid for the "
556+
"newrelic.vulnerability_management.composer_api.per_process_"
557+
"detection setting, using default value instead.",
558+
NEW_VALUE);
559+
return FAILURE;
560+
}
561+
562+
NR_PHP_PROCESS_GLOBALS(composer_api_per_process_detection) = val ? 1 : 0;
563+
564+
return SUCCESS;
565+
}
566+
540567
static PHP_INI_MH(nr_loglevel_mh) {
541568
nr_status_t rv;
542569

@@ -2055,6 +2082,17 @@ PHP_INI_ENTRY_EX("newrelic.preload_framework_library_detection",
20552082
nr_preload_framework_library_detection_mh,
20562083
0)
20572084

2085+
/*
2086+
* Enables per-process Composer API package detection and reporting. Depends on
2087+
* newrelic.vulnerability_management.composer_api.enabled.
2088+
*/
2089+
PHP_INI_ENTRY_EX(
2090+
"newrelic.vulnerability_management.composer_api.per_process_detection",
2091+
"1",
2092+
NR_PHP_SYSTEM,
2093+
nr_composer_per_process_detection_mh,
2094+
0)
2095+
20582096
/*
20592097
* Daemon
20602098
*/
@@ -3135,7 +3173,7 @@ STD_PHP_INI_ENTRY_EX("newrelic.vulnerability_management.package_detection.enable
31353173
nr_enabled_disabled_dh)
31363174

31373175
STD_PHP_INI_ENTRY_EX("newrelic.vulnerability_management.composer_api.enabled",
3138-
"0",
3176+
"1",
31393177
NR_PHP_REQUEST,
31403178
nr_boolean_mh,
31413179
vulnerability_management_composer_api_enabled,

agent/scripts/newrelic.ini.template

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,11 +1363,21 @@ newrelic.daemon.logfile = "/var/log/newrelic/newrelic-daemon.log"
13631363
; Setting: newrelic.vulnerability_management.composer_api.enabled
13641364
; Type : boolean
13651365
; Scope : per-directory
1366-
; Default: false
1366+
; Default: true
13671367
; Info : Toggles whether the agent should try using Composer's runtime API
13681368
; to gather package information for vulnerability management.
13691369
;
1370-
;newrelic.vulnerability_management.composer_api.enabled = false
1370+
;newrelic.vulnerability_management.composer_api.enabled = true
1371+
1372+
; Setting: newrelic.vulnerability_management.composer_api.per_process_detection
1373+
; Type : boolean
1374+
; Scope : system
1375+
; Default: true
1376+
; Info : Controls the frequency at which Composer API samples the runtime environment
1377+
; for package data. When set to `true`, sampling will only occur once per process.
1378+
; If false, Composer will sample the environment every request, increasing the frequency which this package detection is performed.
1379+
;
1380+
;newrelic.vulnerability_management.composer_api.per_process_detection = true
13711381

13721382
; Setting: newrelic.message_tracer.segment_parameters.enabled
13731383
; Type : boolean

tests/integration/autoloader/test_autoloader_with_composer_disabled.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313

1414
/*INI
15+
newrelic.vulnerability_management.composer_api.enabled = false
1516
*/
1617

1718
/*EXPECT_PHP_PACKAGES null*/

tests/integration/autoloader/test_autoloader_without_composer_disabled.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212

1313
/*INI
14+
newrelic.vulnerability_management.composer_api.enabled = false
1415
*/
1516

1617
/*EXPECT_METRICS_DONT_EXIST

tests/integration/external/guzzle5/test_cat.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
[{"name":"OtherTransactionTotalTime"}, [1, "??", "??", "??", "??", "??"]],
5353
[{"name":"OtherTransactionTotalTime/php__FILE__"}, [1, "??", "??", "??", "??", "??"]],
5454
[{"name":"Supportability/library/Guzzle 4-5/detected"}, [1, 0, 0, 0, 0, 0]],
55+
[{"name":"Supportability/library/Autoloader/detected"}, [1, "??", "??", "??", "??", "??"]],
5556
[{"name":"Supportability/Unsupported/curl_setopt/CURLOPT_HEADERFUNCTION/closure"}, [3, 0, 0, 0, 0, 0]],
5657
[{"name":"Supportability/Logging/Forwarding/PHP/enabled"}, [1, "??", "??", "??", "??", "??"]],
5758
[{"name":"Supportability/Logging/Metrics/PHP/enabled"}, [1, "??", "??", "??", "??", "??"]],

tests/integration/external/guzzle5/test_dt.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
[{"name":"OtherTransactionTotalTime"}, [1, "??", "??", "??", "??", "??"]],
4242
[{"name":"OtherTransactionTotalTime/php__FILE__"}, [1, "??", "??", "??", "??", "??"]],
4343
[{"name":"Supportability/library/Guzzle 4-5/detected"}, [1, 0, 0, 0, 0, 0]],
44+
[{"name":"Supportability/library/Autoloader/detected"}, [1, "??", "??", "??", "??", "??"]],
4445
[{"name":"Supportability/Unsupported/curl_setopt/CURLOPT_HEADERFUNCTION/closure"}, [1, 0, 0, 0, 0, 0]],
4546
[{"name":"DurationByCaller/Unknown/Unknown/Unknown/Unknown/all"}, [1, "??", "??", "??", "??", "??"]],
4647
[{"name":"DurationByCaller/Unknown/Unknown/Unknown/Unknown/allOther"}, [1, "??", "??", "??", "??", "??"]],

0 commit comments

Comments
 (0)