Skip to content

Commit

Permalink
Propagated Sampling Rates
Browse files Browse the repository at this point in the history
  • Loading branch information
cleptric committed Nov 22, 2024
1 parent dbfc8c6 commit 80262d5
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/State/Hub.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/Tracing/DynamicSamplingContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions src/Tracing/SamplingContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ final class SamplingContext
*/
private $parentSampled;

/**
* @var float|null The parent sample rate
*/
private $sampleRand;

/**
* @var array<string, mixed>|null Additional context, depending on where the SDK runs
*/
Expand All @@ -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;
}
Expand All @@ -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.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Tracing/TransactionContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions src/Tracing/TransactionMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ final class TransactionMetadata
*/
private $source;

/**
* @var float|null
*/
private $sampleRand;

/**
* Constructor.
*
Expand All @@ -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);
}

/**
Expand All @@ -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;
Expand Down

0 comments on commit 80262d5

Please sign in to comment.