From db6e223cb62d572dc200521ed9e759b8edeee590 Mon Sep 17 00:00:00 2001 From: Martin Linzmayer Date: Mon, 15 Jun 2026 16:22:48 +0200 Subject: [PATCH] feat(scope): remove Hub from Transaction --- src/Tracing/DynamicSamplingContext.php | 6 ++--- src/Tracing/Transaction.php | 16 ++++---------- tests/FunctionsTest.php | 1 + tests/Tracing/DynamicSamplingContextTest.php | 23 +++++++------------- tests/Tracing/TransactionTest.php | 22 +++++++++---------- 5 files changed, 26 insertions(+), 42 deletions(-) diff --git a/src/Tracing/DynamicSamplingContext.php b/src/Tracing/DynamicSamplingContext.php index 410b4fc53..588eead06 100644 --- a/src/Tracing/DynamicSamplingContext.php +++ b/src/Tracing/DynamicSamplingContext.php @@ -4,8 +4,8 @@ namespace Sentry\Tracing; +use Sentry\ClientInterface; use Sentry\Options; -use Sentry\State\HubInterface; use Sentry\State\Scope; /** @@ -149,7 +149,7 @@ public static function fromHeader(string $header): self * * @see https://develop.sentry.dev/sdk/performance/dynamic-sampling-context/#baggage-header */ - public static function fromTransaction(Transaction $transaction, HubInterface $hub): self + public static function fromTransaction(Transaction $transaction, ClientInterface $client): self { $samplingContext = new self(); $samplingContext->set('trace_id', (string) $transaction->getTraceId()); @@ -164,8 +164,6 @@ public static function fromTransaction(Transaction $transaction, HubInterface $h $samplingContext->set('transaction', $transaction->getName()); } - $client = $hub->getClient(); - self::setOrgOptions($client->getOptions(), $samplingContext); if ($transaction->getSampled() !== null) { diff --git a/src/Tracing/Transaction.php b/src/Tracing/Transaction.php index 34134b855..89086bc96 100644 --- a/src/Tracing/Transaction.php +++ b/src/Tracing/Transaction.php @@ -9,18 +9,12 @@ use Sentry\Options; use Sentry\Profiling\Profiler; use Sentry\SentrySdk; -use Sentry\State\HubInterface; /** * This class stores all the information about a Transaction. */ final class Transaction extends Span { - /** - * @var HubInterface The hub instance - */ - private $hub; - /** * @var string Name of the transaction */ @@ -45,15 +39,13 @@ final class Transaction extends Span * Span constructor. * * @param TransactionContext $context The context to create the transaction with - * @param HubInterface|null $hub Instance of a hub to flush the transaction * * @internal */ - public function __construct(TransactionContext $context, ?HubInterface $hub = null) + public function __construct(TransactionContext $context) { parent::__construct($context); - $this->hub = $hub ?? SentrySdk::getCurrentHub(); $this->name = $context->getName(); $this->metadata = $context->getMetadata(); $this->transaction = $this; @@ -98,7 +90,7 @@ public function getDynamicSamplingContext(): DynamicSamplingContext return $this->metadata->getDynamicSamplingContext(); } - $samplingContext = DynamicSamplingContext::fromTransaction($this->transaction, $this->hub); + $samplingContext = DynamicSamplingContext::fromTransaction($this->transaction, SentrySdk::getClient()); $this->getMetadata()->setDynamicSamplingContext($samplingContext); return $samplingContext; @@ -123,7 +115,7 @@ public function initSpanRecorder(int $maxSpans = 1000): self public function initProfiler(?Options $options = null): Profiler { if ($this->profiler === null) { - $this->profiler = new Profiler($options ?? $this->hub->getClient()->getOptions()); + $this->profiler = new Profiler($options ?? SentrySdk::getClient()->getOptions()); } return $this->profiler; @@ -188,6 +180,6 @@ public function finish(?float $endTimestamp = null): ?EventId } } - return $this->hub->captureEvent($event); + return \Sentry\captureEvent($event); } } diff --git a/tests/FunctionsTest.php b/tests/FunctionsTest.php index ba349bde9..4d5c5166a 100644 --- a/tests/FunctionsTest.php +++ b/tests/FunctionsTest.php @@ -631,6 +631,7 @@ public function testBaggageWithTracingEnabled(): void $hub = new Hub($client); + SentrySdk::getGlobalScope()->setClient($client); SentrySdk::setCurrentHub($hub); $transactionContext = new TransactionContext(); diff --git a/tests/Tracing/DynamicSamplingContextTest.php b/tests/Tracing/DynamicSamplingContextTest.php index 0996aa702..7f1c153c4 100644 --- a/tests/Tracing/DynamicSamplingContextTest.php +++ b/tests/Tracing/DynamicSamplingContextTest.php @@ -8,7 +8,6 @@ use Sentry\ClientInterface; use Sentry\NoOpClient; use Sentry\Options; -use Sentry\State\Hub; use Sentry\State\Scope; use Sentry\Tracing\DynamicSamplingContext; use Sentry\Tracing\PropagationContext; @@ -91,16 +90,14 @@ public function testFromTransaction(): void 'environment' => 'test', ])); - $hub = new Hub($client); - $transactionContext = new TransactionContext(); $transactionContext->setName('foo'); - $transaction = new Transaction($transactionContext, $hub); + $transaction = new Transaction($transactionContext); $transaction->getMetadata()->setSamplingRate(1.0); $transaction->getMetadata()->setSampleRand(0.5); - $samplingContext = DynamicSamplingContext::fromTransaction($transaction, $hub); + $samplingContext = DynamicSamplingContext::fromTransaction($transaction, $client); $this->assertSame((string) $transaction->getTraceId(), $samplingContext->get('trace_id')); $this->assertSame((string) $transaction->getMetaData()->getSamplingRate(), $samplingContext->get('sample_rate')); @@ -114,15 +111,13 @@ public function testFromTransaction(): void public function testFromTransactionSourceUrl(): void { - $hub = new Hub(new NoOpClient()); - $transactionContext = new TransactionContext(); $transactionContext->setName('/foo/bar/123'); $transactionContext->setSource(TransactionSource::url()); - $transaction = new Transaction($transactionContext, $hub); + $transaction = new Transaction($transactionContext); - $samplingContext = DynamicSamplingContext::fromTransaction($transaction, $hub); + $samplingContext = DynamicSamplingContext::fromTransaction($transaction, new NoOpClient()); $this->assertNull($samplingContext->get('transaction')); } @@ -189,9 +184,8 @@ public function testFromTransactionUsesConfiguredOrgIdOverDsnOrgId(): void 'org_id' => 2, ])); - $hub = new Hub($client); - $transaction = new Transaction(new TransactionContext(), $hub); - $samplingContext = DynamicSamplingContext::fromTransaction($transaction, $hub); + $transaction = new Transaction(new TransactionContext()); + $samplingContext = DynamicSamplingContext::fromTransaction($transaction, $client); $this->assertSame('2', $samplingContext->get('org_id')); } @@ -205,9 +199,8 @@ public function testFromTransactionFallsBackToDsnOrgId(): void 'dsn' => 'http://public@o1.example.com/1', ])); - $hub = new Hub($client); - $transaction = new Transaction(new TransactionContext(), $hub); - $samplingContext = DynamicSamplingContext::fromTransaction($transaction, $hub); + $transaction = new Transaction(new TransactionContext()); + $samplingContext = DynamicSamplingContext::fromTransaction($transaction, $client); $this->assertSame('1', $samplingContext->get('org_id')); } diff --git a/tests/Tracing/TransactionTest.php b/tests/Tracing/TransactionTest.php index 76a2c3aab..82a767bd3 100644 --- a/tests/Tracing/TransactionTest.php +++ b/tests/Tracing/TransactionTest.php @@ -10,8 +10,9 @@ use Sentry\EventId; use Sentry\EventType; use Sentry\Options; +use Sentry\SentrySdk; use Sentry\State\Hub; -use Sentry\State\HubInterface; +use Sentry\State\Scope; use Sentry\Tests\TestUtil\ClockMock; use Sentry\Tracing\SpanContext; use Sentry\Tracing\Transaction; @@ -37,19 +38,16 @@ public function testFinish(): void ->method('getOptions') ->willReturn(new Options()); - $hub = $this->createMock(HubInterface::class); - $hub->expects($this->once()) - ->method('getClient') - ->willReturn($client); + SentrySdk::init($client); - $transaction = new Transaction($transactionContext, $hub); + $transaction = new Transaction($transactionContext); $transaction->initSpanRecorder(); $span1 = $transaction->startChild(new SpanContext()); $span2 = $transaction->startChild(new SpanContext()); $span3 = $transaction->startChild(new SpanContext()); // This span isn't finished, so it should not be included in the event - $hub->expects($this->once()) + $client->expects($this->once()) ->method('captureEvent') ->with($this->callback(function (Event $eventArg) use ($transactionContext, $span1, $span2): bool { $this->assertSame(EventType::transaction(), $eventArg->getType()); @@ -60,7 +58,7 @@ public function testFinish(): void $this->assertSame([$span1, $span2], $eventArg->getSpans()); return true; - })) + }), null, $this->isInstanceOf(Scope::class)) ->willReturnCallback(static function (Event $eventArg) use (&$expectedEventId): EventId { $expectedEventId = $eventArg->getId(); @@ -77,11 +75,13 @@ public function testFinish(): void public function testFinishDoesNothingIfSampledFlagIsNotTrue(): void { - $hub = $this->createMock(HubInterface::class); - $hub->expects($this->never()) + $client = $this->createMock(ClientInterface::class); + $client->expects($this->never()) ->method('captureEvent'); - $transaction = new Transaction(new TransactionContext(), $hub); + SentrySdk::init($client); + + $transaction = new Transaction(new TransactionContext()); $transaction->finish(); }