From 5e62f2adfaeace29dc6da237f3dee951105869be Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sat, 22 Nov 2025 21:31:25 +0800 Subject: [PATCH] feat: add feature flag support for tracing aspects Add Feature dependency injection to tracing aspects to allow conditional span creation based on feature flags. This enables fine-grained control over which types of operations are traced (db, redis, elasticsearch, rpc). Changes: - DbConnectionAspect: Check db tracing feature flag before caching server info - RedisConnectionAspect: Check redis tracing feature flag before caching slot info - ElasticsearchRequestAspect: Check elasticsearch tracing feature flag before processing - RpcEndpointAspect: Check rpc tracing feature flag before storing server address This allows users to selectively enable/disable tracing for specific operation types without modifying code. --- src/sentry/src/Tracing/Aspect/DbConnectionAspect.php | 7 ++++++- .../src/Tracing/Aspect/ElasticsearchRequestAspect.php | 9 +++++++++ src/sentry/src/Tracing/Aspect/RedisConnectionAspect.php | 7 ++++++- src/sentry/src/Tracing/Aspect/RpcEndpointAspect.php | 9 +++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/sentry/src/Tracing/Aspect/DbConnectionAspect.php b/src/sentry/src/Tracing/Aspect/DbConnectionAspect.php index 9af6280ae..18bf171aa 100644 --- a/src/sentry/src/Tracing/Aspect/DbConnectionAspect.php +++ b/src/sentry/src/Tracing/Aspect/DbConnectionAspect.php @@ -12,6 +12,7 @@ namespace FriendsOfHyperf\Sentry\Tracing\Aspect; use FriendsOfHyperf\Sentry\Constants; +use FriendsOfHyperf\Sentry\Feature; use Hyperf\Context\Context; use Hyperf\Di\Aop\AbstractAspect; use Hyperf\Di\Aop\ProceedingJoinPoint; @@ -29,7 +30,7 @@ class DbConnectionAspect extends AbstractAspect private WeakMap $serverCache; - public function __construct() + public function __construct(protected Feature $feature) { $this->serverCache = new WeakMap(); } @@ -37,6 +38,10 @@ public function __construct() public function process(ProceedingJoinPoint $proceedingJoinPoint) { return tap($proceedingJoinPoint->process(), function ($pdo) use ($proceedingJoinPoint) { + if (! $this->feature->isTracingSpanEnabled('db')) { + return; + } + if ($proceedingJoinPoint->methodName === 'createPdoConnection') { $dsn = $proceedingJoinPoint->arguments['keys']['dsn'] ?? ''; $pattern = '/host=([^;]+);port=(\d+);?/'; diff --git a/src/sentry/src/Tracing/Aspect/ElasticsearchRequestAspect.php b/src/sentry/src/Tracing/Aspect/ElasticsearchRequestAspect.php index 1c7f504d3..844de3b76 100644 --- a/src/sentry/src/Tracing/Aspect/ElasticsearchRequestAspect.php +++ b/src/sentry/src/Tracing/Aspect/ElasticsearchRequestAspect.php @@ -12,6 +12,7 @@ namespace FriendsOfHyperf\Sentry\Tracing\Aspect; use FriendsOfHyperf\Sentry\Constants; +use FriendsOfHyperf\Sentry\Feature; use Hyperf\Context\Context; use Hyperf\Di\Aop\AbstractAspect; use Hyperf\Di\Aop\ProceedingJoinPoint; @@ -26,8 +27,16 @@ class ElasticsearchRequestAspect extends AbstractAspect 'Elastic\Elasticsearch\Client::sendRequest', ]; + public function __construct(protected Feature $feature) + { + } + public function process(ProceedingJoinPoint $proceedingJoinPoint) { + if (! $this->feature->isTracingSpanEnabled('elasticsearch')) { + return $proceedingJoinPoint->process(); + } + if ($proceedingJoinPoint->methodName === 'sendRequest') { // ES 8.x $request = $proceedingJoinPoint->arguments['keys']['request'] ?? null; if ($request instanceof RequestInterface) { diff --git a/src/sentry/src/Tracing/Aspect/RedisConnectionAspect.php b/src/sentry/src/Tracing/Aspect/RedisConnectionAspect.php index 8cf006f6e..23b1a5e7e 100644 --- a/src/sentry/src/Tracing/Aspect/RedisConnectionAspect.php +++ b/src/sentry/src/Tracing/Aspect/RedisConnectionAspect.php @@ -12,6 +12,7 @@ namespace FriendsOfHyperf\Sentry\Tracing\Aspect; use FriendsOfHyperf\Sentry\Constants; +use FriendsOfHyperf\Sentry\Feature; use FriendsOfHyperf\Sentry\Util\RedisClusterKeySlot; use Hyperf\Context\Context; use Hyperf\Di\Aop\AbstractAspect; @@ -30,7 +31,7 @@ class RedisConnectionAspect extends AbstractAspect private WeakMap $slotNodeCache; - public function __construct() + public function __construct(protected Feature $feature) { $this->slotNodeCache = new WeakMap(); } @@ -38,6 +39,10 @@ public function __construct() public function process(ProceedingJoinPoint $proceedingJoinPoint) { return tap($proceedingJoinPoint->process(), function ($result) use ($proceedingJoinPoint) { + if (! $this->feature->isTracingSpanEnabled('redis')) { + return; + } + $redisConnection = $proceedingJoinPoint->getInstance(); $connection = (fn () => $this->connection ?? null)->call($redisConnection); diff --git a/src/sentry/src/Tracing/Aspect/RpcEndpointAspect.php b/src/sentry/src/Tracing/Aspect/RpcEndpointAspect.php index e83ae8e3c..9388ed283 100644 --- a/src/sentry/src/Tracing/Aspect/RpcEndpointAspect.php +++ b/src/sentry/src/Tracing/Aspect/RpcEndpointAspect.php @@ -12,6 +12,7 @@ namespace FriendsOfHyperf\Sentry\Tracing\Aspect; use FriendsOfHyperf\Sentry\Constants; +use FriendsOfHyperf\Sentry\Feature; use Hyperf\Context\Context; use Hyperf\Di\Aop\AbstractAspect; use Hyperf\Di\Aop\ProceedingJoinPoint; @@ -26,9 +27,17 @@ class RpcEndpointAspect extends AbstractAspect 'Hyperf\JsonRpc\JsonRpcPoolTransporter::getConnection', ]; + public function __construct(protected Feature $feature) + { + } + public function process(ProceedingJoinPoint $proceedingJoinPoint) { return tap($proceedingJoinPoint->process(), function ($result) { + if (! $this->feature->isTracingSpanEnabled('rpc')) { + return; + } + // RpcMultiplex if ($result instanceof \Hyperf\RpcMultiplex\Socket) { Context::set(Constants::TRACE_RPC_SERVER_ADDRESS, $result->getName());