Search by

debuss-a / piccolo-opentelemetry

debuss-a

OpenTelemetry for Piccolo: traces, metrics and logs of the requests, stored procedures and HTTP calls, exported by batch over OTLP, made for long-running PHP workers.

Package info

github.com/debuss/piccolo-opentelemetry

pkg:composer/debuss-a/piccolo-opentelemetry

Statistics

Installs: 12

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

0.1.1 2026-12-04 19:12 UTC

This package is auto-updated.

Last update: 2026-12-04 19:13:17 UTC


README

OpenTelemetry for the Piccolo framework: the traces, metrics and logs of the requests, of the stored procedures and of the HTTP calls, exported over OTLP to any collector (OpenTelemetry Collector, Grafana, Jaeger, maple.dev, ...).

Made for the FrankenPHP workers: nothing is exported during a request, the telemetry is exported by batch after the responses, every N requests. No PHP extension needed.

composer require debuss-a/piccolo-opentelemetry
// config/container.php
$builder->addProvider(new Piccolo\OpenTelemetry\ServiceProvider\OpenTelemetryServiceProvider());

// config/pipeline.php
$app->pipe(TracingMiddleware::class);       // first, to cover the whole pipeline
// ...
$app->pipe(RouteMiddleware::class);
$app->pipe(RouteSpanMiddleware::class);     // right after the routing: the route of the span and of the metrics

Configuration

The standard variables of OpenTelemetry (.env, environment variables or config/configuration.php):

Variable
OTEL_EXPORTER_OTLP_ENDPOINT The collector, e.g. http://collector:4318. The telemetry is disabled without it (no cost)
OTEL_EXPORTER_OTLP_PROTOCOL http/protobuf (default) or http/json; gRPC is not supported
OTEL_EXPORTER_OTLP_HEADERS The headers of the export requests, e.g. authorization=Bearer%20<key> (URL-encoded values)
OTEL_SERVICE_NAME The name of the application, app by default
OTEL_SERVICE_VERSION Its version
OTEL_RESOURCE_ATTRIBUTES Other attributes of the application, e.g. team=api,region=eu
OTEL_TRACES_SAMPLER_ARG The ratio of the traces kept when they start here, from 0 to 1, 1 by default; a trace started by Caddy or a client keeps its decision
OTEL_SDK_DISABLED true to disable the telemetry
OTEL_EXPORT_EVERY_REQUESTS In a worker, export every N requests, 10 by default in production, 1 otherwise
OTEL_EXPORT_AFTER_SECONDS In a worker, export at the latest N seconds after the previous export, 10 by default (0 for never)

The environment (APP_ENV) is the deployment.environment.name of the telemetry.

What is traced

Signal Where What
Server span GET /api/v1/posts[/{id:\d+}] TracingMiddleware, RouteSpanMiddleware Method, path, route, status, client address, user agent; a 5xx is an error, with its exception (ErrorListener, attached to the ErrorHandler and the ProblemDetailsMiddleware). It continues the trace of the traceparent header (Caddy, client)
Client span CALL sp_name ProcedureInterceptor, added to the connections of debuss-a/piccolo-database when installed The database, the procedure; never the values of the parameters
Client span GET TracingHttpClient, a PSR-18 decorator The URL without credentials nor query string, the status; sends the traceparent header to the service called
Spans of the application Tracing $tracing->span('posts.sort', fn (SpanInterface $span) => ..., ['posts.count' => 3])
Logs OpenTelemetryHandler, pushed on the logger of the framework at the level of LOG_LEVEL Related to their trace, with their exception as exception.* attributes. TraceContextProcessor adds trace_id and span_id to the other logs (stderr JSON)
Metrics http.server.request.duration, http.client.request.duration, db.client.operation.duration (histograms in seconds, delta temporality)

The query strings are never recorded: they can hold secrets.

Trace the HTTP calls by decorating the PSR-18 client in config/container.php:

$builder->singleton(ClientInterface::class, static fn (Telemetry $telemetry) =>
    new TracingHttpClient(new Http\Client\Curl\Client(), $telemetry)
);

And the work of the application with Tracing:

public function __construct(private Tracing $tracing) {}

$report = $this->tracing->span('report.build', fn () => $this->build($rows), ['report.rows' => count($rows)]);
$counter = $this->tracing->meter()->createCounter('reports.built');

Worker mode

  • The span of a request is the current span during the request only: the next request of a worker starts with none.
  • Nothing is exported during a request. After a response (when the worker resets the container), the telemetry is exported every OTEL_EXPORT_EVERY_REQUESTS requests, or when the previous export is older than OTEL_EXPORT_AFTER_SECONDS; and a last time when the worker stops. In a classic PHP request, it is exported at the end of the request.
  • A worker cannot run anything between two requests: a worker without request keeps what it did not export yet until its next request or until it stops (MAX_REQUESTS, deployment). With a steady traffic, the batches leave right away; outside production, where the requests are few and spread over the workers, each request is exported by default.
  • The metrics use the delta temporality: each worker exports what changed since its previous export.

With Caddy (FrankenPHP)

With the tracing directive of Caddy, Caddy traces the requests too and exports its spans with the same OTEL_* variables (it supports http/protobuf): its span covers the whole request seen by the HTTP server, and the span of PHP is its child. Named handler by default, give it a clearer name with the FrankenPHP image:

docker run ... -e CADDY_SERVER_EXTRA_DIRECTIVES=$'tracing {\n\tspan "caddy {http.request.method}"\n}' ...

Keep the path out of the name ({http.request.uri.path}): a name per URL prevents grouping the requests.

Automatic instrumentations

The opentelemetry PHP extension is not needed. It allows the automatic instrumentations of open-telemetry/opentelemetry-auto-* (PDO, curl, ...), for the code that cannot be instrumented otherwise.

Tests

composer test     # Pest, with the in-memory exporters of the SDK
composer analyse  # PHPStan, level max

License

MIT