tbachert/otel-sdk

OpenTelemetry SDK

Installs: 62

Dependents: 3

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:metapackage

pkg:composer/tbachert/otel-sdk

dev-main / 0.1.x-dev 2026-01-27 16:40 UTC

This package is auto-updated.

Last update: 2026-01-27 17:45:16 UTC


README

Asynchronous OpenTelemetry SDK based on Revolt.

This metapackage contains the basic components that are required for creating traces/metrics/logs through the official OpenTelemetry API and sending them to an OTLP/HTTP compatible collector1. Additional components (e.g. non-OTLP exporters) can be installed as separate packages.

Projects using ReactPHP libraries can use this SDK together with revolt/event-loop-adapter-react.

Installation

composer require tbachert/otel-sdk

Usage

Refer to the official OpenTelemetry documentation for general usage of the OpenTelemetry API.

Initialization from configuration file

$config = Config::loadFile(__DIR__ . '/sdk-config.yaml');
Automatic initialization from configuration file

The OTEL_EXPERIMENTAL_CONFIG_FILE environment variable can be set to initialize the Global instances on startup.

Initialization from environment variables

$config = Config::loadFromEnv();
Automatic initialization from environment variables

The OTEL_PHP_AUTOLOAD_ENABLED environment variable can be set to true to initialize the Global instances on startup.

Manual SDK initialization

$resource = Resource::create(['foo' => 'bar']);

$tracerProvider = (new TracerProviderBuilder())
    ->setResource($resource)
    ->addSpanProcessor(new BatchSpanProcessor(new OtlpStreamSpanExporter(getStdout())))
    ->build($logger);
$meterProvider = (new MeterProviderBuilder())
    ->setResource($resource)
    ->addMetricReader(new PeriodicExportingMetricReader(new OtlpStreamMetricExporter(getStdout())))
    ->build($logger);
$loggerProvider = (new LoggerProviderBuilder())
    ->setResource($resource)
    ->addLogRecordProcessor(new BatchLogRecordProcessor(new OtlpStreamLogRecordExporter(getStdout())))
    ->build($logger);
$cancellation = new TimeoutCancellation(10);
await([
    async($tracerProvider->shutdown(...), $cancellation),
    async($meterProvider->shutdown(...), $cancellation),
    async($loggerProvider->shutdown(...), $cancellation),
]);

Footnotes

  1. It is highly recommended to install the ext-protobuf extension if using one of the OTLP exporters due to its significantly better performance.