Skip to content

Commit

Permalink
Fixed internal function instrumentation if only pre or post hook is g…
Browse files Browse the repository at this point in the history
…iven.

* phpt test for user and internal functions use cases
  • Loading branch information
intuibase committed Sep 10, 2024
1 parent c116317 commit f47ecef
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 1 deletion.
13 changes: 12 additions & 1 deletion prod/native/extension/code/InternalFunctionInstrumentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ void ZEND_FASTCALL internal_function_handler(INTERNAL_FUNCTION_PARAMETERS) {
}

for (auto &callback : *callbacks) {
if (callback.first.isNull() || callback.first.isUndef()) {
continue;
}

try {
AutomaticExceptionStateRestorer restorer;
callPreHook(callback.first);
Expand All @@ -275,6 +279,10 @@ void ZEND_FASTCALL internal_function_handler(INTERNAL_FUNCTION_PARAMETERS) {
callOriginalHandler(originalHandler, INTERNAL_FUNCTION_PARAM_PASSTHRU);

for (auto &callback : *callbacks) {
if (callback.second.isNull() || callback.second.isUndef()) {
continue;
}

try {
AutomaticExceptionStateRestorer restorer;
callPostHook(callback.second, return_value, restorer.getException(), execute_data);
Expand Down Expand Up @@ -422,7 +430,10 @@ zend_observer_fcall_handlers elasticRegisterObserver(zend_execute_data *execute_

auto callbacks = reinterpret_cast<InstrumentedFunctionHooksStorage_t *>(EAPM_GL(hooksStorage_).get())->find(hash);
if (!callbacks) {
ELOG_TRACE(EAPM_GL(logger_), "elasticRegisterObserver hash: 0x%X, not instrumented", hash);
if (EAPM_GL(logger_)->doesMeetsLevelCondition(LogLevel::logLevel_trace)) {
auto [cls, func] = getClassAndFunctionName(execute_data);
ELOG_TRACE(EAPM_GL(logger_), "elasticRegisterObserver hash: 0x%X " PRsv "::" PRsv ", not instrumented", hash, PRsvArg(cls), PRsvArg(func));
}
return {nullptr, nullptr};
}
ELOG_TRACE(EAPM_GL(logger_), "elasticRegisterObserver hash: 0x%X", hash);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
instrumentation - internal func post hook only
--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);

elastic_otel_hook(NULL, "str_contains", NULL, function () {
echo "*** posthook()\n";
});

var_dump(str_contains("elastic obs", "obs"));

echo "Test completed\n";
?>
--EXPECTF--
*** posthook()
bool(true)
Test completed
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
instrumentation - internal func pre hook only
--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);

elastic_otel_hook(NULL, "str_contains", function () {
echo "*** prehook()\n";
}, NULL);

var_dump(str_contains("elastic obs", "obs"));

echo "Test completed\n";
?>
--EXPECTF--
*** prehook()
bool(true)
Test completed
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
--TEST--
instrumentation - spl_autoload_register
--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);

elastic_otel_hook(NULL, "spl_autoload_register", function () {
echo "*** prehook()\n";
}, function () {
echo "*** posthook()\n";
});





spl_autoload_register(function () { }, true, false);

echo "Test completed\n";
?>
--EXPECTF--
*** prehook()
*** posthook()
Test completed
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
--TEST--
instrumentation - user func - post hook only
--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);



function userspace($arg1, $arg2, $arg3) {
echo "* userspace() called.\n";
}

elastic_otel_hook(NULL, "userspace", NULL, function () {
echo "*** posthook userspace()\n";
});

userspace("first", 2, 3);

echo "Test completed\n";
?>
--EXPECTF--
* userspace() called.
*** posthook userspace()
Test completed
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
--TEST--
instrumentation - user func - pre hook only
--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);



function userspace($arg1, $arg2, $arg3) {
echo "* userspace() called.\n";
}

elastic_otel_hook(NULL, "userspace", function () {
echo "*** prehook userspace()\n";
}, NULL);

userspace("first", 2, 3);

echo "Test completed\n";
?>
--EXPECTF--
*** prehook userspace()
* userspace() called.
Test completed

0 comments on commit f47ecef

Please sign in to comment.