Skip to content

PHP and Laravel traces

With its default settings, OpenTelemetry PHP records the request, the database queries, outgoing HTTP calls and a few Eloquent calls. Laravel events, cache and Redis commands, views and your own code get no span, so a trace can show long stretches of untracked time. These settings fill them. They go on the PHP processes (php-fpm or your container), not on the collector.

Every change below adds spans, except the first, which removes many. Try them on one deployment first and watch the ingested volume for a day.

Each query arrives as four spans: PDO::prepare, PDOStatement::execute, PDOStatement::fetchAll and Laravel’s sql SELECT .... Korai folds the first three into the last (Fold repeated in the trace view), but they still count as ingested spans. To stop sending them:

Terminal window
OTEL_PHP_DISABLED_INSTRUMENTATIONS=pdo

The Laravel query span stays. You lose the connection spans (PDO::__construct); to keep those, drop only PDO::prepare and PDOStatement::fetchAll with a filter processor in your collector instead.

The OpenTelemetry PHP distribution can sample the PHP stack while a request runs and turn what it sees into spans, with their stack trace and the attribute is_inferred. It covers vendor libraries and your own code without changing it. Korai draws them dashed and labelled inferred.

Terminal window
OTEL_PHP_INFERRED_SPANS_ENABLED=true
OTEL_PHP_INFERRED_SPANS_SAMPLING_INTERVAL=20ms
OTEL_PHP_INFERRED_SPANS_MIN_DURATION=10ms

A shorter interval gives more detail and costs more CPU; the minimum duration keeps very short functions out.

Laravel’s cache hits, misses and writes arrive as events on the request span, and the SDK keeps only 128 per span by default: after the 128th, they stop. Raise the limit:

Terminal window
OTEL_SPAN_EVENT_COUNT_LIMIT=1024

Korai draws each event on the span’s bar; Events as rows lists them.

The Laravel instrumentation creates a span per Redis command only when Laravel fires its Redis events. Turn them on once, for example in a service provider’s boot():

Redis::enableEvents();

Mark the methods you care about (block rendering, cache fetches, controllers) and they become spans with that name:

use OpenTelemetry\API\Instrumentation\WithSpan;
#[WithSpan('cms.blocks.hydrate_all')]
public function hydrateAll(): array { /* ... */ }

This needs OTEL_PHP_ATTR_HOOKS_ENABLED=true. If your own libraries already create spans with the OpenTelemetry API (OpenTelemetry\API\...) and those spans never arrive, the distribution is running them against a separate, silent copy of the API: set OTEL_PHP_SCOPED_DEPS_BRIDGE_ENABLED=true and keep your open-telemetry/api version aligned with the distribution’s.

With a low head-sampling ratio, the slow request you are looking for is rarely kept. Raise the ratio on the service (OTEL_TRACES_SAMPLER=parentbased_traceidratio, OTEL_TRACES_SAMPLER_ARG), and add tail sampling in your collector so that slow and failed traces are always kept. Tail sampling only helps if the agent sends more than it keeps.

Korai shows a span’s instrumentation library, and offers Instrumentation as a facet, when the span carries it as an attribute. Copy it in your collector with a transform processor in the traces pipeline:

processors:
transform/scope:
trace_statements:
- context: span
statements:
- set(attributes["otel.scope.name"], instrumentation_scope.name)
  • Two request spans per request. The distribution’s own transaction span and the Laravel one have the same name. Korai folds the outer one, so the trace reads as one request; there is nothing to change.
  • One trace that never ends. A caller that reuses the same traceparent for many requests merges them into one trace of thousands of spans. Make each call start its own trace, or make the caller send a fresh one.
  • Errors. A failure your code handles (a 4xx from a payment gateway, a caught exception) does not mark a span as failed by itself. Where you handle it, record it on the active span ($span->recordException($e) and $span->setStatus(StatusCode::STATUS_ERROR)), so that Explain this trace can follow it.