Skip to content

Commit 012018d

Browse files
committed
feat(scopes): use TransactionSampler
1 parent 0878a69 commit 012018d

5 files changed

Lines changed: 23 additions & 36 deletions

File tree

src/State/Hub.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,7 @@ public function getIntegration(string $className): ?IntegrationInterface
236236
*/
237237
public function startTransaction(TransactionContext $context, array $customSamplingContext = []): Transaction
238238
{
239-
$transaction = new Transaction($context, $this);
240-
241-
return (new TransactionSampler($this->getClient()->getOptions()))->startTransaction($transaction, $context, $customSamplingContext);
239+
return TransactionSampler::startTransaction($this->getClient()->getOptions(), $context, $customSamplingContext);
242240
}
243241

244242
/**

src/Tracing/Transaction.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Sentry\Event;
88
use Sentry\EventId;
9+
use Sentry\Options;
910
use Sentry\Profiling\Profiler;
1011
use Sentry\SentrySdk;
1112
use Sentry\State\HubInterface;
@@ -119,10 +120,10 @@ public function initSpanRecorder(int $maxSpans = 1000): self
119120
return $this;
120121
}
121122

122-
public function initProfiler(): Profiler
123+
public function initProfiler(?Options $options = null): Profiler
123124
{
124125
if ($this->profiler === null) {
125-
$this->profiler = new Profiler($this->hub->getClient()->getOptions());
126+
$this->profiler = new Profiler($options ?? $this->hub->getClient()->getOptions());
126127
}
127128

128129
return $this->profiler;

src/Tracing/TransactionSampler.php

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,19 @@
1313
*/
1414
final class TransactionSampler
1515
{
16-
/**
17-
* @var Options
18-
*/
19-
private $options;
20-
21-
public function __construct(Options $options)
16+
private function __construct()
2217
{
23-
$this->options = $options;
2418
}
2519

2620
/**
2721
* @param array<string, mixed> $customSamplingContext Additional context that will be passed to the {@see SamplingContext}
2822
*/
29-
public function startTransaction(Transaction $transaction, TransactionContext $context, array $customSamplingContext = []): Transaction
23+
public static function startTransaction(Options $options, TransactionContext $context, array $customSamplingContext = []): Transaction
3024
{
31-
$logger = $this->options->getLoggerOrNullLogger();
25+
$transaction = new Transaction($context);
26+
$logger = $options->getLoggerOrNullLogger();
3227

33-
if (!$this->options->isTracingEnabled()) {
28+
if (!$options->isTracingEnabled()) {
3429
$transaction->setSampled(false);
3530

3631
$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
4540
$sampleRand = $context->getMetadata()->getSampleRand() ?? 0.0;
4641

4742
if ($transaction->getSampled() === null) {
48-
$tracesSampler = $this->options->getTracesSampler();
43+
$tracesSampler = $options->getTracesSampler();
4944

5045
if ($tracesSampler !== null) {
5146
$sampleRate = $tracesSampler($samplingContext);
@@ -56,15 +51,15 @@ public function startTransaction(Transaction $transaction, TransactionContext $c
5651
$sampleRate = $parentSampleRate;
5752
$sampleSource = 'parent:sample_rate';
5853
} else {
59-
$sampleRate = $this->getSampleRate(
54+
$sampleRate = self::getSampleRate(
6055
$samplingContext->getParentSampled(),
61-
$this->options->getTracesSampleRate() ?? 0
56+
$options->getTracesSampleRate() ?? 0
6257
);
6358
$sampleSource = $samplingContext->getParentSampled() !== null ? 'parent:sampling_decision' : 'config:traces_sample_rate';
6459
}
6560
}
6661

67-
if (!$this->isValidSampleRate($sampleRate)) {
62+
if (!self::isValidSampleRate($sampleRate)) {
6863
$transaction->setSampled(false);
6964

7065
$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,31 +97,31 @@ public function startTransaction(Transaction $transaction, TransactionContext $c
10297
$transaction->initSpanRecorder();
10398

10499
$profilesSampleSource = 'config:profiles_sample_rate';
105-
$profilesSampler = $this->options->getProfilesSampler();
100+
$profilesSampler = $options->getProfilesSampler();
106101

107102
if ($profilesSampler !== null) {
108103
$profilesSampleRate = $profilesSampler($samplingContext);
109104
$profilesSampleSource = 'config:profiles_sampler';
110105
} else {
111-
$profilesSampleRate = $this->options->getProfilesSampleRate();
106+
$profilesSampleRate = $options->getProfilesSampleRate();
112107
}
113108

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

121-
$transaction->initProfiler()->start();
116+
$transaction->initProfiler($options)->start();
122117
} else {
123118
$logger->info(\sprintf('Transaction [%s] is not profiling because it was not sampled.', (string) $transaction->getTraceId()));
124119
}
125120

126121
return $transaction;
127122
}
128123

129-
private function getSampleRate(?bool $hasParentBeenSampled, float $fallbackSampleRate): float
124+
private static function getSampleRate(?bool $hasParentBeenSampled, float $fallbackSampleRate): float
130125
{
131126
if ($hasParentBeenSampled === true) {
132127
return 1.0;
@@ -142,7 +137,7 @@ private function getSampleRate(?bool $hasParentBeenSampled, float $fallbackSampl
142137
/**
143138
* @param mixed $sampleRate
144139
*/
145-
private function sampleRate($sampleRate): bool
140+
private static function sampleRate($sampleRate): bool
146141
{
147142
if (!\is_float($sampleRate) && !\is_int($sampleRate)) {
148143
return false;
@@ -162,7 +157,7 @@ private function sampleRate($sampleRate): bool
162157
/**
163158
* @param mixed $sampleRate
164159
*/
165-
private function isValidSampleRate($sampleRate): bool
160+
private static function isValidSampleRate($sampleRate): bool
166161
{
167162
if (!\is_float($sampleRate) && !\is_int($sampleRate)) {
168163
return false;

tests/State/HubTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ public function testStartTransactionWithCustomSamplingContext(): void
838838
public function testStartTransactionStartsProfilerWithProfilesSampler(): void
839839
{
840840
$client = $this->createMock(ClientInterface::class);
841-
$client->expects($this->exactly(2))
841+
$client->expects($this->once())
842842
->method('getOptions')
843843
->willReturn(new Options([
844844
'traces_sample_rate' => 1.0,

tests/Tracing/TransactionSamplerTest.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
namespace Sentry\Tests\Tracing;
66

77
use PHPUnit\Framework\TestCase;
8-
use Sentry\ClientInterface;
98
use Sentry\Options;
10-
use Sentry\State\Hub;
119
use Sentry\Tracing\DynamicSamplingContext;
1210
use Sentry\Tracing\SamplingContext;
1311
use Sentry\Tracing\Transaction;
@@ -320,11 +318,6 @@ public function testUpdatesTheDscSampleRate(): void
320318
*/
321319
private function sampleTransaction(Options $options, TransactionContext $transactionContext, array $customSamplingContext = []): Transaction
322320
{
323-
$client = $this->createMock(ClientInterface::class);
324-
$client->method('getOptions')->willReturn($options);
325-
326-
$transaction = new Transaction($transactionContext, new Hub($client));
327-
328-
return (new TransactionSampler($options))->startTransaction($transaction, $transactionContext, $customSamplingContext);
321+
return TransactionSampler::startTransaction($options, $transactionContext, $customSamplingContext);
329322
}
330323
}

0 commit comments

Comments
 (0)