Automatic Instrumentation

Automatic instrumentation with PHP requires at least PHP 8.0, and the opentelemetry PHP extension. The extension allows developers code to hook into classes and methods, and execute userland code before and after.


    'class': DemoClass::class,
    'function': 'run',
    'pre': static function (DemoClass $demo, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($tracer) {
        static $instrumentation;
        $instrumentation ??= new CachedInstrumentation('example');
    'post': static function (DemoClass $demo, array $params, $returnValue, ?Throwable $exception) use ($tracer) {
        $scope = Context::storage()->scope();
        $span = Span::fromContext($scope->context());
        if ($exception) {

$demo = new DemoClass();

Here, we provide pre and post functions, which are executed before and after DemoClass::run. The pre function starts and activates a span, and the post function ends it.


  1. Install the extension via pickle or php-extension-installer (docker specific):

    • pickle can be used to install extensions that are available via, however that’s not the case for opentelemetry-php-instrumentation yet, so the only way for it is to install directly from source code. The following command line shows you how to do that using a specific version of the extension (1.0.0beta2 in this case):

      Installing from source requires proper development environment and few dependencies:

       sudo apt-get install gcc make autoconf 
       brew install gcc make autoconf 

      With your environment setup you can install the extension:

      $ php pickle.phar install --source

      Add the extension to your php.ini file:

      [Otel instrumentation]
    • php-extension-installer

      $ install-php-extensions open-telemetry/opentelemetry-php-instrumentation@main
  2. Verify that the extension is installed and enabled:

    $ php -m | grep  otel_instrumentation

Zero-code configuration for automatic instrumentation

When used in conjunction with the OpenTelemetry SDK, you can use environment variables or php.ini to configure auto-instrumentation:

OTEL_SERVICE_NAME=your-service-name \
OTEL_EXPORTER_OTLP_ENDPOINT=http://collector:4317 \
OTEL_PROPAGATORS=baggage,tracecontext \
php myapp.php

Manual setup for automatic instrumentation

OpenTelemetry\API\Common\Instrumentation\Globals::registerInitializer(function (Configurator $configurator) {
    $propagator = TextMapPropagator::getInstance();
    $spanProcessor = new BatchSpanProcessor(/*params*/);
    $tracerProvider = (new TracerProviderBuilder())
        ->setSampler(new ParentBased(new AlwaysOnSampler());)

    ShutdownHandler::register([$tracerProvider, 'shutdown']);

    return $configurator

//instrumentation libraries can access the configured providers (or a no-op implementation) via `Globals`
$tracer = Globals::tracerProvider()->getTracer('example');
//or, via CachedInstrumentation
$instrumentation = new CachedInstrumentation('example');
$tracerProvider = $instrumentation->tracer();

Supported libraries and frameworks

Automatic Instrumentation comes with a number of instrumentation libraries for commonly used PHP libraries. For the full list, see instrumentation libraries on packagist.

Next steps

After you have automatic instrumentation configured for your app or service, you might want to add manual instrumentation to collect custom telemetry data.