Skip to content

Commit

Permalink
Always add sampled flag to W3C traceparent (#1713)
Browse files Browse the repository at this point in the history
  • Loading branch information
cleptric authored Mar 8, 2024
1 parent 8a252ab commit d59afe1
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/Tracing/PropagationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function toTraceparent(): string
*/
public function toW3CTraceparent(): string
{
return sprintf('00-%s-%s', (string) $this->traceId, (string) $this->spanId);
return sprintf('00-%s-%s-00', (string) $this->traceId, (string) $this->spanId);
}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/Tracing/Span.php
Original file line number Diff line number Diff line change
Expand Up @@ -578,10 +578,13 @@ public function toW3CTraceparent(): string
$sampled = '';

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

return sprintf('00-%s-%s%s', (string) $this->traceId, (string) $this->spanId, $sampled);
return sprintf('00-%s-%s-%s', (string) $this->traceId, (string) $this->spanId, $sampled);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions tests/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ public function testW3CTraceparentWithTracingDisabled(): void

$traceParent = getW3CTraceparent();

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

public function testW3CTraceparentWithTracingEnabled(): void
Expand All @@ -462,15 +462,16 @@ public function testW3CTraceparentWithTracingEnabled(): void

$spanContext = (new SpanContext())
->setTraceId(new TraceId('566e3688a61d4bc888951642d6f14a19'))
->setSpanId(new SpanId('566e3688a61d4bc8'));
->setSpanId(new SpanId('566e3688a61d4bc8'))
->setSampled(true);

$span = new Span($spanContext);

$hub->setSpan($span);

$traceParent = getW3CTraceparent();

$this->assertSame('00-566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8', $traceParent);
$this->assertSame('00-566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8-01', $traceParent);
}

public function testBaggageWithTracingDisabled(): void
Expand Down
2 changes: 1 addition & 1 deletion tests/Tracing/PropagationContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function testToW3CTraceparent()
$propagationContext->setTraceId(new TraceId('566e3688a61d4bc888951642d6f14a19'));
$propagationContext->setSpanId(new SpanId('566e3688a61d4bc8'));

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

public function testToBaggage()
Expand Down
10 changes: 0 additions & 10 deletions tests/Tracing/TransactionContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,6 @@ public static function tracingDataProvider(): iterable
true,
];

yield [
'00-566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8',
'',
new SpanId('566e3688a61d4bc8'),
new TraceId('566e3688a61d4bc888951642d6f14a19'),
null,
DynamicSamplingContext::class,
true,
];

yield [
'566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8-1',
'sentry-public_key=public,sentry-trace_id=566e3688a61d4bc888951642d6f14a19,sentry-sample_rate=1',
Expand Down

0 comments on commit d59afe1

Please sign in to comment.