From 5760daa5efdfa04617d5790086afdce3d1053260 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Thu, 18 Sep 2025 15:40:26 +0800 Subject: [PATCH] feat(sentry): improve event flushing and expand listener coverage This commit enhances the Sentry integration by implementing comprehensive event flushing across all listeners and improving event handling coverage: - Add log flushing to Integration::flushEvents() to ensure all logs are sent - Implement automatic event flushing in CoroutineAspect finally block - Expand event listener coverage to handle completion events (AfterConsume, AfterHandle, AfterExecute, RetryHandle) - Add flushEvents() calls after processing completion and failure events - Ensure proper cleanup and event delivery in all async contexts These changes improve reliability of error reporting and ensure events are not lost when processes terminate or coroutines complete. --- src/sentry/src/Aspect/CoroutineAspect.php | 3 +++ src/sentry/src/Integration.php | 3 +++ src/sentry/src/Listener/AmqpExceptionListener.php | 9 ++++++++- .../src/Listener/AsyncQueueExceptionListener.php | 12 +++++++++++- src/sentry/src/Listener/CaptureExceptionListener.php | 6 ++++++ src/sentry/src/Listener/CommandExceptionListener.php | 4 +++- src/sentry/src/Listener/CrontabExceptionListener.php | 9 ++++++++- src/sentry/src/Listener/KafkaExceptionListener.php | 9 ++++++++- src/sentry/src/Listener/RequestExceptionListener.php | 8 +++++++- 9 files changed, 57 insertions(+), 6 deletions(-) diff --git a/src/sentry/src/Aspect/CoroutineAspect.php b/src/sentry/src/Aspect/CoroutineAspect.php index 6160c163f..2eb5f76e4 100644 --- a/src/sentry/src/Aspect/CoroutineAspect.php +++ b/src/sentry/src/Aspect/CoroutineAspect.php @@ -11,6 +11,7 @@ namespace FriendsOfHyperf\Sentry\Aspect; +use FriendsOfHyperf\Sentry\Integration; use Hyperf\Di\Aop\AbstractAspect; use Hyperf\Di\Aop\ProceedingJoinPoint; use Hyperf\Engine\Coroutine as Co; @@ -49,6 +50,8 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) } catch (Throwable $throwable) { SentrySdk::getCurrentHub()->captureException($throwable); throw $throwable; + } finally { + Integration::flushEvents(); } }; diff --git a/src/sentry/src/Integration.php b/src/sentry/src/Integration.php index f3cb34927..7d934ba68 100644 --- a/src/sentry/src/Integration.php +++ b/src/sentry/src/Integration.php @@ -14,6 +14,7 @@ use Sentry\Breadcrumb; use Sentry\Event; use Sentry\Integration\IntegrationInterface; +use Sentry\Logs\Logs; use Sentry\SentrySdk; use Sentry\State\Scope; use Sentry\Tracing\Span; @@ -108,6 +109,8 @@ public static function flushEvents(): void if ($client !== null) { $client->flush(); + + Logs::getInstance()->flush(); } } diff --git a/src/sentry/src/Listener/AmqpExceptionListener.php b/src/sentry/src/Listener/AmqpExceptionListener.php index 154f39123..592e93703 100644 --- a/src/sentry/src/Listener/AmqpExceptionListener.php +++ b/src/sentry/src/Listener/AmqpExceptionListener.php @@ -19,12 +19,13 @@ public function listen(): array { return [ Event\BeforeConsume::class, + Event\AfterConsume::class, Event\FailToConsume::class, ]; } /** - * @param Event\FailToConsume $event + * @param Event\FailToConsume|Event\AfterConsume|Event\BeforeConsume|object $event */ public function process(object $event): void { @@ -36,5 +37,11 @@ public function process(object $event): void Event\FailToConsume::class => $this->captureException($event->getThrowable()), default => $this->setupSentrySdk(), }; + + match ($event::class) { + Event\AfterConsume::class, + Event\FailToConsume::class => $this->flushEvents(), + default => null, + }; } } diff --git a/src/sentry/src/Listener/AsyncQueueExceptionListener.php b/src/sentry/src/Listener/AsyncQueueExceptionListener.php index b4373f8c8..ae0b1551b 100644 --- a/src/sentry/src/Listener/AsyncQueueExceptionListener.php +++ b/src/sentry/src/Listener/AsyncQueueExceptionListener.php @@ -19,12 +19,14 @@ public function listen(): array { return [ Event\BeforeHandle::class, + Event\AfterHandle::class, + Event\RetryHandle::class, Event\FailedHandle::class, ]; } /** - * @param Event\FailedHandle $event + * @param Event\FailedHandle|Event\AfterHandle|Event\BeforeHandle|object $event */ public function process(object $event): void { @@ -33,8 +35,16 @@ public function process(object $event): void } match ($event::class) { + Event\RetryHandle::class, Event\FailedHandle::class => $this->captureException($event->getThrowable()), default => $this->setupSentrySdk(), }; + + match ($event::class) { + Event\AfterHandle::class, + Event\RetryHandle::class, + Event\FailedHandle::class => $this->flushEvents(), + default => null, + }; } } diff --git a/src/sentry/src/Listener/CaptureExceptionListener.php b/src/sentry/src/Listener/CaptureExceptionListener.php index acac0670f..1f56dd7e3 100644 --- a/src/sentry/src/Listener/CaptureExceptionListener.php +++ b/src/sentry/src/Listener/CaptureExceptionListener.php @@ -11,6 +11,7 @@ namespace FriendsOfHyperf\Sentry\Listener; +use FriendsOfHyperf\Sentry\Integration; use FriendsOfHyperf\Sentry\Switcher; use Hyperf\Context\Context; use Hyperf\Contract\StdoutLoggerInterface; @@ -60,4 +61,9 @@ protected function setupSentrySdk(): void SentrySdk::init(); Context::set(static::SETUP, true); } + + protected function flushEvents(): void + { + Integration::flushEvents(); + } } diff --git a/src/sentry/src/Listener/CommandExceptionListener.php b/src/sentry/src/Listener/CommandExceptionListener.php index 1d00c40ed..76f186416 100644 --- a/src/sentry/src/Listener/CommandExceptionListener.php +++ b/src/sentry/src/Listener/CommandExceptionListener.php @@ -20,11 +20,12 @@ public function listen(): array return [ Event\BeforeHandle::class, Event\FailToHandle::class, + Event\AfterExecute::class, ]; } /** - * @param Event\FailToHandle $event + * @param Event\FailToHandle|Event\BeforeHandle|Event\AfterExecute|object $event */ public function process(object $event): void { @@ -34,6 +35,7 @@ public function process(object $event): void match ($event::class) { Event\FailToHandle::class => $this->captureException($event->getThrowable()), + Event\AfterExecute::class => $this->flushEvents(), default => $this->setupSentrySdk(), }; } diff --git a/src/sentry/src/Listener/CrontabExceptionListener.php b/src/sentry/src/Listener/CrontabExceptionListener.php index 8d4ce3ec5..0b09fcbe9 100644 --- a/src/sentry/src/Listener/CrontabExceptionListener.php +++ b/src/sentry/src/Listener/CrontabExceptionListener.php @@ -24,12 +24,13 @@ public function listen(): array { return [ Event\BeforeExecute::class, + Event\AfterExecute::class, Event\FailToExecute::class, ]; } /** - * @param Event\FailToExecute $event + * @param Event\FailToExecute|Event\AfterExecute|Event\BeforeExecute|object $event */ public function process(object $event): void { @@ -41,5 +42,11 @@ public function process(object $event): void Event\FailToExecute::class => $this->captureException($event->throwable), default => $this->setupSentrySdk(), }; + + match ($event::class) { + Event\AfterExecute::class, + Event\FailToExecute::class => $this->flushEvents(), + default => null, + }; } } diff --git a/src/sentry/src/Listener/KafkaExceptionListener.php b/src/sentry/src/Listener/KafkaExceptionListener.php index ab3c7c8b4..6a4f4a2b9 100644 --- a/src/sentry/src/Listener/KafkaExceptionListener.php +++ b/src/sentry/src/Listener/KafkaExceptionListener.php @@ -20,11 +20,12 @@ public function listen(): array return [ Event\BeforeConsume::class, Event\FailToConsume::class, + Event\AfterConsume::class, ]; } /** - * @param Event\FailToConsume $event + * @param Event\FailToConsume|Event\AfterConsume|Event\BeforeConsume|object $event */ public function process(object $event): void { @@ -36,5 +37,11 @@ public function process(object $event): void Event\FailToConsume::class => $this->captureException($event->getThrowable()), default => $this->setupSentrySdk(), }; + + match ($event::class) { + Event\AfterConsume::class, + Event\FailToConsume::class => $this->flushEvents(), + default => null, + }; } } diff --git a/src/sentry/src/Listener/RequestExceptionListener.php b/src/sentry/src/Listener/RequestExceptionListener.php index 527ab6cac..58f82d4fa 100644 --- a/src/sentry/src/Listener/RequestExceptionListener.php +++ b/src/sentry/src/Listener/RequestExceptionListener.php @@ -29,7 +29,7 @@ public function listen(): array } /** - * @param RequestTerminated|object $event + * @param RequestTerminated|RpcRequestTerminated|object $event */ public function process(object $event): void { @@ -41,5 +41,11 @@ public function process(object $event): void RequestTerminated::class, RpcRequestTerminated::class => $this->captureException($event->exception), default => $this->setupSentrySdk(), }; + + match ($event::class) { + RequestTerminated::class, + RpcRequestTerminated::class => $this->flushEvents(), + default => null, + }; } }