Skip to content

Commit 0878a69

Browse files
committed
Revert "make TransactionSampler static"
This reverts commit f28ee44.
1 parent 4737456 commit 0878a69

8 files changed

Lines changed: 252 additions & 150 deletions

File tree

src/State/Hub.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Sentry\Tracing\Span;
2020
use Sentry\Tracing\Transaction;
2121
use Sentry\Tracing\TransactionContext;
22+
use Sentry\Tracing\TransactionSampler;
2223

2324
/**
2425
* This class is a basic implementation of the {@see HubInterface} interface.
@@ -235,7 +236,9 @@ public function getIntegration(string $className): ?IntegrationInterface
235236
*/
236237
public function startTransaction(TransactionContext $context, array $customSamplingContext = []): Transaction
237238
{
238-
return \Sentry\startTransaction($context, $customSamplingContext);
239+
$transaction = new Transaction($context, $this);
240+
241+
return (new TransactionSampler($this->getClient()->getOptions()))->startTransaction($transaction, $context, $customSamplingContext);
239242
}
240243

241244
/**

src/Tracing/TransactionSampler.php

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,30 @@
77
use Sentry\Options;
88

99
/**
10+
* Applies tracing and profiling sampling decisions to transactions.
11+
*
1012
* @internal
1113
*/
1214
final class TransactionSampler
1315
{
14-
private function __construct()
16+
/**
17+
* @var Options
18+
*/
19+
private $options;
20+
21+
public function __construct(Options $options)
1522
{
23+
$this->options = $options;
1624
}
1725

1826
/**
1927
* @param array<string, mixed> $customSamplingContext Additional context that will be passed to the {@see SamplingContext}
2028
*/
21-
public static function startTransaction(Options $options, TransactionContext $context, array $customSamplingContext = []): Transaction
29+
public function startTransaction(Transaction $transaction, TransactionContext $context, array $customSamplingContext = []): Transaction
2230
{
23-
$transaction = new Transaction($context);
24-
$logger = $options->getLoggerOrNullLogger();
31+
$logger = $this->options->getLoggerOrNullLogger();
2532

26-
if (!$options->isTracingEnabled()) {
33+
if (!$this->options->isTracingEnabled()) {
2734
$transaction->setSampled(false);
2835

2936
$logger->warning(\sprintf('Transaction [%s] was started but tracing is not enabled.', (string) $transaction->getTraceId()), ['context' => $context]);
@@ -38,7 +45,7 @@ public static function startTransaction(Options $options, TransactionContext $co
3845
$sampleRand = $context->getMetadata()->getSampleRand() ?? 0.0;
3946

4047
if ($transaction->getSampled() === null) {
41-
$tracesSampler = $options->getTracesSampler();
48+
$tracesSampler = $this->options->getTracesSampler();
4249

4350
if ($tracesSampler !== null) {
4451
$sampleRate = $tracesSampler($samplingContext);
@@ -49,15 +56,15 @@ public static function startTransaction(Options $options, TransactionContext $co
4956
$sampleRate = $parentSampleRate;
5057
$sampleSource = 'parent:sample_rate';
5158
} else {
52-
$sampleRate = self::getSampleRate(
59+
$sampleRate = $this->getSampleRate(
5360
$samplingContext->getParentSampled(),
54-
$options->getTracesSampleRate() ?? 0
61+
$this->options->getTracesSampleRate() ?? 0
5562
);
5663
$sampleSource = $samplingContext->getParentSampled() !== null ? 'parent:sampling_decision' : 'config:traces_sample_rate';
5764
}
5865
}
5966

60-
if (!self::isValidSampleRate($sampleRate)) {
67+
if (!$this->isValidSampleRate($sampleRate)) {
6168
$transaction->setSampled(false);
6269

6370
$logger->warning(\sprintf('Transaction [%s] was started but not sampled because sample rate (decided by %s) is invalid.', (string) $transaction->getTraceId(), $sampleSource), ['context' => $context]);
@@ -95,20 +102,20 @@ public static function startTransaction(Options $options, TransactionContext $co
95102
$transaction->initSpanRecorder();
96103

97104
$profilesSampleSource = 'config:profiles_sample_rate';
98-
$profilesSampler = $options->getProfilesSampler();
105+
$profilesSampler = $this->options->getProfilesSampler();
99106

100107
if ($profilesSampler !== null) {
101108
$profilesSampleRate = $profilesSampler($samplingContext);
102109
$profilesSampleSource = 'config:profiles_sampler';
103110
} else {
104-
$profilesSampleRate = $options->getProfilesSampleRate();
111+
$profilesSampleRate = $this->options->getProfilesSampleRate();
105112
}
106113

107114
if ($profilesSampleRate === null) {
108115
$logger->info(\sprintf('Transaction [%s] is not profiling because neither `profiles_sample_rate` nor `profiles_sampler` option is set.', (string) $transaction->getTraceId()));
109-
} elseif (!self::isValidSampleRate($profilesSampleRate)) {
116+
} elseif (!$this->isValidSampleRate($profilesSampleRate)) {
110117
$logger->warning(\sprintf('Transaction [%s] is not profiling because profile sample rate (decided by %s) is invalid.', (string) $transaction->getTraceId(), $profilesSampleSource));
111-
} elseif (self::sampleRate($profilesSampleRate)) {
118+
} elseif ($this->sampleRate($profilesSampleRate)) {
112119
$logger->info(\sprintf('Transaction [%s] started profiling because it was sampled.', (string) $transaction->getTraceId()));
113120

114121
$transaction->initProfiler()->start();
@@ -119,7 +126,7 @@ public static function startTransaction(Options $options, TransactionContext $co
119126
return $transaction;
120127
}
121128

122-
private static function getSampleRate(?bool $hasParentBeenSampled, float $fallbackSampleRate): float
129+
private function getSampleRate(?bool $hasParentBeenSampled, float $fallbackSampleRate): float
123130
{
124131
if ($hasParentBeenSampled === true) {
125132
return 1.0;
@@ -135,7 +142,7 @@ private static function getSampleRate(?bool $hasParentBeenSampled, float $fallba
135142
/**
136143
* @param mixed $sampleRate
137144
*/
138-
private static function sampleRate($sampleRate): bool
145+
private function sampleRate($sampleRate): bool
139146
{
140147
if (!\is_float($sampleRate) && !\is_int($sampleRate)) {
141148
return false;
@@ -155,7 +162,7 @@ private static function sampleRate($sampleRate): bool
155162
/**
156163
* @param mixed $sampleRate
157164
*/
158-
private static function isValidSampleRate($sampleRate): bool
165+
private function isValidSampleRate($sampleRate): bool
159166
{
160167
if (!\is_float($sampleRate) && !\is_int($sampleRate)) {
161168
return false;

src/functions.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Sentry\Tracing\SpanContext;
1616
use Sentry\Tracing\Transaction;
1717
use Sentry\Tracing\TransactionContext;
18-
use Sentry\Tracing\TransactionSampler;
1918
use Sentry\Transport\TransportInterface;
2019

2120
/**
@@ -269,7 +268,7 @@ function withContext(callable $callback, ?int $timeout = null)
269268
*/
270269
function startTransaction(TransactionContext $context, array $customSamplingContext = []): Transaction
271270
{
272-
return TransactionSampler::startTransaction(SentrySdk::getClient()->getOptions(), $context, $customSamplingContext);
271+
return SentrySdk::getCurrentHub()->startTransaction($context, $customSamplingContext);
273272
}
274273

275274
/**

tests/FunctionsTest.php

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use PHPUnit\Framework\TestCase;
99
use Sentry\Breadcrumb;
1010
use Sentry\CheckInStatus;
11-
use Sentry\Client;
1211
use Sentry\ClientInterface;
1312
use Sentry\Event;
1413
use Sentry\EventHint;
@@ -24,7 +23,6 @@
2423
use Sentry\State\HubInterface;
2524
use Sentry\State\Scope;
2625
use Sentry\Tracing\PropagationContext;
27-
use Sentry\Tracing\SamplingContext;
2826
use Sentry\Tracing\Span;
2927
use Sentry\Tracing\SpanContext;
3028
use Sentry\Tracing\SpanId;
@@ -454,28 +452,18 @@ public function testWithContextAlwaysEndsContextWithOptionalTimeout(): void
454452
public function testStartTransaction(): void
455453
{
456454
$transactionContext = new TransactionContext('foo');
455+
$transaction = new Transaction($transactionContext);
457456
$customSamplingContext = ['foo' => 'bar'];
458-
$samplerInvoked = false;
459457

460-
$client = $this->createMock(ClientInterface::class);
461-
$client->expects($this->once())
462-
->method('getOptions')
463-
->willReturn(new Options([
464-
'traces_sampler' => function (SamplingContext $samplingContext) use ($customSamplingContext, &$samplerInvoked): float {
465-
$this->assertSame($customSamplingContext, $samplingContext->getAdditionalContext());
466-
$samplerInvoked = true;
467-
468-
return 1.0;
469-
},
470-
]));
471-
472-
SentrySdk::init($client);
458+
$hub = $this->createMock(HubInterface::class);
459+
$hub->expects($this->once())
460+
->method('startTransaction')
461+
->with($transactionContext, $customSamplingContext)
462+
->willReturn($transaction);
473463

474-
$transaction = startTransaction($transactionContext, $customSamplingContext);
464+
SentrySdk::setCurrentHub($hub);
475465

476-
$this->assertSame('foo', $transaction->getName());
477-
$this->assertTrue($transaction->getSampled());
478-
$this->assertTrue($samplerInvoked);
466+
$this->assertSame($transaction, startTransaction($transactionContext, $customSamplingContext));
479467
}
480468

481469
public function testTraceReturnsClosureResult(): void
@@ -632,15 +620,17 @@ public function testBaggageWithTracingDisabled(): void
632620

633621
public function testBaggageWithTracingEnabled(): void
634622
{
635-
$client = new Client(new Options([
636-
'traces_sample_rate' => 1.0,
637-
'release' => '1.0.0',
638-
'environment' => 'development',
639-
]), StubTransport::getInstance());
623+
$client = $this->createMock(ClientInterface::class);
624+
$client->expects($this->atLeastOnce())
625+
->method('getOptions')
626+
->willReturn(new Options([
627+
'traces_sample_rate' => 1.0,
628+
'release' => '1.0.0',
629+
'environment' => 'development',
630+
]));
640631

641632
$hub = new Hub($client);
642633

643-
SentrySdk::getGlobalScope()->setClient($client);
644634
SentrySdk::setCurrentHub($hub);
645635

646636
$transactionContext = new TransactionContext();

0 commit comments

Comments
 (0)