MediaWiki master
W3CTraceContextPropagator.php
Go to the documentation of this file.
1<?php
2namespace Wikimedia\Telemetry;
3
21 public function inject( ?SpanContext $spanContext, array $carrier ): array {
22 if ( $spanContext === null ) {
23 return $carrier;
24 }
25 $traceId = $spanContext->getTraceId();
26 $spanId = $spanContext->getSpanId();
27 $sampled = $spanContext->isSampled() ? '01' : '00';
28 if ( strlen( $traceId ) === 32 && strlen( $spanId ) === 16 ) {
29 $carrier['traceparent'] = "00-{$traceId}-{$spanId}-{$sampled}";
30 }
31
32 return $carrier;
33 }
34
38 public function extract( array $carrier ): ?SpanContext {
39 $carrier = array_change_key_case( $carrier, CASE_LOWER );
40 if ( !isset( $carrier['traceparent'] ) ) {
41 return null;
42 }
43 $matches = [];
44 if ( !preg_match( '/^00-([0-9a-f]{32})-([0-9a-f]{16})-([0-9a-f]{2})$/', $carrier['traceparent'], $matches ) ) {
45 return null;
46 }
47 return new SpanContext(
48 $matches[1],
49 $matches[2],
50 null,
51 '',
52 ( hexdec( $matches[3] ) & 1 ) === 1
53 );
54 }
55}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:68
Data transfer object holding data associated with a given span.
A ContextPropagatorInterface implementation for W3C Trace Context.
extract(array $carrier)
Attempt to extract a SpanContext from the given carrier.
inject(?SpanContext $spanContext, array $carrier)
Inject the given SpanContext into the given carrier.SpanContext may be null, in which case the propag...
Interface for classes to serialize and deserialize SpanContexts to and from implementation-specific a...