-
-
Notifications
You must be signed in to change notification settings - Fork 470
feat(scopes): extract transaction sampling logic into TransactionSampler #2118
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
Open
+509
−142
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Sentry\Tracing; | ||
|
|
||
| use Sentry\Options; | ||
|
|
||
| /** | ||
| * Applies tracing and profiling sampling decisions to transactions. | ||
| * | ||
| * @internal | ||
| */ | ||
| final class TransactionSampler | ||
| { | ||
| /** | ||
| * @var Options | ||
| */ | ||
| private $options; | ||
|
|
||
| public function __construct(Options $options) | ||
| { | ||
| $this->options = $options; | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, mixed> $customSamplingContext Additional context that will be passed to the {@see SamplingContext} | ||
| */ | ||
| public function startTransaction(Transaction $transaction, TransactionContext $context, array $customSamplingContext = []): Transaction | ||
| { | ||
| $logger = $this->options->getLoggerOrNullLogger(); | ||
|
|
||
| if (!$this->options->isTracingEnabled()) { | ||
| $transaction->setSampled(false); | ||
|
|
||
| $logger->warning(\sprintf('Transaction [%s] was started but tracing is not enabled.', (string) $transaction->getTraceId()), ['context' => $context]); | ||
|
|
||
| return $transaction; | ||
| } | ||
|
|
||
| $samplingContext = SamplingContext::getDefault($context); | ||
| $samplingContext->setAdditionalContext($customSamplingContext); | ||
|
|
||
| $sampleSource = 'context'; | ||
| $sampleRand = $context->getMetadata()->getSampleRand() ?? 0.0; | ||
|
|
||
| if ($transaction->getSampled() === null) { | ||
| $tracesSampler = $this->options->getTracesSampler(); | ||
|
|
||
| if ($tracesSampler !== null) { | ||
| $sampleRate = $tracesSampler($samplingContext); | ||
| $sampleSource = 'config:traces_sampler'; | ||
| } else { | ||
| $parentSampleRate = $context->getMetadata()->getParentSamplingRate(); | ||
| if ($parentSampleRate !== null) { | ||
| $sampleRate = $parentSampleRate; | ||
| $sampleSource = 'parent:sample_rate'; | ||
| } else { | ||
| $sampleRate = $this->getSampleRate( | ||
| $samplingContext->getParentSampled(), | ||
| $this->options->getTracesSampleRate() ?? 0 | ||
| ); | ||
| $sampleSource = $samplingContext->getParentSampled() !== null ? 'parent:sampling_decision' : 'config:traces_sample_rate'; | ||
| } | ||
| } | ||
|
|
||
| if (!$this->isValidSampleRate($sampleRate)) { | ||
| $transaction->setSampled(false); | ||
|
|
||
| $logger->warning(\sprintf('Transaction [%s] was started but not sampled because sample rate (decided by %s) is invalid.', (string) $transaction->getTraceId(), $sampleSource), ['context' => $context]); | ||
|
|
||
| return $transaction; | ||
| } | ||
|
|
||
| $transaction->getMetadata()->setSamplingRate($sampleRate); | ||
|
|
||
| // Always overwrite the sample_rate in the DSC | ||
| $dynamicSamplingContext = $context->getMetadata()->getDynamicSamplingContext(); | ||
| if ($dynamicSamplingContext !== null) { | ||
| $dynamicSamplingContext->set('sample_rate', (string) $sampleRate, true); | ||
| } | ||
|
|
||
| if ($sampleRate === 0.0) { | ||
| $transaction->setSampled(false); | ||
|
|
||
| $logger->info(\sprintf('Transaction [%s] was started but not sampled because sample rate (decided by %s) is %s.', (string) $transaction->getTraceId(), $sampleSource, $sampleRate), ['context' => $context]); | ||
|
|
||
| return $transaction; | ||
| } | ||
|
|
||
| $transaction->setSampled($sampleRand < $sampleRate); | ||
| } | ||
|
|
||
| if (!$transaction->getSampled()) { | ||
| $logger->info(\sprintf('Transaction [%s] was started but not sampled, decided by %s.', (string) $transaction->getTraceId(), $sampleSource), ['context' => $context]); | ||
|
|
||
| return $transaction; | ||
| } | ||
|
|
||
| $logger->info(\sprintf('Transaction [%s] was started and sampled, decided by %s.', (string) $transaction->getTraceId(), $sampleSource), ['context' => $context]); | ||
|
|
||
| $transaction->initSpanRecorder(); | ||
|
|
||
| $profilesSampleSource = 'config:profiles_sample_rate'; | ||
| $profilesSampler = $this->options->getProfilesSampler(); | ||
|
|
||
| if ($profilesSampler !== null) { | ||
| $profilesSampleRate = $profilesSampler($samplingContext); | ||
| $profilesSampleSource = 'config:profiles_sampler'; | ||
| } else { | ||
| $profilesSampleRate = $this->options->getProfilesSampleRate(); | ||
| } | ||
|
|
||
| if ($profilesSampleRate === null) { | ||
| $logger->info(\sprintf('Transaction [%s] is not profiling because neither `profiles_sample_rate` nor `profiles_sampler` option is set.', (string) $transaction->getTraceId())); | ||
| } elseif (!$this->isValidSampleRate($profilesSampleRate)) { | ||
| $logger->warning(\sprintf('Transaction [%s] is not profiling because profile sample rate (decided by %s) is invalid.', (string) $transaction->getTraceId(), $profilesSampleSource)); | ||
| } elseif ($this->sampleRate($profilesSampleRate)) { | ||
| $logger->info(\sprintf('Transaction [%s] started profiling because it was sampled.', (string) $transaction->getTraceId())); | ||
|
|
||
| $transaction->initProfiler()->start(); | ||
| } else { | ||
| $logger->info(\sprintf('Transaction [%s] is not profiling because it was not sampled.', (string) $transaction->getTraceId())); | ||
| } | ||
|
|
||
| return $transaction; | ||
| } | ||
|
|
||
| private function getSampleRate(?bool $hasParentBeenSampled, float $fallbackSampleRate): float | ||
| { | ||
| if ($hasParentBeenSampled === true) { | ||
| return 1.0; | ||
| } | ||
|
|
||
| if ($hasParentBeenSampled === false) { | ||
| return 0.0; | ||
| } | ||
|
|
||
| return $fallbackSampleRate; | ||
| } | ||
|
|
||
| /** | ||
| * @param mixed $sampleRate | ||
| */ | ||
| private function sampleRate($sampleRate): bool | ||
| { | ||
| if (!\is_float($sampleRate) && !\is_int($sampleRate)) { | ||
| return false; | ||
| } | ||
|
|
||
| if ($sampleRate === 0.0) { | ||
| return false; | ||
| } | ||
|
|
||
| if ($sampleRate === 1.0) { | ||
| return true; | ||
| } | ||
|
|
||
| return mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax() < (float) $sampleRate; | ||
| } | ||
|
|
||
| /** | ||
| * @param mixed $sampleRate | ||
| */ | ||
| private function isValidSampleRate($sampleRate): bool | ||
| { | ||
| if (!\is_float($sampleRate) && !\is_int($sampleRate)) { | ||
| return false; | ||
| } | ||
|
|
||
| if ($sampleRate < 0 || $sampleRate > 1) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.