Skip to content

Commit 45bda7e

Browse files
authored
Propagate Trace Context Level 2 random flag (#1731)
* Propagate Trace Context Level 2 random flag in `TraceContextPropagator` * Propagate Trace Context Level 2 random flag in SDK `SpanBuilder`
1 parent 1121c04 commit 45bda7e

2 files changed

Lines changed: 17 additions & 19 deletions

File tree

Trace/Propagation/TraceContextPropagator.php

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use OpenTelemetry\Context\Propagation\PropagationGetterInterface;
2020
use OpenTelemetry\Context\Propagation\PropagationSetterInterface;
2121
use OpenTelemetry\Context\Propagation\TextMapPropagatorInterface;
22+
use function sprintf;
2223

2324
/**
2425
* TraceContext is a propagator that supports the W3C Trace Context format
@@ -34,7 +35,8 @@ final class TraceContextPropagator implements TextMapPropagatorInterface
3435
{
3536
public const TRACEPARENT = 'traceparent';
3637
public const TRACESTATE = 'tracestate';
37-
private const VERSION = '00'; // Currently, only '00' is supported
38+
private const VERSION = 0x00; // Currently, only '00' is supported
39+
private const SUPPORTED_FLAGS = TraceFlags::SAMPLED | TraceFlags::RANDOM;
3840

3941
public const FIELDS = [
4042
self::TRACEPARENT,
@@ -72,7 +74,13 @@ public function inject(&$carrier, ?PropagationSetterInterface $setter = null, ?C
7274
}
7375

7476
// Build and inject the traceparent header
75-
$traceparent = self::VERSION . '-' . $spanContext->getTraceId() . '-' . $spanContext->getSpanId() . '-' . ($spanContext->isSampled() ? '01' : '00');
77+
$traceparent = sprintf(
78+
'%02x-%s-%s-%02x',
79+
self::VERSION,
80+
$spanContext->getTraceId(),
81+
$spanContext->getSpanId(),
82+
$spanContext->getTraceFlags() & self::SUPPORTED_FLAGS,
83+
);
7684
$setter->set($carrier, self::TRACEPARENT, $traceparent);
7785

7886
// Build and inject the tracestate header
@@ -128,33 +136,22 @@ private static function extractImpl($carrier, PropagationGetterInterface $getter
128136
}
129137

130138
// Return invalid if the trace version is not a future version but still has > 4 pieces.
131-
$versionIsFuture = hexdec($version) > hexdec(self::VERSION);
139+
$versionIsFuture = hexdec($version) > self::VERSION;
132140
if (count($pieces) > 4 && !$versionIsFuture) {
133141
return SpanContext::getInvalid();
134142
}
135143

136-
// Only the sampled flag is extracted from the traceFlags (00000001)
137-
$convertedTraceFlags = hexdec($traceFlags);
138-
$isSampled = ($convertedTraceFlags & TraceFlags::SAMPLED) === TraceFlags::SAMPLED;
139-
140144
// Tracestate = 'Vendor1=Value1,...,VendorN=ValueN'
141145
$rawTracestate = $getter->get($carrier, self::TRACESTATE);
142-
if ($rawTracestate !== null) {
143-
$tracestate = new TraceState($rawTracestate);
144-
145-
return SpanContext::createFromRemoteParent(
146-
$traceId,
147-
$spanId,
148-
$isSampled ? TraceFlags::SAMPLED : TraceFlags::DEFAULT,
149-
$tracestate
150-
);
151-
}
146+
$tracestate = $rawTracestate !== null
147+
? new TraceState($rawTracestate)
148+
: null;
152149

153-
// Only traceparent header is extracted. No tracestate.
154150
return SpanContext::createFromRemoteParent(
155151
$traceId,
156152
$spanId,
157-
$isSampled ? TraceFlags::SAMPLED : TraceFlags::DEFAULT
153+
hexdec($traceFlags) & self::SUPPORTED_FLAGS,
154+
$tracestate,
158155
);
159156
}
160157
}

Trace/TraceFlags.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
interface TraceFlags
88
{
99
public const SAMPLED = 0x01;
10+
public const RANDOM = 0x02;
1011
public const DEFAULT = 0x00;
1112
}

0 commit comments

Comments
 (0)