From ac94f8065b4768952ab774f28e5c57b744f7697d Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 28 Sep 2025 15:29:02 +0800 Subject: [PATCH 1/8] refactor(sentry): optimize hub management and listener priorities - Simplify SentrySdk::setCurrentHub() by removing unnecessary tap usage - Add explicit priorities to event listeners for deterministic execution order - Optimize EventHandleListener by removing redundant hub context management - Improve Tracer initialization to use existing hub instead of creating new ones - Clear breadcrumbs on new transaction scope to prevent cross-request pollution --- src/sentry/class_map/SentrySdk.php | 2 +- src/sentry/src/ConfigProvider.php | 6 +++--- .../src/Listener/EventHandleListener.php | 18 +++++++----------- src/sentry/src/Tracing/Tracer.php | 4 +++- 4 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/sentry/class_map/SentrySdk.php b/src/sentry/class_map/SentrySdk.php index 37d47ff15..299a541a6 100644 --- a/src/sentry/class_map/SentrySdk.php +++ b/src/sentry/class_map/SentrySdk.php @@ -60,6 +60,6 @@ public static function getCurrentHub(): HubInterface */ public static function setCurrentHub(HubInterface $hub): HubInterface { - return tap($hub, fn ($hub) => Context::set(__CLASS__, $hub)); + return Context::set(__CLASS__, $hub); } } diff --git a/src/sentry/src/ConfigProvider.php b/src/sentry/src/ConfigProvider.php index 03f4d332c..981e12ba0 100644 --- a/src/sentry/src/ConfigProvider.php +++ b/src/sentry/src/ConfigProvider.php @@ -54,9 +54,9 @@ public function __invoke(): array \Sentry\Transport\TransportInterface::class => Transport\CoHttpTransport::class, ], 'listeners' => [ - Listener\EventHandleListener::class, - Crons\Listener\EventHandleListener::class, - Tracing\Listener\EventHandleListener::class, + Listener\EventHandleListener::class => PHP_INT_MAX - 1, + Crons\Listener\EventHandleListener::class => PHP_INT_MAX - 1, + Tracing\Listener\EventHandleListener::class => PHP_INT_MAX, ], 'annotations' => [ 'scan' => [ diff --git a/src/sentry/src/Listener/EventHandleListener.php b/src/sentry/src/Listener/EventHandleListener.php index f5c471977..8863a36ed 100644 --- a/src/sentry/src/Listener/EventHandleListener.php +++ b/src/sentry/src/Listener/EventHandleListener.php @@ -16,7 +16,6 @@ use Hyperf\Amqp\Event as AmqpEvent; use Hyperf\AsyncQueue\Event as AsyncQueueEvent; use Hyperf\Command\Event as CommandEvent; -use Hyperf\Context\Context; use Hyperf\Contract\ConfigInterface; use Hyperf\Contract\StdoutLoggerInterface; use Hyperf\Crontab\Event as CrontabEvent; @@ -44,8 +43,6 @@ */ class EventHandleListener implements ListenerInterface { - public const HUB = 'sentry.context.hub'; - protected array $missingKeys = [ // Enable 'sentry.enable.amqp', @@ -207,9 +204,8 @@ protected function captureException(?Throwable $throwable): void } } - protected function setupSentrySdk(): void + protected function setupSentryCurrentHub(): void { - Context::getOrSet(static::HUB, fn () => SentrySdk::init()); } protected function flushEvents(): void @@ -357,7 +353,7 @@ protected function handleRequestReceived(object $event): void return; } - $this->setupSentrySdk(); + $this->setupSentryCurrentHub(); } /** @@ -382,7 +378,7 @@ protected function handleCommandStarting(object $event): void return; } - $this->setupSentrySdk(); + $this->setupSentryCurrentHub(); Integration::configureScope(static function (Scope $scope) use ($event): void { $scope->setTag('command', $event->getCommand()->getName()); @@ -456,7 +452,7 @@ protected function handleAsyncQueueJobProcessing(object $event): void return; } - $this->setupSentrySdk(); + $this->setupSentryCurrentHub(); if ($this->feature->isBreadcrumbEnabled('async_queue')) { $job = [ @@ -508,7 +504,7 @@ protected function handleCrontabTaskStarting(object $event): void return; } - $this->setupSentrySdk(); + $this->setupSentryCurrentHub(); } /** @@ -545,7 +541,7 @@ protected function handleAmqpMessageProcessing(object $event): void return; } - $this->setupSentrySdk(); + $this->setupSentryCurrentHub(); } /** @@ -582,7 +578,7 @@ protected function handleKafkaMessageProcessing(object $event): void return; } - $this->setupSentrySdk(); + $this->setupSentryCurrentHub(); } /** diff --git a/src/sentry/src/Tracing/Tracer.php b/src/sentry/src/Tracing/Tracer.php index d7f46bf65..0b5e196b9 100644 --- a/src/sentry/src/Tracing/Tracer.php +++ b/src/sentry/src/Tracing/Tracer.php @@ -35,8 +35,10 @@ public function __construct(protected Feature $feature) */ public function startTransaction(TransactionContext $transactionContext, array $customSamplingContext = []): Transaction { - $hub = SentrySdk::init(); + // $hub = SentrySdk::init(); + $hub = SentrySdk::getCurrentHub(); $hub->pushScope(); + $hub->configureScope(static fn (Scope $scope) => $scope->clearBreadcrumbs()); $transactionContext->setData(['coroutine.id' => Coroutine::id()] + $transactionContext->getData()); From 3f1c763cbbf3f7549bf2e6aa64a0c70ec7655eeb Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 28 Sep 2025 15:33:36 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=9C=AA=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E7=9A=84=20HttpClient=20=E4=BE=9D=E8=B5=96=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sentry/src/ConfigProvider.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/sentry/src/ConfigProvider.php b/src/sentry/src/ConfigProvider.php index 981e12ba0..f397531dd 100644 --- a/src/sentry/src/ConfigProvider.php +++ b/src/sentry/src/ConfigProvider.php @@ -50,7 +50,6 @@ public function __invoke(): array 'dependencies' => [ \Sentry\ClientBuilder::class => Factory\ClientBuilderFactory::class, \Sentry\State\HubInterface::class => Factory\HubFactory::class, - // \Sentry\HttpClient\HttpClientInterface::class => HttpClient\HttpClientFactory::class, \Sentry\Transport\TransportInterface::class => Transport\CoHttpTransport::class, ], 'listeners' => [ From 9ec316c4eac41349f59bab62a665ae25050f2096 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 28 Sep 2025 15:39:13 +0800 Subject: [PATCH 3/8] fix(tracer): use getCurrentHub instead of init for hub management --- src/sentry/src/Tracing/Tracer.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/sentry/src/Tracing/Tracer.php b/src/sentry/src/Tracing/Tracer.php index 0b5e196b9..b0b68eab3 100644 --- a/src/sentry/src/Tracing/Tracer.php +++ b/src/sentry/src/Tracing/Tracer.php @@ -35,8 +35,7 @@ public function __construct(protected Feature $feature) */ public function startTransaction(TransactionContext $transactionContext, array $customSamplingContext = []): Transaction { - // $hub = SentrySdk::init(); - $hub = SentrySdk::getCurrentHub(); + $hub = SentrySdk::init(); $hub->pushScope(); $hub->configureScope(static fn (Scope $scope) => $scope->clearBreadcrumbs()); From 755d68fa5ea359b5418c366fe9b7820929e500be Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 28 Sep 2025 16:05:00 +0800 Subject: [PATCH 4/8] =?UTF-8?q?refactor(listener):=20=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E6=9C=AA=E4=BD=BF=E7=94=A8=E7=9A=84=20setupSentryCurrentHub=20?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sentry/src/Listener/EventHandleListener.php | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/sentry/src/Listener/EventHandleListener.php b/src/sentry/src/Listener/EventHandleListener.php index 8863a36ed..544aae370 100644 --- a/src/sentry/src/Listener/EventHandleListener.php +++ b/src/sentry/src/Listener/EventHandleListener.php @@ -204,10 +204,6 @@ protected function captureException(?Throwable $throwable): void } } - protected function setupSentryCurrentHub(): void - { - } - protected function flushEvents(): void { try { @@ -352,8 +348,6 @@ protected function handleRequestReceived(object $event): void if (! $this->feature->isEnabled('request')) { return; } - - $this->setupSentryCurrentHub(); } /** @@ -378,8 +372,6 @@ protected function handleCommandStarting(object $event): void return; } - $this->setupSentryCurrentHub(); - Integration::configureScope(static function (Scope $scope) use ($event): void { $scope->setTag('command', $event->getCommand()->getName()); }); @@ -452,8 +444,6 @@ protected function handleAsyncQueueJobProcessing(object $event): void return; } - $this->setupSentryCurrentHub(); - if ($this->feature->isBreadcrumbEnabled('async_queue')) { $job = [ 'job' => $event->getMessage()->job()::class, @@ -503,8 +493,6 @@ protected function handleCrontabTaskStarting(object $event): void if (! $this->feature->isEnabled('crontab')) { return; } - - $this->setupSentryCurrentHub(); } /** @@ -540,8 +528,6 @@ protected function handleAmqpMessageProcessing(object $event): void if (! $this->feature->isEnabled('amqp')) { return; } - - $this->setupSentryCurrentHub(); } /** @@ -577,8 +563,6 @@ protected function handleKafkaMessageProcessing(object $event): void if (! $this->feature->isEnabled('kafka')) { return; } - - $this->setupSentryCurrentHub(); } /** From 2198c5f6175f79e4b7b56066e670fe682326b428 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 28 Sep 2025 16:08:20 +0800 Subject: [PATCH 5/8] =?UTF-8?q?feat(sentry):=20=E6=B7=BB=E5=8A=A0=20sentry?= =?UTF-8?q?:test=20=E5=91=BD=E4=BB=A4=E5=88=B0=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sentry/publish/sentry.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/sentry/publish/sentry.php b/src/sentry/publish/sentry.php index 88afa449e..b00f47fa9 100644 --- a/src/sentry/publish/sentry.php +++ b/src/sentry/publish/sentry.php @@ -87,6 +87,7 @@ 'crontab:run', 'gen:*', 'migrate*', + 'sentry:test', 'tinker', 'vendor:publish', ], From d400e043f5128dc60047c448090ce0ca48c7a420 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 28 Sep 2025 16:15:14 +0800 Subject: [PATCH 6/8] =?UTF-8?q?fix(tracer):=20=E4=BF=AE=E5=A4=8D=E4=BA=8B?= =?UTF-8?q?=E5=8A=A1=E8=8C=83=E5=9B=B4=E7=AE=A1=E7=90=86=EF=BC=8C=E7=A1=AE?= =?UTF-8?q?=E4=BF=9D=E5=9C=A8=E4=BA=8B=E5=8A=A1=E7=BB=93=E6=9D=9F=E6=97=B6?= =?UTF-8?q?=E6=AD=A3=E7=A1=AE=E5=BC=B9=E5=87=BA=E8=8C=83=E5=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sentry/src/Tracing/Tracer.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/sentry/src/Tracing/Tracer.php b/src/sentry/src/Tracing/Tracer.php index b0b68eab3..ab46505f9 100644 --- a/src/sentry/src/Tracing/Tracer.php +++ b/src/sentry/src/Tracing/Tracer.php @@ -39,6 +39,8 @@ public function startTransaction(TransactionContext $transactionContext, array $ $hub->pushScope(); $hub->configureScope(static fn (Scope $scope) => $scope->clearBreadcrumbs()); + defer(static fn () => $hub->popScope()); + $transactionContext->setData(['coroutine.id' => Coroutine::id()] + $transactionContext->getData()); if ($transactionContext->getStartTimestamp() === null) { From dd64e9364dd3bdbe3093d6ca57cd95a3dc995b4f Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 28 Sep 2025 16:18:55 +0800 Subject: [PATCH 7/8] =?UTF-8?q?fix(ConfigProvider):=20=E7=A1=AE=E4=BF=9D?= =?UTF-8?q?=20Tracing\EventHandleListener=20=E6=98=AF=E7=AC=AC=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E5=A4=84=E7=90=86=E4=BA=8B=E4=BB=B6=E7=9A=84=E7=9B=91?= =?UTF-8?q?=E5=90=AC=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sentry/src/ConfigProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sentry/src/ConfigProvider.php b/src/sentry/src/ConfigProvider.php index f397531dd..e7e149680 100644 --- a/src/sentry/src/ConfigProvider.php +++ b/src/sentry/src/ConfigProvider.php @@ -55,7 +55,7 @@ public function __invoke(): array 'listeners' => [ Listener\EventHandleListener::class => PHP_INT_MAX - 1, Crons\Listener\EventHandleListener::class => PHP_INT_MAX - 1, - Tracing\Listener\EventHandleListener::class => PHP_INT_MAX, + Tracing\Listener\EventHandleListener::class => PHP_INT_MAX, // !! Make sure it is the first one to handle the event ], 'annotations' => [ 'scan' => [ From a4c272fa0d947f8fd118af3d99a32b315530223a Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 28 Sep 2025 16:29:20 +0800 Subject: [PATCH 8/8] =?UTF-8?q?fix(tracer):=20=E4=BD=BF=E7=94=A8=20getCurr?= =?UTF-8?q?entHub=20=E6=9B=BF=E4=BB=A3=20init=20=E8=BF=9B=E8=A1=8C=20Hub?= =?UTF-8?q?=20=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sentry/src/Tracing/Tracer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sentry/src/Tracing/Tracer.php b/src/sentry/src/Tracing/Tracer.php index ab46505f9..a4a79e892 100644 --- a/src/sentry/src/Tracing/Tracer.php +++ b/src/sentry/src/Tracing/Tracer.php @@ -35,7 +35,7 @@ public function __construct(protected Feature $feature) */ public function startTransaction(TransactionContext $transactionContext, array $customSamplingContext = []): Transaction { - $hub = SentrySdk::init(); + $hub = SentrySdk::getCurrentHub(); $hub->pushScope(); $hub->configureScope(static fn (Scope $scope) => $scope->clearBreadcrumbs());