From e0d0a5e647c1601ff3b1f5fb946672a0842014cd Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Thu, 18 Jun 2026 12:43:36 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(sentry):=20flush=20metrics?= =?UTF-8?q?=20in=20watchers=20and=20release=20captured=20refs=20in=20defer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Flush metrics in watcher tick callbacks to ensure timely emission - Unset transaction/span/timer references inside defer closures to avoid extending their lifetime under coroutines - Add log_flush_threshold config option - Inline SentrySdk::getCurrentHub() calls in crons listener --- src/sentry/publish/sentry.php | 3 +++ src/sentry/src/Crons/Listener/EventHandleListener.php | 9 +++------ src/sentry/src/Metrics/Listener/OnBeforeHandle.php | 2 ++ .../src/Metrics/Listener/OnCoroutineServerStart.php | 2 ++ .../src/Metrics/Listener/OnMetricFactoryReady.php | 2 ++ src/sentry/src/Metrics/Listener/OnWorkerStart.php | 2 ++ src/sentry/src/Metrics/Listener/PoolWatcher.php | 2 ++ src/sentry/src/Metrics/Listener/QueueWatcher.php | 2 ++ src/sentry/src/Metrics/Listener/RequestWatcher.php | 2 ++ src/sentry/src/Tracing/Aspect/CoroutineAspect.php | 4 +--- .../src/Tracing/Listener/EventHandleListener.php | 10 ++++++++++ 11 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/sentry/publish/sentry.php b/src/sentry/publish/sentry.php index 07c29a541..0c550917d 100644 --- a/src/sentry/publish/sentry.php +++ b/src/sentry/publish/sentry.php @@ -43,6 +43,9 @@ // @see: https://docs.sentry.io/platforms/php/guides/laravel/configuration/options/#enable_logs 'enable_logs' => env('SENTRY_ENABLE_LOGS', true), + // @see: https://docs.sentry.io/platforms/php/guides/laravel/configuration/options/#log_flush_threshold + 'log_flush_threshold' => env('SENTRY_LOG_FLUSH_THRESHOLD') === null ? null : (int) env('SENTRY_LOG_FLUSH_THRESHOLD'), + // @see: https://docs.sentry.io/platforms/php/configuration/options/#before_send_log // 'before_send_log' => function (Sentry\Logs\Log $log): Sentry\Logs\Log { // return $log; diff --git a/src/sentry/src/Crons/Listener/EventHandleListener.php b/src/sentry/src/Crons/Listener/EventHandleListener.php index c083e8d96..2d21bf7df 100644 --- a/src/sentry/src/Crons/Listener/EventHandleListener.php +++ b/src/sentry/src/Crons/Listener/EventHandleListener.php @@ -66,7 +66,6 @@ public function process(object $event): void protected function handleCrontabTaskStarting(Event\BeforeExecute $event, array $options): void { - $hub = SentrySdk::getCurrentHub(); $slug = $event->crontab->getName(); $rule = $event->crontab->getRule(); $rules = explode(' ', $rule); @@ -83,7 +82,7 @@ protected function handleCrontabTaskStarting(Event\BeforeExecute $event, array $ $monitorConfig = $this->createMonitorConfig($event, $options, $rule); } - $checkInId = $hub->captureCheckIn( + $checkInId = SentrySdk::getCurrentHub()->captureCheckIn( slug: $slug, status: CheckInStatus::inProgress(), monitorConfig: $monitorConfig, @@ -100,10 +99,9 @@ protected function handleCrontabTaskFinished(Event\AfterExecute $event): void return; } - $hub = SentrySdk::getCurrentHub(); $slug = $event->crontab->getName(); - $hub->captureCheckIn( + SentrySdk::getCurrentHub()->captureCheckIn( slug: $slug, status: CheckInStatus::ok(), checkInId: $checkInId, @@ -118,10 +116,9 @@ protected function handleCrontabTaskFailed(Event\FailToExecute $event): void return; } - $hub = SentrySdk::getCurrentHub(); $slug = $event->crontab->getName(); - $hub->captureCheckIn( + SentrySdk::getCurrentHub()->captureCheckIn( slug: $slug, status: CheckInStatus::error(), checkInId: $checkInId, diff --git a/src/sentry/src/Metrics/Listener/OnBeforeHandle.php b/src/sentry/src/Metrics/Listener/OnBeforeHandle.php index bd28da0cc..f75d48f57 100644 --- a/src/sentry/src/Metrics/Listener/OnBeforeHandle.php +++ b/src/sentry/src/Metrics/Listener/OnBeforeHandle.php @@ -113,6 +113,8 @@ function () use ($metrics) { ['worker' => '0'], Unit::megabyte() ); + + metrics()->flush(); } ); } diff --git a/src/sentry/src/Metrics/Listener/OnCoroutineServerStart.php b/src/sentry/src/Metrics/Listener/OnCoroutineServerStart.php index 9985eb36b..aff237387 100644 --- a/src/sentry/src/Metrics/Listener/OnCoroutineServerStart.php +++ b/src/sentry/src/Metrics/Listener/OnCoroutineServerStart.php @@ -111,6 +111,8 @@ function () use ($metrics) { ['worker' => '0'], Unit::megabyte() ); + + metrics()->flush(); } ); } diff --git a/src/sentry/src/Metrics/Listener/OnMetricFactoryReady.php b/src/sentry/src/Metrics/Listener/OnMetricFactoryReady.php index b2066e07d..e34c294a2 100644 --- a/src/sentry/src/Metrics/Listener/OnMetricFactoryReady.php +++ b/src/sentry/src/Metrics/Listener/OnMetricFactoryReady.php @@ -128,6 +128,8 @@ function () use ($metrics, $serverStatsFactory, $workerId) { ['worker' => (string) $workerId], Unit::megabyte() ); + + metrics()->flush(); } ); } diff --git a/src/sentry/src/Metrics/Listener/OnWorkerStart.php b/src/sentry/src/Metrics/Listener/OnWorkerStart.php index 613585a0d..a2b9d5b60 100644 --- a/src/sentry/src/Metrics/Listener/OnWorkerStart.php +++ b/src/sentry/src/Metrics/Listener/OnWorkerStart.php @@ -118,6 +118,8 @@ function () use ($metrics, $event) { ['worker' => (string) ($event->workerId ?? 0)], Unit::megabyte() ); + + metrics()->flush(); } ); } diff --git a/src/sentry/src/Metrics/Listener/PoolWatcher.php b/src/sentry/src/Metrics/Listener/PoolWatcher.php index 52ccfb96f..8371d5991 100644 --- a/src/sentry/src/Metrics/Listener/PoolWatcher.php +++ b/src/sentry/src/Metrics/Listener/PoolWatcher.php @@ -88,6 +88,8 @@ function () use ($pool, $workerId, $poolName) { 'worker' => (string) $workerId, ] ); + + metrics()->flush(); } ); } diff --git a/src/sentry/src/Metrics/Listener/QueueWatcher.php b/src/sentry/src/Metrics/Listener/QueueWatcher.php index 5f7a0d682..d58d64762 100644 --- a/src/sentry/src/Metrics/Listener/QueueWatcher.php +++ b/src/sentry/src/Metrics/Listener/QueueWatcher.php @@ -82,6 +82,8 @@ function () { ['queue' => $name] ); } + + metrics()->flush(); } ); } diff --git a/src/sentry/src/Metrics/Listener/RequestWatcher.php b/src/sentry/src/Metrics/Listener/RequestWatcher.php index a828d1e1d..e779034a6 100644 --- a/src/sentry/src/Metrics/Listener/RequestWatcher.php +++ b/src/sentry/src/Metrics/Listener/RequestWatcher.php @@ -63,6 +63,8 @@ public function process(object $event): void --$this->stats->connection_num; $timer->end(true); + + unset($timer); }); } } diff --git a/src/sentry/src/Tracing/Aspect/CoroutineAspect.php b/src/sentry/src/Tracing/Aspect/CoroutineAspect.php index 07665d281..3a9c638b0 100644 --- a/src/sentry/src/Tracing/Aspect/CoroutineAspect.php +++ b/src/sentry/src/Tracing/Aspect/CoroutineAspect.php @@ -27,9 +27,6 @@ use function Hyperf\Coroutine\defer; use function Sentry\continueTrace; -/** - * Run after FriendsOfHyperf\Sentry\Aspect\CoroutineAspect. - */ class CoroutineAspect extends AbstractAspect { public const CONTEXT_KEYS = [ @@ -88,6 +85,7 @@ function (Scope $scope) use ($proceedingJoinPoint, $callingOnFunction) { defer(function () use ($transaction) { $transaction->finish(); SentrySdk::endContext(); + unset($transaction); }); return trace( diff --git a/src/sentry/src/Tracing/Listener/EventHandleListener.php b/src/sentry/src/Tracing/Listener/EventHandleListener.php index 4dcb28fae..86f93f82e 100644 --- a/src/sentry/src/Tracing/Listener/EventHandleListener.php +++ b/src/sentry/src/Tracing/Listener/EventHandleListener.php @@ -336,6 +336,8 @@ protected function handleRequestReceived(HttpEvent\RequestReceived|RpcEvent\Requ // Finish transaction $transaction->finish(); + + unset($transaction, $span); }); } @@ -532,6 +534,8 @@ protected function handleCrontabTaskStarting(CrontabEvent\BeforeExecute $event): // Finish transaction $transaction->finish(); + + unset($transaction); }); } @@ -598,6 +602,8 @@ protected function handleAmqpMessageProcessing(AmqpEvent\BeforeConsume $event): // Finish transaction $transaction->finish(); + + unset($transaction); }); } @@ -663,6 +669,8 @@ protected function handleKafkaMessageProcessing(KafkaEvent\BeforeConsume $event) // Finish transaction $transaction->finish(); + + unset($transaction); }); } @@ -710,6 +718,8 @@ protected function handleAsyncQueueJobProcessing(AsyncQueueEvent\BeforeHandle $e // Finish transaction $transaction->finish(); + + unset($transaction); }); }