Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagated Sampling Rates #1793

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
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
26 changes: 17 additions & 9 deletions src/State/Hub.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@
$samplingContext->setAdditionalContext($customSamplingContext);

$sampleSource = 'context';
$sampleRand = $context->getMetadata()->getSampleRand();

if ($transaction->getSampled() === null) {
$tracesSampler = $options->getTracesSampler();
Expand All @@ -278,12 +279,17 @@

$sampleSource = 'config:traces_sampler';
} else {
$sampleRate = $this->getSampleRate(
$samplingContext->getParentSampled(),
$options->getTracesSampleRate() ?? 0
);

$sampleSource = $samplingContext->getParentSampled() ? 'parent' : 'config:traces_sample_rate';
$parentSampleRate = (float) $context->getMetadata()->getDynamicSamplingContext()->get('sample_rate');

Check failure on line 282 in src/State/Hub.php

View workflow job for this annotation

GitHub Actions / PHPStan

Cannot call method get() on Sentry\Tracing\DynamicSamplingContext|null.
if ($parentSampleRate !== null) {
$sampleRate = $parentSampleRate;
$sampleSource = 'parent';
} else {

Check failure on line 286 in src/State/Hub.php

View workflow job for this annotation

GitHub Actions / PHPStan

Else branch is unreachable because previous condition is always true.
$sampleRate = $this->getSampleRate(
$samplingContext->getParentSampled(),
$options->getTracesSampleRate() ?? 0
);
$sampleSource = $samplingContext->getParentSampled() ? 'parent' : 'config:traces_sample_rate';
}
}

if (!$this->isValidSampleRate($sampleRate)) {
Expand All @@ -294,6 +300,7 @@
return $transaction;
}

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

if ($sampleRate === 0.0) {
Expand All @@ -304,7 +311,8 @@
return $transaction;
}

$transaction->setSampled($this->sample($sampleRate));
$sampleRand = $context->getMetadata()->getSampleRand();
$transaction->setSampled($sampleRand < $sampleRate);
}

if (!$transaction->getSampled()) {
Expand Down Expand Up @@ -376,11 +384,11 @@
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
5 changes: 5 additions & 0 deletions src/Tracing/DynamicSamplingContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@
$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 All @@ -195,6 +199,7 @@
{
$samplingContext = new self();
$samplingContext->set('trace_id', (string) $scope->getPropagationContext()->getTraceId());
$samplingContext->set('sample_rand', (string) $scope->getPropagationContext()->getSampleRand());

Check failure on line 202 in src/Tracing/DynamicSamplingContext.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to an undefined method Sentry\Tracing\PropagationContext::getSampleRand().

if ($options->getTracesSampleRate() !== null) {
$samplingContext->set('sample_rate', (string) $options->getTracesSampleRate());
Expand Down
9 changes: 9 additions & 0 deletions src/Tracing/PropagationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
*/
private $dynamicSamplingContext;

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

Check failure on line 39 in src/Tracing/PropagationContext.php

View workflow job for this annotation

GitHub Actions / PHPStan

Property Sentry\Tracing\PropagationContext::$sampleRand is never read, only written.

private function __construct()
{
}
Expand All @@ -46,6 +51,9 @@
$context->parentSpanId = null;
$context->dynamicSamplingContext = null;

// TODO check if this is precise enough
$context->sampleRand = round(mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax(), 6);

return $context;
}

Expand Down Expand Up @@ -159,6 +167,7 @@
return $this;
}

// TODO add same logic as in TransactionContext
private static function parseTraceparentAndBaggage(string $traceparent, string $baggage): self
{
$context = self::fromDefaults();
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
18 changes: 18 additions & 0 deletions src/Tracing/TransactionContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,24 @@
$context->getMetadata()->setDynamicSamplingContext($samplingContext);
}

if ($samplingContext->has('sample_rand')) {
// TODO check for 1e13 etc.
$context->getMetadata()->setSampleRand((float) $samplingContext->get('sample_rand'));
} else {
if ($samplingContext->has('sample-rate') && $context->parentSampled !== null) {
if ($context->parentSampled === true) {
// [0, rate)
$context->getMetadata()->setSampleRand(round(mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax() * $samplingContext->get('sample-rate'), 6));

Check failure on line 208 in src/Tracing/TransactionContext.php

View workflow job for this annotation

GitHub Actions / PHPStan

Binary operation "*" between (float|int) and string|null results in an error.
} else {
// [rate, 1]
$context->getMetadata()->setSampleRand(round(mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax() * (1 - $samplingContext->get('sample-rate')) + $samplingContext->get('sample-rate'), 6));

Check failure on line 211 in src/Tracing/TransactionContext.php

View workflow job for this annotation

GitHub Actions / PHPStan

Binary operation "+" between (float|int) and string|null results in an error.

Check failure on line 211 in src/Tracing/TransactionContext.php

View workflow job for this annotation

GitHub Actions / PHPStan

Binary operation "-" between 1 and string|null results in an error.
}
} elseif ($context->parentSampled !== null) {
// [0, 1)
$context->getMetadata()->setSampleRand(round(mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax(), 6));
}
}

return $context;
}
}
20 changes: 20 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,9 @@ public function __construct(
$this->samplingRate = $samplingRate;
$this->dynamicSamplingContext = $dynamicSamplingContext;
$this->source = $source ?? TransactionSource::custom();

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

/**
Expand All @@ -56,6 +64,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
Loading