Skip to content

Commit

Permalink
Tests of instrumentation of functions/methods in namespace (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
intuibase authored Feb 12, 2025
1 parent 3b36417 commit ebbf97a
Show file tree
Hide file tree
Showing 2 changed files with 206 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
--TEST--
instrumentation - user func in namespace
--ENV--
ELASTIC_OTEL_LOG_LEVEL_STDERR=INFO
--INI--
extension=/elastic/elastic_otel_php.so
elastic_otel.bootstrap_php_part_file={PWD}/includes/bootstrap_mock.inc
--FILE--
<?php
declare(strict_types=1);


namespace SomeNameSpace;

function userspace($arg1, $arg2, $arg3) {
echo "* userspace() body start.\n";
echo "args:\n";
var_dump(func_get_args());
echo "* userspace() body end\n";
return "userspace_rv";
}

elastic_otel_hook(null, "somenamespace\\userspace", function () {
echo "*** prehook userspace()\n";
echo "args:\n";
var_dump(func_get_args());
}, function () {
echo "*** posthook userspace()\n";
echo "args:\n";
var_dump(func_get_args());
});

var_dump(userspace("first", 2, 3));

echo "Test completed\n";
?>
--EXPECTF--
*** prehook userspace()
args:
array(6) {
[0]=>
NULL
[1]=>
array(3) {
[0]=>
string(5) "first"
[1]=>
int(2)
[2]=>
int(3)
}
[2]=>
NULL
[3]=>
string(%d) "SomeNameSpace\userspace"
[4]=>
string(%d) "%a/instrumentation_user_func_namespace.php"
[5]=>
int(7)
}
* userspace() body start.
args:
array(3) {
[0]=>
string(5) "first"
[1]=>
int(2)
[2]=>
int(3)
}
* userspace() body end
*** posthook userspace()
args:
array(8) {
[0]=>
NULL
[1]=>
array(3) {
[0]=>
string(5) "first"
[1]=>
int(2)
[2]=>
int(3)
}
[2]=>
string(12) "userspace_rv"
[3]=>
NULL
[4]=>
NULL
[5]=>
string(%d) "SomeNameSpace\userspace"
[6]=>
string(%d) "%a/instrumentation_user_func_namespace.php"
[7]=>
int(7)
}
string(12) "userspace_rv"
Test completed
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
--TEST--
instrumentation - user method in namespace - args post processing and return value replacement
--ENV--
ELASTIC_OTEL_LOG_LEVEL_STDERR=INFO
--INI--
extension=/elastic/elastic_otel_php.so
elastic_otel.bootstrap_php_part_file={PWD}/includes/bootstrap_mock.inc
--FILE--
<?php
declare(strict_types=1);

namespace SomeNameSpace;
class TestClass {
function userspace($arg1, $arg2, $arg3) {
echo "* userspace() body start.\n";
echo "args:\n";
var_dump(func_get_args());
echo "* userspace() body end\n";
return "userspace_rv";
}
}

elastic_otel_hook("somenamespace\\testclass", "userspace", function () {
echo "*** prehook userspace()\n";
echo "args:\n";
var_dump(func_get_args());
return [0 => "replaced_first_argument"];
}, function () : string {
echo "*** posthook userspace()\n";
echo "args:\n";
var_dump(func_get_args());
return "replaced_return_value";
});

$obj = new TestClass;

var_dump($obj->userspace("first", 2, 3));

echo "Test completed\n";
?>
--EXPECTF--
*** prehook userspace()
args:
array(6) {
[0]=>
object(SomeNameSpace\TestClass)#%d (0) {
}
[1]=>
array(3) {
[0]=>
string(5) "first"
[1]=>
int(2)
[2]=>
int(3)
}
[2]=>
string(23) "SomeNameSpace\TestClass"
[3]=>
string(9) "userspace"
[4]=>
string(%d) "%a/instrumentation_user_method_namespace.php"
[5]=>
int(6)
}
* userspace() body start.
args:
array(3) {
[0]=>
string(23) "replaced_first_argument"
[1]=>
int(2)
[2]=>
int(3)
}
* userspace() body end
*** posthook userspace()
args:
array(8) {
[0]=>
object(SomeNameSpace\TestClass)#%d (0) {
}
[1]=>
array(3) {
[0]=>
string(23) "replaced_first_argument"
[1]=>
int(2)
[2]=>
int(3)
}
[2]=>
string(12) "userspace_rv"
[3]=>
NULL
[4]=>
string(23) "SomeNameSpace\TestClass"
[5]=>
string(9) "userspace"
[6]=>
string(%d) "%a/instrumentation_user_method_namespace.php"
[7]=>
int(6)
}
string(21) "replaced_return_value"
Test completed

0 comments on commit ebbf97a

Please sign in to comment.