Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion ext/otel_observer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,16 @@ static void destroy_observer_class_lookup(zval *zv) {

static void add_function_observer(HashTable *ht, zend_string *fn,
zval *pre_hook, zval *post_hook) {
zend_string *lc = zend_string_tolower(fn);
// Strip leading slash if present
zend_string *normalized_fn;
if (ZSTR_LEN(fn) > 0 && ZSTR_VAL(fn)[0] == '\\') {
normalized_fn = zend_string_init(ZSTR_VAL(fn) + 1, ZSTR_LEN(fn) - 1, 0);
} else {
normalized_fn = zend_string_copy(fn);
}
zend_string *lc = zend_string_tolower(normalized_fn);
zend_string_release(normalized_fn);

otel_observer *observer = zend_hash_find_ptr(ht, lc);
if (!observer) {
observer = create_observer();
Expand Down
74 changes: 74 additions & 0 deletions ext/tests/011.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
--TEST--
Check hooking namespaced function
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might also want to add a test for hooking a namespaced class method with leading \ in the classname.

--EXTENSIONS--
opentelemetry
--FILE--
<?php
namespace Some\Namespace;

//with leading slash
\OpenTelemetry\Instrumentation\hook(
null,
'\Some\Namespace\helloWorld',
function(...$args) {
var_dump($args);
}
);

//without leading slash
\OpenTelemetry\Instrumentation\hook(
null,
'Some\Namespace\helloWorld',
function(...$args) {
var_dump($args);
}
);
function helloWorld() {
return 42;
}

\Some\Namespace\helloWorld();
?>
--EXPECTF--
array(8) {
[0]=>
NULL
[1]=>
array(0) {
}
[2]=>
NULL
[3]=>
string(25) "Some\Namespace\helloWorld"
[4]=>
string(%d) "%s011.php"
[5]=>
int(21)
[6]=>
array(0) {
}
[7]=>
array(0) {
}
}
array(8) {
[0]=>
NULL
[1]=>
array(0) {
}
[2]=>
NULL
[3]=>
string(25) "Some\Namespace\helloWorld"
[4]=>
string(%d) "%s011.php"
[5]=>
int(21)
[6]=>
array(0) {
}
[7]=>
array(0) {
}
}