open-telemetry / opentelemetry-php

The OpenTelemetry PHP Library
https://opentelemetry.io/docs/instrumentation/php/
Apache License 2.0
756 stars 187 forks source link

Unable to hook into a PHP extension #1224

Closed gnumoksha closed 10 months ago

gnumoksha commented 10 months ago

I'm trying to create an instrumentation code for the phalcon framework, which is a PHP extension, but the hook never works.

hook(
    \Phalcon\Mvc\Application::class,
    'handle',
    function (
        \Phalcon\Mvc\Application $app,
        array        $params,
        string       $class,
        string       $function,
        ?string      $filename,
        ?int         $lineNumber,
    ) {
        // the following will never be executed
        var_dump($app, $params, $class, $function, $filename, $lineNumber);
    }
);
agoallikmaa commented 10 months ago

Which PHP version are you running this on? Hooking internal functions (such as those provided by an extension) is only supported on PHP 8.2+.

gnumoksha commented 10 months ago

Which PHP version are you running this on? Hooking internal functions (such as those provided by an extension) is only supported on PHP 8.2+.

8.1. I'm working on updating the version.

gnumoksha commented 10 months ago

It worked, thank you

gnumoksha commented 10 months ago

Is the exception catch expected to work in the post hook, @agoallikmaa?

brettmc commented 10 months ago

Pre and post hooks are not expected to throw exceptions - if they do, it will be logged and discarded (as of 1.0.1beta2). If the function being hooked throws an exception, then that exception is available in the post hook.

gnumoksha commented 10 months ago

I've tested throwing an exception in a controller, called by the hooked method, and the post hook didn't receive the exception. Instrumenting a PHP code works as expected (i.e. it receives the exception).

brettmc commented 10 months ago

That should definitely work: https://github.com/open-telemetry/opentelemetry-php-instrumentation/blob/main/ext/tests/007.phpt (the exception will be the 4th parameter to the post hook).