|
| 1 | +[](https://github.com/opentelemetry-php/contrib-propagator-server-timing/releases) |
| 2 | +[](https://github.com/open-telemetry/opentelemetry-php/issues) |
| 3 | +[](https://github.com/open-telemetry/opentelemetry-php-contrib/tree/main/src/Propagation/ServerTiming) |
| 4 | +[](https://github.com/opentelemetry-php/contrib-propagator-server-timing) |
| 5 | +[](https://packagist.org/packages/open-telemetry/opentelemetry-propagation-server-timing/) |
| 6 | +[](https://packagist.org/packages/open-telemetry/opentelemetry-propagation-server-timing/) |
| 7 | + |
| 8 | +This is a read-only subtree split of https://github.com/open-telemetry/opentelemetry-php-contrib. |
| 9 | + |
| 10 | +# OpenTelemetry ServerTiming Propagator |
| 11 | + |
| 12 | +This package provides a [Server-Timing](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Server-Timing) |
| 13 | +propagator to inject the current span context into Response datastructures. |
| 14 | + |
| 15 | +The main goal is to allow client-side technology (Real User Monitoring, HTTP Clients) to record |
| 16 | +the server side context in order to allow referencing it. |
| 17 | + |
| 18 | +Server-Timing response headers are especially useful for this approach, as they are accessible on the client side, |
| 19 | +even for the initial page load. |
| 20 | + |
| 21 | +## Requirements |
| 22 | + |
| 23 | +* OpenTelemetry SDK and exporters (required to actually export traces) |
| 24 | + |
| 25 | +Optional: |
| 26 | +* OpenTelemetry extension (Some instrumentations can automatically use the `TraceResponsePropagator`) |
| 27 | + |
| 28 | +## Usage |
| 29 | + |
| 30 | +Assuming there is an active `SpanContext`, you can inject it into your response as follows: |
| 31 | + |
| 32 | +```php |
| 33 | +// your framework probably provides a datastructure to model HTTP responses |
| 34 | +// and allows you to hook into the end of a request / listen to a matching event. |
| 35 | +$response = new Response(); |
| 36 | + |
| 37 | +// get the current scope, bail out if none |
| 38 | +$scope = Context::storage()->scope(); |
| 39 | +if (null === $scope) { |
| 40 | + return; |
| 41 | +} |
| 42 | + |
| 43 | +// create a PropagationSetterInterface that knows how to inject response headers |
| 44 | +$propagationSetter = new class implements OpenTelemetry\Context\Propagation\PropagationSetterInterface { |
| 45 | + public function set(&$carrier, string $key, string $value) : void { |
| 46 | + $carrier->headers->set($key, $value); |
| 47 | + } |
| 48 | +}; |
| 49 | +$propagator = new ServerTimingPropagator(); |
| 50 | +$propagator->inject($response, $propagationSetter, $scope->context()); |
| 51 | +``` |
| 52 | + |
| 53 | +## Installation via composer |
| 54 | + |
| 55 | +```bash |
| 56 | +$ composer require open-telemetry/opentelemetry-propagation-server-timing |
| 57 | +``` |
| 58 | + |
| 59 | +## Installing dependencies and executing tests |
| 60 | + |
| 61 | +From TraceResponse subdirectory: |
| 62 | + |
| 63 | +```bash |
| 64 | +$ composer install |
| 65 | +$ ./vendor/bin/phpunit tests |
| 66 | +``` |
0 commit comments