Skip to content

Commit 4c98d5a

Browse files
committed
feat(agent): add new function that checks for guzzle version
1 parent d04bf87 commit 4c98d5a

File tree

2 files changed

+58
-17
lines changed

2 files changed

+58
-17
lines changed

agent/lib_guzzle_common.c

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,22 @@
88
#include "php_user_instrument.h"
99
#include "php_hash.h"
1010
#include "php_wrapper.h"
11+
#include "php_execute.h"
12+
#include "php_globals.h"
1113
#include "lib_guzzle_common.h"
1214
#include "lib_guzzle4.h"
1315
#include "lib_guzzle6.h"
16+
#include "fw_laravel.h"
17+
#include "fw_laravel_queue.h"
18+
#include "fw_support.h"
19+
#include "php_error.h"
1420
#include "nr_header.h"
1521
#include "util_logging.h"
1622
#include "util_memory.h"
1723
#include "util_strings.h"
1824

25+
int php_version_compare(char*, char*);
26+
1927
char* nr_guzzle_create_async_context_name(const char* prefix, const zval* obj) {
2028
if (!nr_php_is_zval_valid_object(obj)) {
2129
return NULL;
@@ -74,9 +82,44 @@ int nr_guzzle_in_call_stack(TSRMLS_D) {
7482
return in_guzzle;
7583
}
7684

77-
int nr_guzzle_does_zval_implement_has_emitter(zval* obj TSRMLS_DC) {
78-
return nr_php_object_instanceof_class(
79-
obj, "GuzzleHttp\\Event\\HasEmitterInterface" TSRMLS_CC);
85+
extern char* nr_guzzle_version(zval* obj TSRMLS_DC) {
86+
char* retval = NULL;
87+
zval* version = NULL;
88+
zend_class_entry* ce = NULL;
89+
90+
if (0 == nr_php_is_zval_valid_object(obj)) {
91+
nrl_verbosedebug(NRL_FRAMEWORK, "%s: Application object is invalid",
92+
__func__);
93+
return NULL;
94+
}
95+
96+
ce = Z_OBJCE_P(obj);
97+
if (NULL == ce) {
98+
nrl_verbosedebug(NRL_FRAMEWORK, "%s: Application has NULL class entry",
99+
__func__);
100+
return NULL;
101+
}
102+
103+
if (nr_php_get_class_constant(ce, "VERSION") == NULL){
104+
version = nr_php_get_class_constant(ce, "MAJOR_VERSION");
105+
} else {
106+
version = nr_php_get_class_constant(ce, "VERSION");
107+
}
108+
if (NULL == version) {
109+
nrl_verbosedebug(NRL_FRAMEWORK, "%s: Application does not have VERSION",
110+
__func__);
111+
return NULL;
112+
}
113+
114+
if (nr_php_is_zval_valid_string(version)) {
115+
retval = nr_strndup(Z_STRVAL_P(version), Z_STRLEN_P(version));
116+
} else {
117+
nrl_verbosedebug(NRL_FRAMEWORK,
118+
"%s: expected VERSION be a valid string, got type %d",
119+
__func__, Z_TYPE_P(version));
120+
}
121+
nr_php_zval_free(&version);
122+
return retval;
80123
}
81124

82125
nr_segment_t* nr_guzzle_obj_add(const zval* obj,
@@ -272,19 +315,20 @@ char* nr_guzzle_response_get_header(const char* header,
272315
}
273316

274317
NR_PHP_WRAPPER_START(nr_guzzle_client_construct) {
275-
int is_guzzle_45 = 0;
276318
zval* this_var = nr_php_scope_get(NR_EXECUTE_ORIG_ARGS TSRMLS_CC);
319+
char *version = NULL;
320+
version = nr_guzzle_version(this_var TSRMLS_CC);
277321

278322
(void)wraprec;
279323
NR_UNUSED_SPECIALFN;
280-
281-
is_guzzle_45 = nr_guzzle_does_zval_implement_has_emitter(this_var TSRMLS_CC);
282324
nr_php_scope_release(&this_var);
283-
284-
if (is_guzzle_45) {
285-
NR_PHP_WRAPPER_DELEGATE(nr_guzzle4_client_construct);
286-
} else {
325+
326+
if (php_version_compare(version, "7") >= 0){
327+
NR_PHP_WRAPPER_DELEGATE(nr_guzzle7_client_construct);
328+
} else if (php_version_compare(version, "6") >= 0) {
287329
NR_PHP_WRAPPER_DELEGATE(nr_guzzle6_client_construct);
330+
} else{
331+
NR_PHP_WRAPPER_DELEGATE(nr_guzzle4_client_construct);
288332
}
289333
}
290334
NR_PHP_WRAPPER_END

agent/lib_guzzle_common.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,13 @@ extern char* nr_guzzle_create_async_context_name(const char* prefix,
3131
extern int nr_guzzle_in_call_stack(TSRMLS_D);
3232

3333
/*
34-
* Purpose : Checks if the given object implements
35-
* GuzzleHttp\Event\HasEmitterInterface. For a Client object, this
36-
* indicates that the object is from Guzzle 4 or 5.
34+
* Purpose: This function checks which guzzle version is being used by the object
3735
*
3836
* Params : 1. The object to check.
39-
*
40-
* Returns : Non-zero if the object does implement the interface; zero
41-
* otherwise.
37+
*
38+
* Returns : A string indicating the guzzle version being used
4239
*/
43-
extern int nr_guzzle_does_zval_implement_has_emitter(zval* obj TSRMLS_DC);
40+
extern char* nr_guzzle_version(zval* obj TSRMLS_DC);
4441

4542
/*
4643
* Purpose : Adds a Guzzle Request object to the hashmap containing all active

0 commit comments

Comments
 (0)