#Tracing in PHP with OpenTelemetry: spans, attributes and errors
Let's say a nightly script imports orders: it downloads a file, validates the rows and saves them. Most nights it takes a few seconds. Some nights it takes ten minutes, and nobody knows which step is slow.
Doesn't look like a complicated task, right?
#Measure a PHP script with hrtime
The first idea is always a timer around every step.
<?php
declare(strict_types=1);
function download(): array
{
usleep(120_000);
return range(1, 500);
}
function validate(array $orders): array
{
usleep(30_000);
return $orders;
}
function save(array $orders): void
{
usleep(250_000);
}
$start = hrtime(true);
$orders = download();
printf("download: %dms\n", (hrtime(true) - $start) / 1e6);
$start = hrtime(true);
$orders = validate($orders);
printf("validate: %dms\n", (hrtime(true) - $start) / 1e6);
$start = hrtime(true);
save($orders);
printf("save: %dms\n", (hrtime(true) - $start) / 1e6);
download: 125ms
validate: 35ms
save: 250ms
It works, and for one script on our machine it's enough.
#Why timers are not enough
The slow night happens on the server, not on our machine.
There these three lines are somewhere in a log file, between the lines of other runs, and nothing connects them to each other. When save calls two more functions, we add two more timers and indent the output by hand. When it throws, the timer line is never printed.
What we want has a name. A span is one step with a start, a duration and details. A trace is all the spans of one run, connected by one id. OpenTelemetry is the standard for both, and every monitoring tool reads it.
#Trace a PHP script with OpenTelemetry
flow-php/telemetry is an independent implementation of OpenTelemetry, written in PHP. It needs PHP 8.3 or newer, no extension, and it is not a wrapper around the official SDK.
composer require flow-php/telemetry flow-php/etl
<?php
declare(strict_types=1);
use function Flow\ETL\DSL\clock;
use function Flow\Telemetry\DSL\{
console_exporter,
memory_context_storage,
pass_through_span_processor,
resource,
telemetry,
tracer_provider,
};
require __DIR__ . '/vendor/autoload.php';
function download(): array
{
usleep(120_000);
return range(1, 500);
}
function validate(array $orders): array
{
usleep(30_000);
return $orders;
}
function save(array $orders): void
{
usleep(250_000);
}
$telemetry = telemetry(
resource(['service.name' => 'nightly-import']),
tracer_provider(
pass_through_span_processor(console_exporter(colors: false)),
clock(),
memory_context_storage(),
),
);
$tracer = $telemetry->tracer('import');
$tracer->trace('import-orders', static function () use ($tracer): void {
$orders = $tracer->trace('download', download(...));
$orders = $tracer->trace('validate', static fn (): array => validate($orders));
$tracer->trace('save', static fn () => save($orders));
});
The script prints four spans. This is the first one, and the last one:
+-------------------------------------------------------------------------------------------+
| SPAN: download |
+-------------------------------------------------------------------------------------------+
| Trace: 7f54bdacc35e3e6f4cd87a31a805e3b9 Span: 38a57c917b46d260 Parent: 88febcef4ffc57db |
| Kind: INTERNAL Status: UNSET Duration: 125.06ms |
| Start: 2026-12-01 18:18:44.243811 |
+-------------------------------------------------------------------------------------------+
| Resource: |
| service.name = nightly-import |
| Scope: import unknown |
+-------------------------------------------------------------------------------------------+
+-----------------------------------------------------------------+
| SPAN: import-orders |
+-----------------------------------------------------------------+
| Trace: 7f54bdacc35e3e6f4cd87a31a805e3b9 Span: 88febcef4ffc57db |
| Kind: INTERNAL Status: UNSET Duration: 408.56ms |
| Start: 2026-12-01 18:18:44.243102 |
+-----------------------------------------------------------------+
| Resource: |
| service.name = nightly-import |
| Scope: import unknown |
+-----------------------------------------------------------------+
Every span has the same trace id, so the four of them belong to one run, and no other run can mix into it.
download has a parent, and that parent is the id of import-orders. We didn't indent anything, the tracer knows which span is open.
trace starts a span, calls our function, and ends the span when the function returns or throws.
console_exporter prints the spans. It's the place we change later to send them to a real tool, the rest of the script stays.
#Add details and errors to a span
A duration says how long. To know why, a span carries attributes, and it remembers the exception that ended it.
<?php
declare(strict_types=1);
use function Flow\ETL\DSL\clock;
use function Flow\Telemetry\DSL\{
console_exporter,
memory_context_storage,
pass_through_span_processor,
resource,
telemetry,
tracer_provider,
};
require __DIR__ . '/vendor/autoload.php';
$telemetry = telemetry(
resource(['service.name' => 'nightly-import']),
tracer_provider(
pass_through_span_processor(console_exporter(colors: false)),
clock(),
memory_context_storage(),
),
);
$tracer = $telemetry->tracer('import');
$span = $tracer->span('save');
$span->setAttribute('orders.count', 500);
$span->setAttribute('db.table', 'orders');
$scope = $tracer->activate($span);
try {
$tracer->trace('insert-batch', static function (): void {
throw new RuntimeException('Connection to the database was lost');
});
} catch (RuntimeException $exception) {
echo 'import failed: ', $exception->getMessage(), "\n";
} finally {
$scope->detach();
$tracer->complete($span);
}
The span of the failed step, with the long lines cut:
| SPAN: insert-batch
| Trace: 91bd69444627a2919048e391e19929ba Span: 578034676f8823ee Parent: 16f0f032bee8d2b3
| Kind: INTERNAL Status: ERROR (Connection to the database was lost) Duration: 287.00us
| Attributes:
| error.type = RuntimeException
| Events (1):
| 18:18:54.815543 exception {"exception.type":"RuntimeException","exception.message":"Connection to the database was lost", ...
And its parent:
| SPAN: save
| Trace: 91bd69444627a2919048e391e19929ba Span: 16f0f032bee8d2b3
| Kind: INTERNAL Status: UNSET Duration: 1.10ms
| Attributes:
| orders.count = 500
| db.table = orders
trace caught the exception and wrote its class, its message and its stack trace into the span.
Then it set the status to ERROR and threw the exception again, so our catch works as before.
span and complete are the long form of trace. We use them when a span needs attributes, or when it doesn't fit into one function.
A new span is not a parent until we activate it, and we detach it before we complete it.
#What to name spans and attributes
- A span name is the step, not the data:
save, notsave 500 orders. The number goes into an attribute. - Attribute names use dots and small letters:
orders.count,db.table. OpenTelemetry has a list of standard names, and tools understand them. service.namein the resource is the name of the whole application. Every span of this script carries it.
#Where to go from here
The console is for our machine. On a server the same spans go to a collector or a monitoring tool over OTLP, the protocol of OpenTelemetry. Only the exporter changes, and it comes from flow-php/telemetry-otlp-bridge. The spans, the attributes and the tracer stay as they are in this guide. The next steps are PHP metrics with OpenTelemetry, PHP logs with a trace id and distributed tracing in PHP. To send the spans to a real tool, read send PHP traces over OTLP.