Skip to content

Commit d74fca0

Browse files
committed
Cache composer api packages so these are considered each requst
1 parent f58a459 commit d74fca0

File tree

6 files changed

+89
-0
lines changed

6 files changed

+89
-0
lines changed

agent/lib_composer.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,23 @@ static void nr_execute_handle_autoload_composer_get_packages_information(
162162
}
163163
}
164164
ZEND_HASH_FOREACH_END();
165+
166+
/* cache Composer packages if only running once per process */
167+
if (NR_PHP_PROCESS_GLOBALS(composer_api_per_process_detection)) {
168+
nr_php_packages_destroy(&NR_PHP_PROCESS_GLOBALS(composer_php_packages));
169+
NR_PHP_PROCESS_GLOBALS(composer_php_packages)
170+
= nr_php_packages_clone(NRPRG(txn)->php_packages);
171+
if (NULL == NR_PHP_PROCESS_GLOBALS(composer_php_packages)) {
172+
nrl_verbosedebug(NRL_INSTRUMENT,
173+
"%s - unable to clone composer packages", __func__);
174+
} else {
175+
nrl_verbosedebug(NRL_INSTRUMENT, "%s - cloned %zu composer packages",
176+
__func__,
177+
nr_php_packages_count(
178+
NR_PHP_PROCESS_GLOBALS(composer_php_packages)));
179+
}
180+
}
181+
165182
} else {
166183
char strbuf[80];
167184
nr_format_zval_for_debug(&retval, strbuf, 0, sizeof(strbuf) - 1, 0);

agent/php_globals.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ static void nr_php_per_process_globals_dispose(void) {
4242
nr_free(nr_php_per_process_globals.env_labels);
4343
nr_free(nr_php_per_process_globals.apache_add);
4444
nr_free(nr_php_per_process_globals.docker_id);
45+
nr_php_packages_destroy(&nr_php_per_process_globals.composer_php_packages);
4546

4647
nr_memset(&nr_php_per_process_globals, 0, sizeof(nr_php_per_process_globals));
4748
}

agent/php_globals.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ typedef struct _nrphpglobals_t {
8282
detection has run. Used in conjunction with
8383
composer_api_per_process_detection. */
8484
char* docker_id; /* 64 byte hex docker ID parsed from /proc/self/mountinfo */
85+
nr_php_packages_t* composer_php_packages; /* Cache of PHP packages detected in
86+
the current process. */
8587

8688
/* Original PHP callback pointer contents */
8789
nrphperrfn_t orig_error_cb;

agent/php_txn.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,17 @@ nr_status_t nr_php_txn_begin(const char* appnames,
11511151
}
11521152
}
11531153

1154+
/* preload cached composer packages if present */
1155+
if (NR_PHP_PROCESS_GLOBALS(composer_packages_detected)
1156+
&& (NULL != NR_PHP_PROCESS_GLOBALS(composer_php_packages))) {
1157+
nrl_verbosedebug(NRL_FRAMEWORK,
1158+
"composer packages already detected, using cached values");
1159+
nr_php_packages_destroy(&NRPRG(txn)->php_packages);
1160+
NRPRG(txn)->php_packages
1161+
= nr_php_packages_clone(NR_PHP_PROCESS_GLOBALS(composer_php_packages));
1162+
nrl_verbosedebug(NRL_FRAMEWORK, "composer packages cloned from cache");
1163+
}
1164+
11541165
return NR_SUCCESS;
11551166
}
11561167

axiom/nr_php_packages.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,56 @@ nr_php_packages_t* nr_php_packages_create() {
9292
}
9393
return h;
9494
}
95+
static void clone_callback(void* value,
96+
const char* key,
97+
size_t key_len,
98+
void* userdata) {
99+
nr_php_packages_t* dest = NULL;
100+
nr_php_package_t* orig_pkg = NULL;
101+
nr_php_package_t* new_pkg = NULL;
102+
103+
(void)key_len; // Unused parameter
104+
(void)key; // Unused parameter
105+
106+
if (NULL == value || NULL == userdata) {
107+
return;
108+
}
109+
110+
if (NULL == ((nr_php_packages_t*)userdata)->data) {
111+
return;
112+
}
113+
114+
/* Clone the package and add it to the destination hashmap */
115+
// userdata is a pointer to nr_php_packages_t
116+
// value is a pointer to nr_php_package_t
117+
// we will clone the package and add it to the destination hashmap
118+
dest = (nr_php_packages_t*)userdata;
119+
orig_pkg = (nr_php_package_t*)value;
120+
if (NULL == orig_pkg) {
121+
return;
122+
}
123+
new_pkg = nr_php_package_create_with_source(orig_pkg->package_name,
124+
orig_pkg->package_version,
125+
orig_pkg->source_priority);
126+
nr_php_packages_add_package(dest, new_pkg);
127+
}
128+
129+
nr_php_packages_t* nr_php_packages_clone(nr_php_packages_t* pkgs) {
130+
nr_php_packages_t* h = NULL;
131+
132+
if (NULL == pkgs) {
133+
return NULL;
134+
}
135+
136+
h = nr_php_packages_create();
137+
if (NULL == h) {
138+
return NULL;
139+
}
140+
141+
nr_hashmap_apply(pkgs->data, clone_callback, h);
142+
143+
return h;
144+
}
95145

96146
nr_php_package_t* nr_php_packages_add_package(nr_php_packages_t* h,
97147
nr_php_package_t* p) {

axiom/nr_php_packages.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ extern nr_php_package_t* nr_php_package_create_with_source(
7070
extern nr_php_package_t* nr_php_package_create(const char* name,
7171
const char* version);
7272

73+
/* Purpose : Clone a collection of php packages
74+
*
75+
* Params : 1. A pointer to nr_php_packages_t
76+
*
77+
* Returns : A new nr_php_packages_t that is a copy of the original
78+
*/
79+
nr_php_packages_t* nr_php_packages_clone(nr_php_packages_t* pkgs);
80+
7381
/*
7482
* Purpose : Destroy/free php package
7583
*

0 commit comments

Comments
 (0)