Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
59e6e4b
refactor: centralize context management in Sentry integration
huangdijia Dec 3, 2025
f3515f4
refactor: 更新获取Carrier的方法以支持协程ID
huangdijia Dec 3, 2025
0266bcc
refactor: 使用call_user_func获取消息ID以提高兼容性
huangdijia Dec 3, 2025
829ee23
refactor: 使用SentryContext替代Hyperf\Context以管理追踪载体
huangdijia Dec 3, 2025
1a85fd7
refactor: 更新SentryContext以支持Elasticsearch和RPC追踪数据管理
huangdijia Dec 3, 2025
fb027d7
refactor: 优化RPC结果的服务器地址和端口数据设置逻辑
huangdijia Dec 3, 2025
ffbfe0b
refactor: 确保服务器地址在缺失时默认为'unknown'
huangdijia Dec 3, 2025
8ef54bf
refactor: 替换Context为SentryContext以统一追踪管理
huangdijia Dec 3, 2025
c6f080f
refactor: 更新SentryContext以使用数据库服务器地址和端口设置
huangdijia Dec 3, 2025
2497bb3
refactor: 移除不必要的类型转换以简化ElasticsearchSpanData获取逻辑
huangdijia Dec 3, 2025
323a127
refactor: 更新RpcEndpointAspect以使用SentryContext的RPC服务器地址和端口设置
huangdijia Dec 3, 2025
48d4f6a
Apply suggestion from @Copilot
huangdijia Dec 3, 2025
095c1da
refactor: 修正TRACE_CARRIER常量的格式以统一命名约定
huangdijia Dec 3, 2025
9a88598
refactor: 统一使用Context类替代别名以提高代码可读性
huangdijia Dec 3, 2025
8ae1a2e
refactor: 调整TRACE_CARRIER常量的位置以提高代码结构清晰度
huangdijia Dec 3, 2025
9f14095
refactor: 统一SentryContext常量命名约定以提高一致性
huangdijia Dec 3, 2025
1734ae5
refactor: 添加destroyRpcSpanContext方法以简化RPC上下文销毁逻辑
huangdijia Dec 3, 2025
21a426c
refactor: 移除不必要的服务器地址和端口常量及相关方法以简化代码
huangdijia Dec 3, 2025
77d02b2
refactor: 修正CTX_CARRIER常量的命名以提高一致性
huangdijia Dec 3, 2025
c6d818e
refactor: 修正CTX_RPC_SPAN_CONTEXT常量的命名以提高一致性
huangdijia Dec 3, 2025
f295f31
refactor: 移除isTracingEnabled方法以简化代码
huangdijia Dec 3, 2025
67fe140
refactor: 强制转换isTracingDisabled方法的返回值为布尔类型
huangdijia Dec 3, 2025
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
27 changes: 27 additions & 0 deletions src/sentry/src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,49 @@ class Constants
{
public const TRACE_CARRIER = 'sentry.tracing.trace_carrier';

/**
* @deprecated since v3.1, will be removed in v3.2.
*/
public const TRACE_DB_SERVER_ADDRESS = 'sentry.tracing.db.server.address';

/**
* @deprecated since v3.1, will be removed in v3.2.
*/
public const TRACE_DB_SERVER_PORT = 'sentry.tracing.db.server.port';

/**
* @deprecated since v3.1, will be removed in v3.2.
*/
public const TRACE_REDIS_SERVER_ADDRESS = 'sentry.tracing.redis.server.address';

/**
* @deprecated since v3.1, will be removed in v3.2.
*/
public const TRACE_REDIS_SERVER_PORT = 'sentry.tracing.redis.server.port';

/**
* @deprecated since v3.1, will be removed in v3.2.
*/
public const TRACE_RPC_SERVER_ADDRESS = 'sentry.tracing.rpc.server.address';

/**
* @deprecated since v3.1, will be removed in v3.2.
*/
public const TRACE_RPC_SERVER_PORT = 'sentry.tracing.rpc.server.port';

/**
* @deprecated since v3.1, will be removed in v3.2.
*/
public const TRACE_ELASTICSEARCH_REQUEST_DATA = 'sentry.tracing.elasticsearch.request.data';

/**
* @deprecated since v3.1, will be removed in v3.2.
*/
public const CRON_CHECKIN_ID = 'sentry.crons.checkin_id';

/**
* @deprecated since v3.1, will be removed in v3.2.
*/
public const DISABLE_COROUTINE_TRACING = 'sentry.tracing.coroutine.disabled';

public const SENTRY_TRACE = 'sentry-trace';
Expand Down
13 changes: 6 additions & 7 deletions src/sentry/src/Crons/Listener/EventHandleListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@

namespace FriendsOfHyperf\Sentry\Crons\Listener;

use FriendsOfHyperf\Sentry\Constants;
use FriendsOfHyperf\Sentry\Feature;
use Hyperf\Context\Context;
use FriendsOfHyperf\Sentry\SentryContext;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Crontab\Event;
Expand Down Expand Up @@ -90,13 +89,13 @@ protected function handleCrontabTaskStarting(Event\BeforeExecute $event, array $
monitorConfig: $monitorConfig,
);

Context::set(Constants::CRON_CHECKIN_ID, $checkInId);
SentryContext::setCronCheckInId($checkInId);
}

