Nevay/opentelemetry-php-instrumentation-1

OpenTelemetry PHP auto-instrumentation extension

★ 0Forks 0CGitHub ↗Compare

README

OpenTelemetry auto-instrumentation extension

This is an experimental extension for OpenTelemetry, to enable auto-instrumentation. It is based on zend_observer and requires php8+

The extension allows creating pre and post hook functions to arbitrary PHP functions and methods, which allows those methods to be wrapped with telemetry.

Requirements

Installation

https://github.com/mlocati/docker-php-extension-installer :

$ install-php-extensions open-telemetry/opentelemetry-php-instrumentation@main

Usage

The following example adds an observer to the DemoClass::run method, and provides two functions which will be run before and after the method call.

The pre method starts and activates a span. The post method ends the span after the observed method has finished.

<?php

$tracer = new Tracer(...);

OpenTelemetry\Instrumentation\hook(
    DemoClass::class,
    'run',
    static function (DemoClass $demo, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($tracer) {
        $tracer->spanBuilder($class)
            ->startSpan()
            ->activate();
    },
    static function (DemoClass $demo, array $params, $returnValue, ?Throwable $exception) use ($tracer) {
        $scope = Context::storage()->scope();
        $scope?->detach();
        $span = Span::fromContext($scope->context());
        $exception && $span->recordException($exception);
        $span->setStatus($exception ? StatusCode::STATUS_ERROR : StatusCode::STATUS_OK);
        $span->end();
    }
);

There are more examples in the tests directory

Contributing

See DEVELOPMENT.md and https://github.com/open-telemetry/opentelemetry-php/blob/main/CONTRIBUTING.md

Contributors

brettmctigrannajaryanNevay

Issues