Skip to content

Commit db21532

Browse files
authored
Use correct 'sample_rate' key when deriving sampleRand (#1874)
1 parent 23f25f1 commit db21532

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

src/Tracing/PropagationContext.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ private static function parseTraceparentAndBaggage(string $traceparent, string $
231231
$context->sampleRand = round(mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax() * (float) $samplingContext->get('sample_rate'), 6);
232232
} else {
233233
// [rate, 1)
234-
$context->sampleRand = round(mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax() * (1 - (float) $samplingContext->get('sample_rate')) + (float) $samplingContext->get('sample-rate'), 6);
234+
$context->sampleRand = round(mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax() * (1 - (float) $samplingContext->get('sample_rate')) + (float) $samplingContext->get('sample_rate'), 6);
235235
}
236236
} elseif ($context->parentSampled !== null) {
237237
// [0, 1)

src/Tracing/TransactionContext.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ private static function parseTraceAndBaggage(string $sentryTrace, string $baggag
196196
$context->getMetadata()->setSampleRand(round(mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax() * (float) $samplingContext->get('sample_rate'), 6));
197197
} else {
198198
// [rate, 1)
199-
$context->getMetadata()->setSampleRand(round(mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax() * (1 - (float) $samplingContext->get('sample_rate')) + (float) $samplingContext->get('sample-rate'), 6));
199+
$context->getMetadata()->setSampleRand(round(mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax() * (1 - (float) $samplingContext->get('sample_rate')) + (float) $samplingContext->get('sample_rate'), 6));
200200
}
201201
} elseif ($context->parentSampled !== null) {
202202
// [0, 1)

tests/Tracing/PropagationContextTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,21 @@ public function testGetTraceContext()
136136
], $propagationContext->getTraceContext());
137137
}
138138

139+
public function testSampleRandRangeWhenParentNotSampledAndSampleRateProvided(): void
140+
{
141+
$propagationContext = PropagationContext::fromHeaders(
142+
'566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8-0',
143+
'sentry-sample_rate=0.4'
144+
);
145+
146+
$sampleRand = $propagationContext->getSampleRand();
147+
148+
$this->assertNotNull($sampleRand);
149+
// Should be within [rate, 1) and rounded to 6 decimals
150+
$this->assertGreaterThanOrEqual(0.4, $sampleRand);
151+
$this->assertLessThanOrEqual(0.999999, $sampleRand);
152+
}
153+
139154
/**
140155
* @dataProvider gettersAndSettersDataProvider
141156
*/

tests/Tracing/TransactionContextTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,19 @@ public static function tracingDataProvider(): iterable
137137
true,
138138
];
139139
}
140+
141+
public function testSampleRandRangeWhenParentNotSampledAndSampleRateProvided(): void
142+
{
143+
$context = TransactionContext::fromHeaders(
144+
'566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8-0',
145+
'sentry-sample_rate=0.4'
146+
);
147+
148+
$sampleRand = $context->getMetadata()->getSampleRand();
149+
150+
$this->assertNotNull($sampleRand);
151+
// Should be within [rate, 1) and rounded to 6 decimals
152+
$this->assertGreaterThanOrEqual(0.4, $sampleRand);
153+
$this->assertLessThanOrEqual(0.999999, $sampleRand);
154+
}
140155
}

0 commit comments

Comments
 (0)