From d4ded9540b93a4c9432b2fc16c18d1017996e251 Mon Sep 17 00:00:00 2001 From: Martin Linzmayer Date: Thu, 11 Jun 2026 17:56:15 +0200 Subject: [PATCH] feat(scopes): use TransactionSampler --- src/State/Hub.php | 4 +-- src/Tracing/Transaction.php | 5 +-- src/Tracing/TransactionSampler.php | 39 +++++++++++------------- tests/State/HubTest.php | 2 +- tests/Tracing/TransactionSamplerTest.php | 9 +----- 5 files changed, 23 insertions(+), 36 deletions(-) diff --git a/src/State/Hub.php b/src/State/Hub.php index 2f4b947ed..82597e79c 100644 --- a/src/State/Hub.php +++ b/src/State/Hub.php @@ -236,9 +236,7 @@ public function getIntegration(string $className): ?IntegrationInterface */ public function startTransaction(TransactionContext $context, array $customSamplingContext = []): Transaction { - $transaction = new Transaction($context, $this); - - return (new TransactionSampler($this->getClient()->getOptions()))->startTransaction($transaction, $context, $customSamplingContext); + return TransactionSampler::startTransaction($this->getClient()->getOptions(), $context, $customSamplingContext); } /** diff --git a/src/Tracing/Transaction.php b/src/Tracing/Transaction.php index e2baa09b9..34134b855 100644 --- a/src/Tracing/Transaction.php +++ b/src/Tracing/Transaction.php @@ -6,6 +6,7 @@ use Sentry\Event; use Sentry\EventId; +use Sentry\Options; use Sentry\Profiling\Profiler; use Sentry\SentrySdk; use Sentry\State\HubInterface; @@ -119,10 +120,10 @@ public function initSpanRecorder(int $maxSpans = 1000): self return $this; } - public function initProfiler(): Profiler + public function initProfiler(?Options $options = null): Profiler { if ($this->profiler === null) { - $this->profiler = new Profiler($this->hub->getClient()->getOptions()); + $this->profiler = new Profiler($options ?? $this->hub->getClient()->getOptions()); } return $this->profiler; diff --git a/src/Tracing/TransactionSampler.php b/src/Tracing/TransactionSampler.php index c3bd46b9f..ef6a284ec 100644 --- a/src/Tracing/TransactionSampler.php +++ b/src/Tracing/TransactionSampler.php @@ -13,24 +13,19 @@ */ final class TransactionSampler { - /** - * @var Options - */ - private $options; - - public function __construct(Options $options) + private function __construct() { - $this->options = $options; } /** * @param array $customSamplingContext Additional context that will be passed to the {@see SamplingContext} */ - public function startTransaction(Transaction $transaction, TransactionContext $context, array $customSamplingContext = []): Transaction + public static function startTransaction(Options $options, TransactionContext $context, array $customSamplingContext = []): Transaction { - $logger = $this->options->getLoggerOrNullLogger(); + $transaction = new Transaction($context); + $logger = $options->getLoggerOrNullLogger(); - if (!$this->options->isTracingEnabled()) { + if (!$options->isTracingEnabled()) { $transaction->setSampled(false); $logger->warning(\sprintf('Transaction [%s] was started but tracing is not enabled.', (string) $transaction->getTraceId()), ['context' => $context]); @@ -45,7 +40,7 @@ public function startTransaction(Transaction $transaction, TransactionContext $c $sampleRand = $context->getMetadata()->getSampleRand() ?? 0.0; if ($transaction->getSampled() === null) { - $tracesSampler = $this->options->getTracesSampler(); + $tracesSampler = $options->getTracesSampler(); if ($tracesSampler !== null) { $sampleRate = $tracesSampler($samplingContext); @@ -56,15 +51,15 @@ public function startTransaction(Transaction $transaction, TransactionContext $c $sampleRate = $parentSampleRate; $sampleSource = 'parent:sample_rate'; } else { - $sampleRate = $this->getSampleRate( + $sampleRate = self::getSampleRate( $samplingContext->getParentSampled(), - $this->options->getTracesSampleRate() ?? 0 + $options->getTracesSampleRate() ?? 0 ); $sampleSource = $samplingContext->getParentSampled() !== null ? 'parent:sampling_decision' : 'config:traces_sample_rate'; } } - if (!$this->isValidSampleRate($sampleRate)) { + if (!self::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]); @@ -102,23 +97,23 @@ public function startTransaction(Transaction $transaction, TransactionContext $c $transaction->initSpanRecorder(); $profilesSampleSource = 'config:profiles_sample_rate'; - $profilesSampler = $this->options->getProfilesSampler(); + $profilesSampler = $options->getProfilesSampler(); if ($profilesSampler !== null) { $profilesSampleRate = $profilesSampler($samplingContext); $profilesSampleSource = 'config:profiles_sampler'; } else { - $profilesSampleRate = $this->options->getProfilesSampleRate(); + $profilesSampleRate = $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)) { + } elseif (!self::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)) { + } elseif (self::sampleRate($profilesSampleRate)) { $logger->info(\sprintf('Transaction [%s] started profiling because it was sampled.', (string) $transaction->getTraceId())); - $transaction->initProfiler()->start(); + $transaction->initProfiler($options)->start(); } else { $logger->info(\sprintf('Transaction [%s] is not profiling because it was not sampled.', (string) $transaction->getTraceId())); } @@ -126,7 +121,7 @@ public function startTransaction(Transaction $transaction, TransactionContext $c return $transaction; } - private function getSampleRate(?bool $hasParentBeenSampled, float $fallbackSampleRate): float + private static function getSampleRate(?bool $hasParentBeenSampled, float $fallbackSampleRate): float { if ($hasParentBeenSampled === true) { return 1.0; @@ -142,7 +137,7 @@ private function getSampleRate(?bool $hasParentBeenSampled, float $fallbackSampl /** * @param mixed $sampleRate */ - private function sampleRate($sampleRate): bool + private static function sampleRate($sampleRate): bool { if (!\is_float($sampleRate) && !\is_int($sampleRate)) { return false; @@ -162,7 +157,7 @@ private function sampleRate($sampleRate): bool /** * @param mixed $sampleRate */ - private function isValidSampleRate($sampleRate): bool + private static function isValidSampleRate($sampleRate): bool { if (!\is_float($sampleRate) && !\is_int($sampleRate)) { return false; diff --git a/tests/State/HubTest.php b/tests/State/HubTest.php index 972ce617e..5e9bd9837 100644 --- a/tests/State/HubTest.php +++ b/tests/State/HubTest.php @@ -838,7 +838,7 @@ public function testStartTransactionWithCustomSamplingContext(): void public function testStartTransactionStartsProfilerWithProfilesSampler(): void { $client = $this->createMock(ClientInterface::class); - $client->expects($this->exactly(2)) + $client->expects($this->once()) ->method('getOptions') ->willReturn(new Options([ 'traces_sample_rate' => 1.0, diff --git a/tests/Tracing/TransactionSamplerTest.php b/tests/Tracing/TransactionSamplerTest.php index 1fe3aab72..9118e9c12 100644 --- a/tests/Tracing/TransactionSamplerTest.php +++ b/tests/Tracing/TransactionSamplerTest.php @@ -5,9 +5,7 @@ namespace Sentry\Tests\Tracing; use PHPUnit\Framework\TestCase; -use Sentry\ClientInterface; use Sentry\Options; -use Sentry\State\Hub; use Sentry\Tracing\DynamicSamplingContext; use Sentry\Tracing\SamplingContext; use Sentry\Tracing\Transaction; @@ -320,11 +318,6 @@ public function testUpdatesTheDscSampleRate(): void */ private function sampleTransaction(Options $options, TransactionContext $transactionContext, array $customSamplingContext = []): Transaction { - $client = $this->createMock(ClientInterface::class); - $client->method('getOptions')->willReturn($options); - - $transaction = new Transaction($transactionContext, new Hub($client)); - - return (new TransactionSampler($options))->startTransaction($transaction, $transactionContext, $customSamplingContext); + return TransactionSampler::startTransaction($options, $transactionContext, $customSamplingContext); } }