Skip to content

Commit 1aefa71

Browse files
committed
WIP
1 parent 2ce61f1 commit 1aefa71

File tree

5 files changed

+35
-9
lines changed

5 files changed

+35
-9
lines changed

src/State/Hub.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ public function startTransaction(TransactionContext $context, array $customSampl
269269
$samplingContext->setAdditionalContext($customSamplingContext);
270270

271271
$sampleSource = 'context';
272+
$sampleRand = $context->getMetadata()->getSampleRand();
272273

273274
if ($transaction->getSampled() === null) {
274275
$tracesSampler = $options->getTracesSampler();
@@ -278,12 +279,17 @@ public function startTransaction(TransactionContext $context, array $customSampl
278279

279280
$sampleSource = 'config:traces_sampler';
280281
} else {
281-
$sampleRate = $this->getSampleRate(
282-
$samplingContext->getParentSampled(),
283-
$options->getTracesSampleRate() ?? 0
284-
);
285-
286-
$sampleSource = $samplingContext->getParentSampled() ? 'parent' : 'config:traces_sample_rate';
282+
$parentSampleRate = (float) $context->getMetadata()->getDynamicSamplingContext()->get('sample_rate');
283+
if ($parentSampleRate !== null) {
284+
$sampleRate = $parentSampleRate;
285+
$sampleSource = 'parent';
286+
} else {
287+
$sampleRate = $this->getSampleRate(
288+
$samplingContext->getParentSampled(),
289+
$options->getTracesSampleRate() ?? 0
290+
);
291+
$sampleSource = $samplingContext->getParentSampled() ? 'parent' : 'config:traces_sample_rate';
292+
}
287293
}
288294

289295
if (!$this->isValidSampleRate($sampleRate)) {
@@ -294,6 +300,7 @@ public function startTransaction(TransactionContext $context, array $customSampl
294300
return $transaction;
295301
}
296302

303+
// TODO Why are we doing this?
297304
$transaction->getMetadata()->setSamplingRate($sampleRate);
298305

299306
if ($sampleRate === 0.0) {

src/Tracing/DynamicSamplingContext.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ public static function fromOptions(Options $options, Scope $scope): self
199199
{
200200
$samplingContext = new self();
201201
$samplingContext->set('trace_id', (string) $scope->getPropagationContext()->getTraceId());
202+
$samplingContext->set('sample_rand', (string) $scope->getPropagationContext()->getSampleRand());
202203

203204
if ($options->getTracesSampleRate() !== null) {
204205
$samplingContext->set('sample_rate', (string) $options->getTracesSampleRate());

src/Tracing/PropagationContext.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ final class PropagationContext
3333
*/
3434
private $dynamicSamplingContext;
3535

36+
/**
37+
* @var float|null
38+
*/
39+
private $sampleRand;
40+
3641
private function __construct()
3742
{
3843
}
@@ -46,6 +51,9 @@ public static function fromDefaults(): self
4651
$context->parentSpanId = null;
4752
$context->dynamicSamplingContext = null;
4853

54+
// TODO check if this is precise enough
55+
$context->sampleRand = round(mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax(), 6);
56+
4957
return $context;
5058
}
5159

@@ -159,6 +167,7 @@ public function setDynamicSamplingContext(DynamicSamplingContext $dynamicSamplin
159167
return $this;
160168
}
161169

170+
// TODO add same logic as in TransactionContext
162171
private static function parseTraceparentAndBaggage(string $traceparent, string $baggage): self
163172
{
164173
$context = self::fromDefaults();

src/Tracing/TransactionContext.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,20 @@ private static function parseTraceAndBaggage(string $sentryTrace, string $baggag
199199
}
200200

201201
if ($samplingContext->has('sample_rand')) {
202+
// TODO check for 1e13 etc.
202203
$context->getMetadata()->setSampleRand((float) $samplingContext->get('sample_rand'));
203204
} else {
204205
if ($samplingContext->has('sample-rate') && $context->parentSampled !== null) {
205-
$context->getMetadata()->setSampleRand(1);
206+
if ($context->parentSampled === true) {
207+
// [0, rate)
208+
$context->getMetadata()->setSampleRand(round(mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax() * $samplingContext->get('sample-rate'), 6));
209+
} else {
210+
// [rate, 1]
211+
$context->getMetadata()->setSampleRand(round(mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax() * (1 - $samplingContext->get('sample-rate')) + $samplingContext->get('sample-rate'), 6));
212+
}
206213
} elseif ($context->parentSampled !== null) {
207-
$context->getMetadata()->setSampleRand(1);
214+
// [0, 1)
215+
$context->getMetadata()->setSampleRand(round(mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax(), 6));
208216
}
209217
}
210218

src/Tracing/TransactionMetadata.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public function __construct(
4242
$this->dynamicSamplingContext = $dynamicSamplingContext;
4343
$this->source = $source ?? TransactionSource::custom();
4444

45-
$this->sampleRand = round(mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax(), 2);
45+
// TODO check if this is precise enough
46+
$this->sampleRand = round(mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax(), 6);
4647
}
4748

4849
/**

0 commit comments

Comments
 (0)