Skip to content

Commit 26bbd27

Browse files
committed
fix(tracing): ignore baggage sample rate if sentry trace is not present
1 parent f2f085b commit 26bbd27

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

src/Tracing/Traits/TraceHeaderParserTrait.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,21 @@ protected static function parseTraceAndBaggageHeaders(string $sentryTrace, strin
6565

6666
$samplingContext = DynamicSamplingContext::fromHeader($baggage);
6767

68-
if ($hasSentryTrace && !$samplingContext->hasEntries()) {
68+
if ($hasSentryTrace) {
6969
// The request comes from an old SDK which does not support Dynamic Sampling.
7070
// Propagate the Dynamic Sampling Context as is, but frozen, even without sentry-* entries.
71-
$samplingContext->freeze();
72-
$result['dynamicSamplingContext'] = $samplingContext;
73-
}
71+
if (!$samplingContext->hasEntries()) {
72+
$samplingContext->freeze();
73+
}
7474

75-
if ($hasSentryTrace && $samplingContext->hasEntries()) {
7675
// The baggage header contains Dynamic Sampling Context data from an upstream SDK.
7776
// Propagate this Dynamic Sampling Context.
7877
$result['dynamicSamplingContext'] = $samplingContext;
79-
}
8078

81-
// Store the propagated traces sample rate
82-
if ($samplingContext->has('sample_rate')) {
83-
$result['parentSamplingRate'] = (float) $samplingContext->get('sample_rate');
79+
// Store the propagated traces sample rate
80+
if ($samplingContext->has('sample_rate')) {
81+
$result['parentSamplingRate'] = (float)$samplingContext->get('sample_rate');
82+
}
8483
}
8584

8685
// Store the propagated trace sample rand or generate a new one

tests/State/HubTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,22 @@ public function testStartTransactionWithTracesSampler(Options $options, Transact
636636
$this->assertSame($expectedSampled, $transaction->getSampled());
637637
}
638638

639+
public function testStartTransactionIgnoresBaggageSampleRateWithoutSentryTrace(): void
640+
{
641+
$client = $this->createMock(ClientInterface::class);
642+
$client->expects($this->once())
643+
->method('getOptions')
644+
->willReturn(new Options([
645+
'traces_sample_rate' => 0.0,
646+
]));
647+
648+
$hub = new Hub($client);
649+
$transactionContext = TransactionContext::fromHeaders('', 'sentry-sample_rate=1');
650+
$transaction = $hub->startTransaction($transactionContext);
651+
652+
$this->assertFalse($transaction->getSampled());
653+
}
654+
639655
public static function startTransactionDataProvider(): iterable
640656
{
641657
yield 'Acceptable float value returned from traces_sampler' => [

tests/Tracing/TransactionContextTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,11 @@ public function testSampleRandRangeWhenParentNotSampledAndSampleRateProvided():
149149
$this->assertGreaterThanOrEqual(0.4, $sampleRand);
150150
$this->assertLessThanOrEqual(0.999999, $sampleRand);
151151
}
152+
153+
public function testParentSamplingRateIsIgnoredWithoutSentryTraceHeader(): void
154+
{
155+
$context = TransactionContext::fromHeaders('', 'sentry-sample_rate=1');
156+
157+
$this->assertNull($context->getMetadata()->getParentSamplingRate());
158+
}
152159
}

0 commit comments

Comments
 (0)