Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/sentry/src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
17 changes: 10 additions & 7 deletions src/sentry/src/Integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace FriendsOfHyperf\Sentry;

use Hyperf\Context\Context;
use Sentry\Breadcrumb;
use Sentry\Event;
use Sentry\Integration\IntegrationInterface;
Expand All @@ -28,7 +29,7 @@

class Integration implements IntegrationInterface
{
private static ?string $transaction = null;
public const TRANSACTION = 'sentry.integration.transaction';
Copy link

Copilot AI Dec 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The constant naming doesn't follow the established pattern in the codebase. Looking at the SentryContext class, all context key constants use a prefix like CTX_ (e.g., CTX_CRON_CHECKIN_ID, CTX_CARRIER). For consistency, this constant should be named CTX_TRANSACTION instead of TRANSACTION, and the value should follow the pattern 'sentry.ctx.transaction' instead of 'sentry.integration.transaction'.

Copilot uses AI. Check for mistakes.

public function setupOnce(): void
{
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Comment on lines 94 to 102
Copy link

Copilot AI Dec 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The refactored transaction storage methods lack test coverage. Since the Sentry component already has test files (CarrierTest, FeatureTest, SqlParseTest), consider adding tests to verify that getTransaction() and setTransaction() correctly store and retrieve transaction data from the Context API, and that the transaction data is properly isolated between different coroutines.

Copilot uses AI. Check for mistakes.

/**
Expand Down
3 changes: 1 addition & 2 deletions src/sentry/src/Metrics/Listener/OnBeforeHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions src/sentry/src/Metrics/Listener/OnMetricFactoryReady.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
12 changes: 12 additions & 0 deletions src/sentry/src/SentryContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}