Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
cleptric committed Dec 16, 2024
1 parent b0c1b09 commit 51655ba
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
19 changes: 13 additions & 6 deletions src/State/Hub.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ public function startTransaction(TransactionContext $context, array $customSampl
$samplingContext->setAdditionalContext($customSamplingContext);

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

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

$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 @@ public function startTransaction(TransactionContext $context, array $customSampl
return $transaction;
}

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

if ($sampleRate === 0.0) {
Expand Down
1 change: 1 addition & 0 deletions src/Tracing/DynamicSamplingContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ public static function fromOptions(Options $options, Scope $scope): self
{
$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 @@ final class PropagationContext
*/
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 @@ public static function fromDefaults(): self
$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 @@ public function setDynamicSamplingContext(DynamicSamplingContext $dynamicSamplin
return $this;
}

// TODO add same logic as in TransactionContext
private static function parseTraceparentAndBaggage(string $traceparent, string $baggage): self
{
$context = self::fromDefaults();
Expand Down
12 changes: 10 additions & 2 deletions src/Tracing/TransactionContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,20 @@ private static function parseTraceAndBaggage(string $sentryTrace, string $baggag
}

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) {
$context->getMetadata()->setSampleRand(1);
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) {
$context->getMetadata()->setSampleRand(1);
// [0, 1)
$context->getMetadata()->setSampleRand(round(mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax(), 6));
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/Tracing/TransactionMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public function __construct(
$this->dynamicSamplingContext = $dynamicSamplingContext;
$this->source = $source ?? TransactionSource::custom();

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

/**
Expand Down

0 comments on commit 51655ba

Please sign in to comment.