Skip to content

Commit 0352e56

Browse files
committed
feat(scope): remove Hub from Transaction
1 parent 012018d commit 0352e56

5 files changed

Lines changed: 26 additions & 42 deletions

File tree

src/Tracing/DynamicSamplingContext.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
namespace Sentry\Tracing;
66

7+
use Sentry\ClientInterface;
78
use Sentry\Options;
8-
use Sentry\State\HubInterface;
99
use Sentry\State\Scope;
1010

1111
/**
@@ -149,7 +149,7 @@ public static function fromHeader(string $header): self
149149
*
150150
* @see https://develop.sentry.dev/sdk/performance/dynamic-sampling-context/#baggage-header
151151
*/
152-
public static function fromTransaction(Transaction $transaction, HubInterface $hub): self
152+
public static function fromTransaction(Transaction $transaction, ClientInterface $client): self
153153
{
154154
$samplingContext = new self();
155155
$samplingContext->set('trace_id', (string) $transaction->getTraceId());
@@ -164,8 +164,6 @@ public static function fromTransaction(Transaction $transaction, HubInterface $h
164164
$samplingContext->set('transaction', $transaction->getName());
165165
}
166166

167-
$client = $hub->getClient();
168-
169167
self::setOrgOptions($client->getOptions(), $samplingContext);
170168

171169
if ($transaction->getSampled() !== null) {

src/Tracing/Transaction.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,12 @@
99
use Sentry\Options;
1010
use Sentry\Profiling\Profiler;
1111
use Sentry\SentrySdk;
12-
use Sentry\State\HubInterface;
1312

1413
/**
1514
* This class stores all the information about a Transaction.
1615
*/
1716
final class Transaction extends Span
1817
{
19-
/**
20-
* @var HubInterface The hub instance
21-
*/
22-
private $hub;
23-
2418
/**
2519
* @var string Name of the transaction
2620
*/
@@ -45,15 +39,13 @@ final class Transaction extends Span
4539
* Span constructor.
4640
*
4741
* @param TransactionContext $context The context to create the transaction with
48-
* @param HubInterface|null $hub Instance of a hub to flush the transaction
4942
*
5043
* @internal
5144
*/
52-
public function __construct(TransactionContext $context, ?HubInterface $hub = null)
45+
public function __construct(TransactionContext $context)
5346
{
5447
parent::__construct($context);
5548

56-
$this->hub = $hub ?? SentrySdk::getCurrentHub();
5749
$this->name = $context->getName();
5850
$this->metadata = $context->getMetadata();
5951
$this->transaction = $this;
@@ -98,7 +90,7 @@ public function getDynamicSamplingContext(): DynamicSamplingContext
9890
return $this->metadata->getDynamicSamplingContext();
9991
}
10092

101-
$samplingContext = DynamicSamplingContext::fromTransaction($this->transaction, $this->hub);
93+
$samplingContext = DynamicSamplingContext::fromTransaction($this->transaction, SentrySdk::getClient());
10294
$this->getMetadata()->setDynamicSamplingContext($samplingContext);
10395

10496
return $samplingContext;
@@ -123,7 +115,7 @@ public function initSpanRecorder(int $maxSpans = 1000): self
123115
public function initProfiler(?Options $options = null): Profiler
124116
{
125117
if ($this->profiler === null) {
126-
$this->profiler = new Profiler($options ?? $this->hub->getClient()->getOptions());
118+
$this->profiler = new Profiler($options ?? SentrySdk::getClient()->getOptions());
127119
}
128120

129121
return $this->profiler;
@@ -188,6 +180,6 @@ public function finish(?float $endTimestamp = null): ?EventId
188180
}
189181
}
190182

191-
return $this->hub->captureEvent($event);
183+
return \Sentry\captureEvent($event);
192184
}
193185
}

tests/FunctionsTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,7 @@ public function testBaggageWithTracingEnabled(): void
631631

632632
$hub = new Hub($client);
633633

634+
SentrySdk::getGlobalScope()->setClient($client);
634635
SentrySdk::setCurrentHub($hub);
635636

636637
$transactionContext = new TransactionContext();

tests/Tracing/DynamicSamplingContextTest.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Sentry\ClientInterface;
99
use Sentry\NoOpClient;
1010
use Sentry\Options;
11-
use Sentry\State\Hub;
1211
use Sentry\State\Scope;
1312
use Sentry\Tracing\DynamicSamplingContext;
1413
use Sentry\Tracing\PropagationContext;
@@ -91,16 +90,14 @@ public function testFromTransaction(): void
9190
'environment' => 'test',
9291
]));
9392

94-
$hub = new Hub($client);
95-
9693
$transactionContext = new TransactionContext();
9794
$transactionContext->setName('foo');
9895

99-
$transaction = new Transaction($transactionContext, $hub);
96+
$transaction = new Transaction($transactionContext);
10097
$transaction->getMetadata()->setSamplingRate(1.0);
10198
$transaction->getMetadata()->setSampleRand(0.5);
10299

