From f526d1c34775d07d9fe1e405a5e2dc85425e5eab Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Mon, 29 Sep 2025 13:33:34 +0800 Subject: [PATCH] refactor(sentry): optimize event flushing strategy by moving to tracing components - Remove flushEvents() method and calls from main EventHandleListener - Move event flushing responsibility to tracing components where transactions finish - Clean up commented code in CoroutineAspect files - Ensure events are flushed at appropriate transaction boundaries for better performance This change improves the efficiency of Sentry event handling by flushing events only when tracing transactions complete, rather than after every event. --- src/sentry/src/Aspect/CoroutineAspect.php | 1 - .../src/Listener/EventHandleListener.php | 23 ------------------- .../src/Tracing/Aspect/CoroutineAspect.php | 10 ++++++-- .../Tracing/Listener/EventHandleListener.php | 19 +++++++++++++++ 4 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/sentry/src/Aspect/CoroutineAspect.php b/src/sentry/src/Aspect/CoroutineAspect.php index ff1a0da76..d0cdc007f 100644 --- a/src/sentry/src/Aspect/CoroutineAspect.php +++ b/src/sentry/src/Aspect/CoroutineAspect.php @@ -25,7 +25,6 @@ class CoroutineAspect extends AbstractAspect ]; protected array $keys = [ - // SentrySdk::class, \Psr\Http\Message\ServerRequestInterface::class, ]; diff --git a/src/sentry/src/Listener/EventHandleListener.php b/src/sentry/src/Listener/EventHandleListener.php index 544aae370..569346038 100644 --- a/src/sentry/src/Listener/EventHandleListener.php +++ b/src/sentry/src/Listener/EventHandleListener.php @@ -204,15 +204,6 @@ protected function captureException(?Throwable $throwable): void } } - protected function flushEvents(): void - { - try { - Integration::flushEvents(); - } catch (Throwable $e) { - $this->logger->error((string) $e); - } - } - /** * @param BootApplication $event */ @@ -360,7 +351,6 @@ protected function handleRequestTerminated(object $event): void } $this->captureException($event->exception); - $this->flushEvents(); } /** @@ -428,7 +418,6 @@ protected function handleCommandFinished(object $event): void } $this->captureException($event->getThrowable()); - $this->flushEvents(); Integration::configureScope(static function (Scope $scope): void { $scope->removeTag('command'); @@ -468,8 +457,6 @@ protected function handleAsyncQueueJobProcessed(object $event): void if (! $this->feature->isEnabled('async_queue')) { return; } - - $this->flushEvents(); } /** @@ -482,7 +469,6 @@ protected function handleAsyncQueueJobRetryOrFailed(object $event): void } $this->captureException($event->getThrowable()); - $this->flushEvents(); } /** @@ -503,8 +489,6 @@ protected function handleCrontabTaskFinished(object $event): void if (! $this->feature->isEnabled('crontab')) { return; } - - $this->flushEvents(); } /** @@ -517,7 +501,6 @@ protected function handleCrontabTaskFailed(object $event): void } $this->captureException($event->throwable); - $this->flushEvents(); } /** @@ -538,8 +521,6 @@ protected function handleAmqpMessageProcessed(object $event): void if (! $this->feature->isEnabled('amqp')) { return; } - - $this->flushEvents(); } /** @@ -552,7 +533,6 @@ protected function handleAmqpMessageFailed(object $event): void } $this->captureException($event->getThrowable()); - $this->flushEvents(); } /** @@ -573,8 +553,6 @@ protected function handleKafkaMessageProcessed(object $event): void if (! $this->feature->isEnabled('kafka')) { return; } - - $this->flushEvents(); } /** @@ -587,7 +565,6 @@ protected function handleKafkaMessageFailed(object $event): void } $this->captureException($event->getThrowable()); - $this->flushEvents(); } /** diff --git a/src/sentry/src/Tracing/Aspect/CoroutineAspect.php b/src/sentry/src/Tracing/Aspect/CoroutineAspect.php index fdb1740fe..5abee47db 100644 --- a/src/sentry/src/Tracing/Aspect/CoroutineAspect.php +++ b/src/sentry/src/Tracing/Aspect/CoroutineAspect.php @@ -12,6 +12,7 @@ namespace FriendsOfHyperf\Sentry\Tracing\Aspect; use FriendsOfHyperf\Sentry\Feature; +use FriendsOfHyperf\Sentry\Integration; use FriendsOfHyperf\Sentry\Util\CoroutineBacktraceHelper; use Hyperf\Di\Aop\AbstractAspect; use Hyperf\Di\Aop\ProceedingJoinPoint; @@ -32,7 +33,6 @@ class CoroutineAspect extends AbstractAspect ]; protected array $keys = [ - // SentrySdk::class, \Psr\Http\Message\ServerRequestInterface::class, ]; @@ -57,7 +57,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) return $proceedingJoinPoint->process(); } - // If there's no active transaction, skip tracing. + // Get the current transaction from the current scope. $transaction = SentrySdk::getCurrentHub()->getTransaction(); // If there's no active transaction, skip tracing. @@ -99,8 +99,14 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) ); defer(function () use ($coTransaction) { + // Set the transaction on the current scope to ensure it's the active one. SentrySdk::getCurrentHub()->setSpan($coTransaction); + + // Finish the transaction when the coroutine ends. $coTransaction->finish(); + + // Flush events + Integration::flushEvents(); }); try { diff --git a/src/sentry/src/Tracing/Listener/EventHandleListener.php b/src/sentry/src/Tracing/Listener/EventHandleListener.php index 3206c97ea..4868be093 100644 --- a/src/sentry/src/Tracing/Listener/EventHandleListener.php +++ b/src/sentry/src/Tracing/Listener/EventHandleListener.php @@ -14,6 +14,7 @@ use Closure; use FriendsOfHyperf\Sentry\Constants; use FriendsOfHyperf\Sentry\Feature; +use FriendsOfHyperf\Sentry\Integration; use FriendsOfHyperf\Sentry\Util\Carrier; use FriendsOfHyperf\Sentry\Util\SqlParser; use FriendsOfHyperf\Support\RedisCommand; @@ -335,6 +336,9 @@ protected function handleRequestReceived(HttpEvent\RequestReceived|RpcEvent\Requ // Finish transaction $transaction->finish(); + + // Flush events + Integration::flushEvents(); }); } @@ -396,6 +400,9 @@ protected function handleCommandStarting(CommandEvent\BeforeHandle $event): void // Finish transaction $transaction->finish(); + + // Flush events + Integration::flushEvents(); }); } @@ -516,6 +523,9 @@ protected function handleCrontabTaskStarting(CrontabEvent\BeforeExecute $event): // Finish transaction $transaction->finish(); + + // Flush events + Integration::flushEvents(); }); } @@ -586,6 +596,9 @@ protected function handleAmqpMessageProcessing(AmqpEvent\BeforeConsume $event): // Finish transaction $transaction->finish(); + + // Flush events + Integration::flushEvents(); }); } @@ -656,6 +669,9 @@ protected function handleKafkaMessageProcessing(KafkaEvent\BeforeConsume $event) // Finish transaction $transaction->finish(); + + // Flush events + Integration::flushEvents(); }); } @@ -709,6 +725,9 @@ protected function handleAsyncQueueJobProcessing(AsyncQueueEvent\BeforeHandle $e // Finish transaction $transaction->finish(); + + // Flush events + Integration::flushEvents(); }); }