Propagation

Context propagation for the PHP API

With context propagation, Signals can be correlated with each other, regardless of where they are generated. Although not limited to tracing, context propagation allows traces to build causal information about a system across services that are arbitrarily distributed across process and network boundaries.

For the vast majority of use cases, libraries that natively support OpenTelemetry or instrumentation libraries will automatically propagate trace context across services for you. It is only in rare cases that you will need to propagate context manually.

To learn more, see Context propagation.

Propagation is the mechanism that moves data between services and processes. Although not limited to tracing, propagation allows traces to build causal information about a system across services that are arbitrarily distributed across process and network boundaries.

OpenTelemetry provides a text-based approach to propagate context to remote services using the W3C Trace Context HTTP headers.

Automatic context propagation

Auto-instrumentation exists for some popular PHP frameworks, such as Symfony, Laravel, or Slim. HTTP libraries propagate context for incoming and outgoing HTTP requests.

Incoming requests

Auto-instrumentation for frameworks which implement the PSR-15 RequestHandlerInterface automatically extract W3C tracecontext headers, create a root span, and set a remote parent for the root span.

composer require open-telemetry/opentelemetry-auto-psr15

Outgoing requests

PSR-18 auto-instrumentation automatically apply W3C tracecontext headers to outgoing HTTP requests for any library which implements the PSR-18 interface.

open-telemetry/opentelemetry-auto-psr18

Manual context propagation

In some cases, it is not possible to propagate context using an instrumentation library. There might not be an instrumentation library that matches a library you’re using to have services communicate with each other. Or you might have requirements that instrumentation libraries cannot fulfill, even if they exist.

When you must propagate context manually, use the context API.

The following snippet shows an example of an outgoing HTTP request:

$request = new Request('GET', 'http://localhost:8080/resource');
$outgoing = $tracer->spanBuilder('/resource')->setSpanKind(SpanKind::CLIENT)->startSpan();
$outgoing->setAttribute(TraceAttributes::HTTP_METHOD, $request->getMethod());
$outgoing->setAttribute(TraceAttributes::HTTP_URL, (string) $request->getUri());

$carrier = [];
TraceContextPropagator::getInstance()->inject($carrier);
foreach ($carrier as $name => $value) {
    $request = $request->withAddedHeader($name, $value);
}
try {
    $response = $client->send($request);
} finally {
    $outgoing->end();
}

Similarly, use the text-based approach to read the W3C Trace Context from incoming requests. The following presents an example of processing an incoming HTTP request:

$request = ServerRequestCreator::createFromGlobals();
$context = TraceContextPropagator::getInstance()->extract($request->getHeaders());
$root = $tracer->spanBuilder('HTTP ' . $request->getMethod())
    ->setStartTimestamp((int) ($request->getServerParams()['REQUEST_TIME_FLOAT'] * 1e9))
    ->setParent($context)
    ->setSpanKind(SpanKind::KIND_SERVER)
    ->startSpan();
$scope = $root->activate();
try {
    /* do stuff */
} finally {
    $root->end();
    $scope->detach();
}

Next steps

To learn more about propagation, read the Propagators API specification.


Dernière modification July 9, 2024: [PHP] Edit propagation doc (#4802) (b9fcde2d)