Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions src/Tracing/Traits/TraceHeaderParserTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,21 @@ protected static function parseTraceAndBaggageHeaders(string $sentryTrace, strin

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

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

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

// Store the propagated traces sample rate
if ($samplingContext->has('sample_rate')) {
$result['parentSamplingRate'] = (float) $samplingContext->get('sample_rate');
// Store the propagated traces sample rate
if ($samplingContext->has('sample_rate')) {
$result['parentSamplingRate'] = (float)$samplingContext->get('sample_rate');
}
}

// Store the propagated trace sample rand or generate a new one
Expand Down
16 changes: 16 additions & 0 deletions tests/State/HubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,22 @@ public function testStartTransactionWithTracesSampler(Options $options, Transact
$this->assertSame($expectedSampled, $transaction->getSampled());
}

public function testStartTransactionIgnoresBaggageSampleRateWithoutSentryTrace(): void
{
$client = $this->createMock(ClientInterface::class);
$client->expects($this->once())
->method('getOptions')
->willReturn(new Options([
'traces_sample_rate' => 0.0,
]));

$hub = new Hub($client);
$transactionContext = TransactionContext::fromHeaders('', 'sentry-sample_rate=1');
$transaction = $hub->startTransaction($transactionContext);

$this->assertFalse($transaction->getSampled());
}

public static function startTransactionDataProvider(): iterable
{
yield 'Acceptable float value returned from traces_sampler' => [
Expand Down
7 changes: 7 additions & 0 deletions tests/Tracing/TransactionContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,11 @@ public function testSampleRandRangeWhenParentNotSampledAndSampleRateProvided():
$this->assertGreaterThanOrEqual(0.4, $sampleRand);
$this->assertLessThanOrEqual(0.999999, $sampleRand);
}

public function testParentSamplingRateIsIgnoredWithoutSentryTraceHeader(): void
{
$context = TransactionContext::fromHeaders('', 'sentry-sample_rate=1');

$this->assertNull($context->getMetadata()->getParentSamplingRate());
}
}
Loading