From 24fff17bcc4e1738a04345ea4afcb18d59a32e24 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Tue, 30 Dec 2025 08:26:43 +0800 Subject: [PATCH 1/2] refactor(sentry): use Context API for transaction storage Replace static property with Hyperf's Context API for storing transaction data, ensuring proper coroutine isolation in Swoole/Swow environments. --- src/sentry/src/Integration.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/sentry/src/Integration.php b/src/sentry/src/Integration.php index 776ccd165..24923c026 100644 --- a/src/sentry/src/Integration.php +++ b/src/sentry/src/Integration.php @@ -11,6 +11,7 @@ namespace FriendsOfHyperf\Sentry; +use Hyperf\Context\Context; use Sentry\Breadcrumb; use Sentry\Event; use Sentry\Integration\IntegrationInterface; @@ -28,7 +29,7 @@ class Integration implements IntegrationInterface { - private static ?string $transaction = null; + public const TRANSACTION = 'sentry.integration.transaction'; public function setupOnce(): void { @@ -47,13 +48,15 @@ public function setupOnce(): void if (defined('\Swow\Extension::VERSION')) { $event->setContext('swow', [ - /* @phpstan-ignore-next-line */ - 'version' => \Swow\Extension::VERSION, + 'version' => \Swow\Extension::VERSION, // @phpstan-ignore-line ]); } - if (empty($event->getTransaction())) { - $event->setTransaction($self->getTransaction()); + if ( + empty($event->getTransaction()) + && $transaction = $self->getTransaction() + ) { + $event->setTransaction($transaction); } return $event; @@ -90,12 +93,12 @@ public static function configureScope(callable $callback): void public static function getTransaction(): ?string { - return self::$transaction; + return Context::get(self::TRANSACTION); } public static function setTransaction(?string $transaction): void { - self::$transaction = $transaction; + Context::set(self::TRANSACTION, $transaction); } /** From e0c524db4bc30df41d9e5ac35f5d3f4034d589d3 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Tue, 30 Dec 2025 16:18:43 +0800 Subject: [PATCH 2/2] feat: Migrate `runningInCommand` state from `Constants` to `SentryContext` with new setter and getter methods, deprecating the old static property. --- src/sentry/src/Constants.php | 3 +++ src/sentry/src/Metrics/Listener/OnBeforeHandle.php | 3 +-- .../src/Metrics/Listener/OnMetricFactoryReady.php | 4 ++-- src/sentry/src/SentryContext.php | 12 ++++++++++++ 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/sentry/src/Constants.php b/src/sentry/src/Constants.php index 28fb71007..4b9b75a81 100644 --- a/src/sentry/src/Constants.php +++ b/src/sentry/src/Constants.php @@ -66,5 +66,8 @@ class Constants public const TRACEPARENT = 'traceparent'; + /** + * @deprecated since v3.1, will be removed in v3.2. + */ public static bool $runningInCommand = false; } diff --git a/src/sentry/src/Metrics/Listener/OnBeforeHandle.php b/src/sentry/src/Metrics/Listener/OnBeforeHandle.php index 82220efd6..080d5cb18 100644 --- a/src/sentry/src/Metrics/Listener/OnBeforeHandle.php +++ b/src/sentry/src/Metrics/Listener/OnBeforeHandle.php @@ -11,7 +11,6 @@ namespace FriendsOfHyperf\Sentry\Metrics\Listener; -use FriendsOfHyperf\Sentry\Constants; use FriendsOfHyperf\Sentry\Feature; use FriendsOfHyperf\Sentry\Integration; use FriendsOfHyperf\Sentry\Metrics\Event\MetricFactoryReady; @@ -59,7 +58,7 @@ public function process(object $event): void return; } - Constants::$runningInCommand = true; + SentryContext::setRunningInCommand(); if ($this->feature->isCommandMetricsEnabled() && $this->container->has(EventDispatcherInterface::class)) { $this->container->get(EventDispatcherInterface::class)->dispatch(new MetricFactoryReady()); diff --git a/src/sentry/src/Metrics/Listener/OnMetricFactoryReady.php b/src/sentry/src/Metrics/Listener/OnMetricFactoryReady.php index c4cf57437..3d5ea0965 100644 --- a/src/sentry/src/Metrics/Listener/OnMetricFactoryReady.php +++ b/src/sentry/src/Metrics/Listener/OnMetricFactoryReady.php @@ -11,12 +11,12 @@ namespace FriendsOfHyperf\Sentry\Metrics\Listener; -use FriendsOfHyperf\Sentry\Constants as SentryConstants; use FriendsOfHyperf\Sentry\Feature; use FriendsOfHyperf\Sentry\Integration; use FriendsOfHyperf\Sentry\Metrics\CoroutineServerStats; use FriendsOfHyperf\Sentry\Metrics\Event\MetricFactoryReady; use FriendsOfHyperf\Sentry\Metrics\Traits\MetricSetter; +use FriendsOfHyperf\Sentry\SentryContext; use Hyperf\Coordinator\Timer; use Hyperf\Engine\Coroutine as Co; use Hyperf\Event\Contract\ListenerInterface; @@ -84,7 +84,7 @@ public function process(object $event): void $serverStatsFactory = null; - if (! SentryConstants::$runningInCommand) { + if (! SentryContext::isRunningInCommand()) { if ($this->container->has(SwooleServer::class) && $server = $this->container->get(SwooleServer::class)) { if ($server instanceof SwooleServer) { $serverStatsFactory = fn (): array => $server->stats(); diff --git a/src/sentry/src/SentryContext.php b/src/sentry/src/SentryContext.php index 0be693ade..f88cecd87 100644 --- a/src/sentry/src/SentryContext.php +++ b/src/sentry/src/SentryContext.php @@ -39,6 +39,8 @@ class SentryContext public const CTX_RPC_SPAN_CONTEXT = 'sentry.ctx.rpc.span.context'; + public const CTX_RUNNING_IN_COMMAND = 'sentry.ctx.running_in_command'; + public static function disableTracing(): void { Context::set(self::CTX_DISABLE_COROUTINE_TRACING, true); @@ -158,4 +160,14 @@ public static function destroyRpcSpanContext(): void { Context::destroy(self::CTX_RPC_SPAN_CONTEXT); } + + public static function setRunningInCommand(): void + { + Context::set(self::CTX_RUNNING_IN_COMMAND, true); + } + + public static function isRunningInCommand(): bool + { + return (bool) Context::get(self::CTX_RUNNING_IN_COMMAND); + } }