open-telemetry / opentelemetry-propagation-server-timing
OpenTelemetry server-timing propagator.
0.0.3
2024-08-21 06:43 UTC
Requires
- php: ^8.0
- open-telemetry/context: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3
- guzzlehttp/promises: ^1.5
- nyholm/psr7: ^1.5
- open-telemetry/sdk: ^1.0
- phan/phan: ^5.0
- php-http/message-factory: ^1.0
- phpstan/phpstan: ^1.1
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
- psalm/plugin-phpunit: ^0.18.4
- symfony/http-client: ^5.4|^6.0
- vimeo/psalm: ^4|^5
README
This is a read-only subtree split of https://github.com/open-telemetry/opentelemetry-php-contrib.
OpenTelemetry ServerTiming Propagator
This package provides a Server-Timing propagator to inject the current span context into Response datastructures.
The main goal is to allow client-side technology (Real User Monitoring, HTTP Clients) to record the server side context in order to allow referencing it.
Server-Timing response headers are especially useful for this approach, as they are accessible on the client side, even for the initial page load.
Requirements
- OpenTelemetry SDK and exporters (required to actually export traces)
Optional:
- OpenTelemetry extension (Some instrumentations can automatically use the
TraceResponsePropagator
)
Usage
Assuming there is an active SpanContext
, you can inject it into your response as follows:
// your framework probably provides a datastructure to model HTTP responses // and allows you to hook into the end of a request / listen to a matching event. $response = new Response(); // get the current scope, bail out if none $scope = Context::storage()->scope(); if (null === $scope) { return; } // create a PropagationSetterInterface that knows how to inject response headers $propagationSetter = new class implements OpenTelemetry\Context\Propagation\PropagationSetterInterface { public function set(&$carrier, string $key, string $value) : void { $carrier->headers->set($key, $value); } }; $propagator = new ServerTimingPropagator(); $propagator->inject($response, $propagationSetter, $scope->context());
Installation via composer
$ composer require open-telemetry/opentelemetry-propagation-server-timing
Installing dependencies and executing tests
From TraceResponse subdirectory:
$ composer install $ ./vendor/bin/phpunit tests