Skip to content

Commit d59afe1

Browse files
authored
Always add sampled flag to W3C traceparent (#1713)
1 parent 8a252ab commit d59afe1

File tree

5 files changed

+11
-17
lines changed

5 files changed

+11
-17
lines changed

src/Tracing/PropagationContext.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function toTraceparent(): string
7272
*/
7373
public function toW3CTraceparent(): string
7474
{
75-
return sprintf('00-%s-%s', (string) $this->traceId, (string) $this->spanId);
75+
return sprintf('00-%s-%s-00', (string) $this->traceId, (string) $this->spanId);
7676
}
7777

7878
/**

src/Tracing/Span.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,10 +578,13 @@ public function toW3CTraceparent(): string
578578
$sampled = '';
579579

580580
if ($this->sampled !== null) {
581-
$sampled = $this->sampled ? '-01' : '-00';
581+
$sampled = $this->sampled ? '01' : '00';
582+
} else {
583+
// If no sampling decision was made, set the flag to 00
584+
$sampled = '00';
582585
}
583586

584-
return sprintf('00-%s-%s%s', (string) $this->traceId, (string) $this->spanId, $sampled);
587+
return sprintf('00-%s-%s-%s', (string) $this->traceId, (string) $this->spanId, $sampled);
585588
}
586589

587590
/**

tests/FunctionsTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ public function testW3CTraceparentWithTracingDisabled(): void
444444

445445
$traceParent = getW3CTraceparent();
446446

447-
$this->assertSame('00-566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8', $traceParent);
447+
$this->assertSame('00-566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8-00', $traceParent);
448448
}
449449

450450
public function testW3CTraceparentWithTracingEnabled(): void
@@ -462,15 +462,16 @@ public function testW3CTraceparentWithTracingEnabled(): void
462462

463463
$spanContext = (new SpanContext())
464464
->setTraceId(new TraceId('566e3688a61d4bc888951642d6f14a19'))
465-
->setSpanId(new SpanId('566e3688a61d4bc8'));
465+
->setSpanId(new SpanId('566e3688a61d4bc8'))
466+
->setSampled(true);
466467

467468
$span = new Span($spanContext);
468469

469470
$hub->setSpan($span);
470471

471472
$traceParent = getW3CTraceparent();
472473

473-
$this->assertSame('00-566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8', $traceParent);
474+
$this->assertSame('00-566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8-01', $traceParent);
474475
}
475476

476477
public function testBaggageWithTracingDisabled(): void

tests/Tracing/PropagationContextTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public function testToW3CTraceparent()
118118
$propagationContext->setTraceId(new TraceId('566e3688a61d4bc888951642d6f14a19'));
119119
$propagationContext->setSpanId(new SpanId('566e3688a61d4bc8'));
120120

121-
$this->assertSame('00-566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8', $propagationContext->toW3CTraceparent());
121+
$this->assertSame('00-566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8-00', $propagationContext->toW3CTraceparent());
122122
}
123123

124124
public function testToBaggage()

tests/Tracing/TransactionContextTest.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,6 @@ public static function tracingDataProvider(): iterable
137137
true,
138138
];
139139

140-
yield [
141-
'00-566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8',
142-
'',
143-
new SpanId('566e3688a61d4bc8'),
144-
new TraceId('566e3688a61d4bc888951642d6f14a19'),
145-
null,
146-
DynamicSamplingContext::class,
147-
true,
148-
];
149-
150140
yield [
151141
'566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8-1',
152142
'sentry-public_key=public,sentry-trace_id=566e3688a61d4bc888951642d6f14a19,sentry-sample_rate=1',

0 commit comments

Comments
 (0)