103-
$samplingContext = DynamicSamplingContext::fromTransaction($transaction, $hub);
100+
$samplingContext = DynamicSamplingContext::fromTransaction($transaction, $client);
104101

105102
$this->assertSame((string) $transaction->getTraceId(), $samplingContext->get('trace_id'));
106103
$this->assertSame((string) $transaction->getMetaData()->getSamplingRate(), $samplingContext->get('sample_rate'));
@@ -114,15 +111,13 @@ public function testFromTransaction(): void
114111

115112
public function testFromTransactionSourceUrl(): void
116113
{
117-
$hub = new Hub(new NoOpClient());
118-
119114
$transactionContext = new TransactionContext();
120115
$transactionContext->setName('/foo/bar/123');
121116
$transactionContext->setSource(TransactionSource::url());
122117

123-
$transaction = new Transaction($transactionContext, $hub);
118+
$transaction = new Transaction($transactionContext);
124119

125-
$samplingContext = DynamicSamplingContext::fromTransaction($transaction, $hub);
120+
$samplingContext = DynamicSamplingContext::fromTransaction($transaction, new NoOpClient());
126121

127122
$this->assertNull($samplingContext->get('transaction'));
128123
}
@@ -189,9 +184,8 @@ public function testFromTransactionUsesConfiguredOrgIdOverDsnOrgId(): void
189184
'org_id' => 2,
190185
]));
191186

192-
$hub = new Hub($client);
193-
$transaction = new Transaction(new TransactionContext(), $hub);
194-
$samplingContext = DynamicSamplingContext::fromTransaction($transaction, $hub);
187+
$transaction = new Transaction(new TransactionContext());
188+
$samplingContext = DynamicSamplingContext::fromTransaction($transaction, $client);
195189

196190
$this->assertSame('2', $samplingContext->get('org_id'));
197191
}
@@ -205,9 +199,8 @@ public function testFromTransactionFallsBackToDsnOrgId(): void
205199
'dsn' => 'http://public@o1.example.com/1',
206200
]));
207201

208-
$hub = new Hub($client);
209-
$transaction = new Transaction(new TransactionContext(), $hub);
210-
$samplingContext = DynamicSamplingContext::fromTransaction($transaction, $hub);
202+
$transaction = new Transaction(new TransactionContext());
203+
$samplingContext = DynamicSamplingContext::fromTransaction($transaction, $client);
211204

212205
$this->assertSame('1', $samplingContext->get('org_id'));
213206
}

tests/Tracing/TransactionTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
use Sentry\EventId;
1111
use Sentry\EventType;
1212
use Sentry\Options;
13+
use Sentry\SentrySdk;
1314
use Sentry\State\Hub;
14-
use Sentry\State\HubInterface;
15+
use Sentry\State\Scope;
1516
use Sentry\Tests\TestUtil\ClockMock;
1617
use Sentry\Tracing\SpanContext;
1718
use Sentry\Tracing\Transaction;
@@ -37,19 +38,16 @@ public function testFinish(): void
3738
->method('getOptions')
3839
->willReturn(new Options());
3940

40-
$hub = $this->createMock(HubInterface::class);
41-
$hub->expects($this->once())
42-
->method('getClient')
43-
->willReturn($client);
41+
SentrySdk::init($client);
4442

45-
$transaction = new Transaction($transactionContext, $hub);
43+
$transaction = new Transaction($transactionContext);
4644
$transaction->initSpanRecorder();
4745

4846
$span1 = $transaction->startChild(new SpanContext());
4947
$span2 = $transaction->startChild(new SpanContext());
5048
$span3 = $transaction->startChild(new SpanContext()); // This span isn't finished, so it should not be included in the event
5149

52-
$hub->expects($this->once())
50+
$client->expects($this->once())
5351
->method('captureEvent')
5452
->with($this->callback(function (Event $eventArg) use ($transactionContext, $span1, $span2): bool {
5553
$this->assertSame(EventType::transaction(), $eventArg->getType());
@@ -60,7 +58,7 @@ public function testFinish(): void
6058
$this->assertSame([$span1, $span2], $eventArg->getSpans());
6159

6260
return true;
63-
}))
61+
}), null, $this->isInstanceOf(Scope::class))
6462
->willReturnCallback(static function (Event $eventArg) use (&$expectedEventId): EventId {
6563
$expectedEventId = $eventArg->getId();
6664

@@ -77,11 +75,13 @@ public function testFinish(): void
7775

7876
public function testFinishDoesNothingIfSampledFlagIsNotTrue(): void
7977
{
80-
$hub = $this->createMock(HubInterface::class);
81-
$hub->expects($this->never())
78+
$client = $this->createMock(ClientInterface::class);
79+
$client->expects($this->never())
8280
->method('captureEvent');
8381

84-
$transaction = new Transaction(new TransactionContext(), $hub);
82+
SentrySdk::init($client);
83+
84+
$transaction = new Transaction(new TransactionContext());
8585
$transaction->finish();
8686
}
8787

0 commit comments

Comments
 (0)