From 80262d53591e6b547d80fadc50f669e89792d653 Mon Sep 17 00:00:00 2001 From: Michael Hoffmann Date: Fri, 22 Nov 2024 09:42:53 +0100 Subject: [PATCH] Propagated Sampling Rates --- src/State/Hub.php | 13 ++++++++++--- src/Tracing/DynamicSamplingContext.php | 4 ++++ src/Tracing/SamplingContext.php | 11 +++++++++++ src/Tracing/TransactionContext.php | 8 ++++++++ src/Tracing/TransactionMetadata.php | 19 +++++++++++++++++++ 5 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/State/Hub.php b/src/State/Hub.php index 4fa9a4382..f34ff82f5 100644 --- a/src/State/Hub.php +++ b/src/State/Hub.php @@ -304,7 +304,14 @@ public function startTransaction(TransactionContext $context, array $customSampl return $transaction; } - $transaction->setSampled($this->sample($sampleRate)); + // The sample rand should only be used if the traces sampler is set. + // Otherwise, we use the configured traces sample rate. + $sampleRand = $context->getMetadata()->getSampleRand(); + if ($tracesSampler !== null && $sampleRand !== null) { + $transaction->setSampled($sampleRand < $sampleRate); + } else { + $transaction->setSampled($this->sample($sampleRate)); + } } if (!$transaction->getSampled()) { @@ -376,11 +383,11 @@ private function getStackTop(): Layer private function getSampleRate(?bool $hasParentBeenSampled, float $fallbackSampleRate): float { if ($hasParentBeenSampled === true) { - return 1; + return 1.0; } if ($hasParentBeenSampled === false) { - return 0; + return 0.0; } return $fallbackSampleRate; diff --git a/src/Tracing/DynamicSamplingContext.php b/src/Tracing/DynamicSamplingContext.php index cf159ed59..8146894cc 100644 --- a/src/Tracing/DynamicSamplingContext.php +++ b/src/Tracing/DynamicSamplingContext.php @@ -186,6 +186,10 @@ public static function fromTransaction(Transaction $transaction, HubInterface $h $samplingContext->set('sampled', $transaction->getSampled() ? 'true' : 'false'); } + if ($transaction->getMetadata()->getSampleRand() !== null) { + $samplingContext->set('sample_rand', (string) $transaction->getMetadata()->getSampleRand()); + } + $samplingContext->freeze(); return $samplingContext; diff --git a/src/Tracing/SamplingContext.php b/src/Tracing/SamplingContext.php index bee540bfb..24a393cbc 100644 --- a/src/Tracing/SamplingContext.php +++ b/src/Tracing/SamplingContext.php @@ -16,6 +16,11 @@ final class SamplingContext */ private $parentSampled; + /** + * @var float|null The parent sample rate + */ + private $sampleRand; + /** * @var array|null Additional context, depending on where the SDK runs */ @@ -29,6 +34,7 @@ public static function getDefault(TransactionContext $transactionContext): self $context = new self(); $context->transactionContext = $transactionContext; $context->parentSampled = $transactionContext->getParentSampled(); + $context->sampleRand = $transactionContext->getMetadata()->getSampleRand(); return $context; } @@ -46,6 +52,11 @@ public function getParentSampled(): ?bool return $this->parentSampled; } + public function getSampleRand(): ?float + { + return $this->sampleRand; + } + /** * Sets the sampling decision from the parent transaction, if any. */ diff --git a/src/Tracing/TransactionContext.php b/src/Tracing/TransactionContext.php index e804a1bd9..04f42f0fb 100644 --- a/src/Tracing/TransactionContext.php +++ b/src/Tracing/TransactionContext.php @@ -196,6 +196,14 @@ private static function parseTraceAndBaggage(string $sentryTrace, string $baggag // The baggage header contains Dynamic Sampling Context data from an upstream SDK. // Propagate this Dynamic Sampling Context. $context->getMetadata()->setDynamicSamplingContext($samplingContext); + + // Store the parent sample rate (sample_rand) if it exists, + // otherwise set it to 1.0 if the parent was sampled. + if ($samplingContext->has('sample_rand')) { + $context->getMetadata()->setSampleRand((float) $samplingContext->get('sample_rand')); + } elseif ($context->parentSampled === true) { + $context->getMetadata()->setSampleRand(1.0); + } } return $context; diff --git a/src/Tracing/TransactionMetadata.php b/src/Tracing/TransactionMetadata.php index 2a10923bc..670a1586c 100644 --- a/src/Tracing/TransactionMetadata.php +++ b/src/Tracing/TransactionMetadata.php @@ -21,6 +21,11 @@ final class TransactionMetadata */ private $source; + /** + * @var float|null + */ + private $sampleRand; + /** * Constructor. * @@ -36,6 +41,8 @@ public function __construct( $this->samplingRate = $samplingRate; $this->dynamicSamplingContext = $dynamicSamplingContext; $this->source = $source ?? TransactionSource::custom(); + + $this->sampleRand = round(mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax(), 2); } /** @@ -56,6 +63,18 @@ public function setSamplingRate($samplingRate): self return $this; } + public function getSampleRand(): ?float + { + return $this->sampleRand; + } + + public function setSampleRand(?float $sampleRand): self + { + $this->sampleRand = $sampleRand; + + return $this; + } + public function getDynamicSamplingContext(): ?DynamicSamplingContext { return $this->dynamicSamplingContext;