protected function handleCrontabTaskFinished(Event\AfterExecute $event): void
{
/** @var null|string $checkInId */
$checkInId = Context::get(Constants::CRON_CHECKIN_ID);
$checkInId = SentryContext::getCronCheckInId();

if (! $checkInId) {
return;
}
Expand All @@ -113,8 +112,8 @@ protected function handleCrontabTaskFinished(Event\AfterExecute $event): void

protected function handleCrontabTaskFailed(Event\FailToExecute $event): void
{
/** @var null|string $checkInId */
$checkInId = Context::get(Constants::CRON_CHECKIN_ID);
$checkInId = SentryContext::getCronCheckInId();

if (! $checkInId) {
return;
}
Expand Down
6 changes: 6 additions & 0 deletions src/sentry/src/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,17 @@ public function isCronsEnabled(): bool
return (bool) $this->config->get('sentry.crons.enable', true);
}

/**
* @deprecated since v3.1, will be removed in v3.2, use `\FriendsOfHyperf\Sentry\Context::disableTracing()` instead.
*/
public static function disableCoroutineTracing(): void
{
Context::set(Constants::DISABLE_COROUTINE_TRACING, true);
}

/**
* @deprecated since v3.1, will be removed in v3.2, use `\FriendsOfHyperf\Sentry\Context::isTracingDisabled()` instead.
*/
public static function isDisableCoroutineTracing(): bool
{
return (bool) Context::get(Constants::DISABLE_COROUTINE_TRACING);
Expand Down
161 changes: 161 additions & 0 deletions src/sentry/src/SentryContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php

declare(strict_types=1);
/**
* This file is part of friendsofhyperf/components.
*
* @link https://github.com/friendsofhyperf/components
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
* @contact huangdijia@gmail.com
*/

namespace FriendsOfHyperf\Sentry;

use FriendsOfHyperf\Sentry\Util\Carrier;
use Hyperf\Context\Context;
use Sentry\Tracing\SpanContext;

class SentryContext
{
public const CTX_CRON_CHECKIN_ID = 'sentry.ctx.crons.checkin_id';

public const CTX_DISABLE_COROUTINE_TRACING = 'sentry.ctx.coroutine.disabled';

public const CTX_CARRIER = 'sentry.ctx.carrier';

public const CTX_ELASTICSEARCH_SPAN_DATA = 'sentry.ctx.elasticsearch.span.data';

public const CTX_DB_SERVER_ADDRESS = 'sentry.ctx.db.server.address';

public const CTX_DB_SERVER_PORT = 'sentry.ctx.db.server.port';

public const CTX_REDIS_SERVER_ADDRESS = 'sentry.ctx.redis.server.address';

public const CTX_REDIS_SERVER_PORT = 'sentry.ctx.redis.server.port';

public const CTX_RPC_SERVER_ADDRESS = 'sentry.ctx.rpc.server.address';

public const CTX_RPC_SERVER_PORT = 'sentry.ctx.rpc.server.port';

public const CTX_RPC_SPAN_CONTEXT = 'sentry.ctx.rpc.span.context';

public static function disableTracing(): void
{
Context::set(self::CTX_DISABLE_COROUTINE_TRACING, true);
}

public static function enableTracing(): void
{
Context::set(self::CTX_DISABLE_COROUTINE_TRACING, false);
}

public static function isTracingDisabled(): bool
{
return (bool) Context::get(self::CTX_DISABLE_COROUTINE_TRACING, false);
}

public static function setCronCheckInId(string $checkInId): void
{
Context::set(self::CTX_CRON_CHECKIN_ID, $checkInId);
}

public static function getCronCheckInId(): ?string
{
return Context::get(self::CTX_CRON_CHECKIN_ID);
}

public static function setCarrier(Carrier $carrier): void
{
Context::set(self::CTX_CARRIER, $carrier);
}

public static function getCarrier(?int $coroutineId = null): ?Carrier
{
return Context::get(self::CTX_CARRIER, coroutineId: $coroutineId);
}

public static function setRedisServerAddress(string $address): void
{
Context::set(self::CTX_REDIS_SERVER_ADDRESS, $address);
}

public static function getRedisServerAddress(): ?string
{
return Context::get(self::CTX_REDIS_SERVER_ADDRESS);
}

public static function setRedisServerPort(int $port): void
{
Context::set(self::CTX_REDIS_SERVER_PORT, $port);
}

public static function getRedisServerPort(): ?int
{
return Context::get(self::CTX_REDIS_SERVER_PORT);
}

public static function setRpcServerAddress(string $address): void
{
Context::set(self::CTX_RPC_SERVER_ADDRESS, $address);
}

public static function getRpcServerAddress(): ?string
{
return Context::get(self::CTX_RPC_SERVER_ADDRESS);
}

public static function setRpcServerPort(int $port): void
{
Context::set(self::CTX_RPC_SERVER_PORT, $port);
}

public static function getRpcServerPort(): ?int
{
return Context::get(self::CTX_RPC_SERVER_PORT);
}

public static function setDbServerAddress(string $address): void
{
Context::set(self::CTX_DB_SERVER_ADDRESS, $address);
}

public static function getDbServerAddress(): ?string
{
return Context::get(self::CTX_DB_SERVER_ADDRESS);
}

public static function setDbServerPort(int $port): void
{
Context::set(self::CTX_DB_SERVER_PORT, $port);
}

public static function getDbServerPort(): ?int
{
return Context::get(self::CTX_DB_SERVER_PORT);
}

public static function setElasticsearchSpanData(array $data): void
{
Context::set(self::CTX_ELASTICSEARCH_SPAN_DATA, $data);
}

public static function getElasticsearchSpanData(): ?array
{
return Context::get(self::CTX_ELASTICSEARCH_SPAN_DATA);
}

public static function setRpcSpanContext(SpanContext $spanContext): void
{
Context::set(self::CTX_RPC_SPAN_CONTEXT, $spanContext);
}

public static function getRpcSpanContext(): ?SpanContext
{
return Context::get(self::CTX_RPC_SPAN_CONTEXT);
}

public static function destroyRpcSpanContext(): void
{
Context::destroy(self::CTX_RPC_SPAN_CONTEXT);
}
}
9 changes: 5 additions & 4 deletions src/sentry/src/Tracing/Aspect/AsyncQueueJobMessageAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use FriendsOfHyperf\Sentry\Constants;
use FriendsOfHyperf\Sentry\Feature;
use FriendsOfHyperf\Sentry\SentryContext;
use FriendsOfHyperf\Sentry\Util\Carrier;
use Hyperf\AsyncQueue\Driver\RedisDriver;
use Hyperf\Context\Context;
Expand Down Expand Up @@ -77,7 +78,7 @@ public function handlePush(ProceedingJoinPoint $proceedingJoinPoint)

/** @var \Hyperf\AsyncQueue\Driver\Driver $driver */
$driver = $proceedingJoinPoint->getInstance();
$messageId = method_exists($job, 'getId') ? $job->getId() : SentryUid::generate();
$messageId = method_exists($job, 'getId') ? call_user_func([$job, 'getId']) : SentryUid::generate();
$destinationName = Context::get('sentry.messaging.destination.name', 'default');
$bodySize = (fn ($job) => strlen($this->packer->pack($job)))->call($driver, $job);
$data = [
Expand Down Expand Up @@ -106,7 +107,7 @@ function (Scope $scope) use ($proceedingJoinPoint, $messageId, $destinationName,
$carrier = Carrier::fromArray([])->with($extra);
}

Context::set(Constants::TRACE_CARRIER, $carrier);
SentryContext::setCarrier($carrier);

return $proceedingJoinPoint->process();
},
Expand Down Expand Up @@ -140,7 +141,7 @@ protected function buildSpanDataOfRedisDriver(RedisDriver $driver): array
protected function handleSerialize(ProceedingJoinPoint $proceedingJoinPoint)
{
return with($proceedingJoinPoint->process(), function ($result) {
if (is_array($result) && $carrier = Context::get(Constants::TRACE_CARRIER)) {
if (is_array($result) && $carrier = SentryContext::getCarrier()) {
if (array_is_list($result)) {
$result[] = $carrier->toJson();
} elseif (isset($result['job'])) {
Expand All @@ -164,7 +165,7 @@ protected function handleUnserialize(ProceedingJoinPoint $proceedingJoinPoint)

/** @var null|string $carrier */
if ($carrier) {
Context::set(Constants::TRACE_CARRIER, Carrier::fromJson($carrier));
SentryContext::setCarrier(Carrier::fromJson($carrier));
}

return $proceedingJoinPoint->process();
Expand Down
3 changes: 2 additions & 1 deletion src/sentry/src/Tracing/Aspect/CoroutineAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use FriendsOfHyperf\Sentry\Feature;
use FriendsOfHyperf\Sentry\Integration;
use FriendsOfHyperf\Sentry\SentryContext;
use FriendsOfHyperf\Sentry\Util\CoroutineBacktraceHelper;
use Hyperf\Context\Context;
use Hyperf\Di\Aop\AbstractAspect;
Expand Down Expand Up @@ -46,7 +47,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
if (
! $this->feature->isTracingSpanEnabled('coroutine')
|| Feature::isDisableCoroutineTracing()
|| SentryContext::isTracingDisabled()
) {
return $proceedingJoinPoint->process();
}
Expand Down
11 changes: 5 additions & 6 deletions src/sentry/src/Tracing/Aspect/DbAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@

namespace FriendsOfHyperf\Sentry\Tracing\Aspect;

use FriendsOfHyperf\Sentry\Constants;
use FriendsOfHyperf\Sentry\Feature;
use FriendsOfHyperf\Sentry\SentryContext;
use FriendsOfHyperf\Sentry\Util\SqlParser;
use Hyperf\Context\Context;
use Hyperf\DB\Pool\PoolFactory;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
Expand Down Expand Up @@ -71,8 +70,8 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
/** @var \Hyperf\DB\AbstractConnection $connection */
$server = $this->serverCache[$connection] ?? null;
if ($server !== null) {
Context::set(Constants::TRACE_DB_SERVER_ADDRESS, $server['host'] ?? 'localhost');
Context::set(Constants::TRACE_DB_SERVER_PORT, $server['port'] ?? 3306);
SentryContext::setDbServerAddress($server['host'] ?? 'localhost');
SentryContext::setDbServerPort((int) ($server['port'] ?? 3306));
}
});
}
Expand Down Expand Up @@ -108,8 +107,8 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
'db.pool.max_idle_time' => $pool->getOption()->getMaxIdleTime(),
'db.pool.idle' => $pool->getConnectionsInChannel(),
'db.pool.using' => $pool->getCurrentConnections(),
'server.host' => Context::get(Constants::TRACE_DB_SERVER_ADDRESS, 'localhost'),
'server.port' => Context::get(Constants::TRACE_DB_SERVER_PORT, 3306),
'server.host' => SentryContext::getDbServerAddress() ?? 'localhost',
'server.port' => SentryContext::getDbServerPort() ?? 3306,
];

if ($this->feature->isTracingTagEnabled('db.sql.bindings', true)) {
Expand Down
6 changes: 3 additions & 3 deletions src/sentry/src/Tracing/Aspect/DbConnectionAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

namespace FriendsOfHyperf\Sentry\Tracing\Aspect;

use FriendsOfHyperf\Sentry\Constants;
use FriendsOfHyperf\Sentry\Feature;
use FriendsOfHyperf\Sentry\SentryContext;
use Hyperf\Context\Context;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
Expand Down Expand Up @@ -57,8 +57,8 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
$server = $this->serverCache[$pdo] ?? null;

if (is_array($server)) {
Context::set(Constants::TRACE_DB_SERVER_ADDRESS, $server['host']);
Context::set(Constants::TRACE_DB_SERVER_PORT, $server['port']);
SentryContext::setDbServerAddress($server['host']);
SentryContext::setDbServerPort((int) $server['port']);
}

return true;
Expand Down
Loading