From 2ee65ee1d77473c19ed0a7db668717b80d6f6402 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Thu, 20 Nov 2025 14:06:13 +0800 Subject: [PATCH 1/7] feat: add server address tracing for Redis connections - Add server.address and server.port to Redis tracing data - Support cluster and sentinel configurations - Add ConnectionContainer utility for WeakMap-based connection tracking - Add RedisConnectionAspect for connection configuration capture (commented out in config) --- src/sentry/src/ConfigProvider.php | 1 + .../Tracing/Aspect/RedisConnectionAspect.php | 40 +++++++++++++++ .../Tracing/Listener/EventHandleListener.php | 6 +++ src/sentry/src/Util/ConnectionContainer.php | 49 +++++++++++++++++++ 4 files changed, 96 insertions(+) create mode 100644 src/sentry/src/Tracing/Aspect/RedisConnectionAspect.php create mode 100644 src/sentry/src/Util/ConnectionContainer.php diff --git a/src/sentry/src/ConfigProvider.php b/src/sentry/src/ConfigProvider.php index e9c1efe0e..1212d55d9 100644 --- a/src/sentry/src/ConfigProvider.php +++ b/src/sentry/src/ConfigProvider.php @@ -41,6 +41,7 @@ public function __invoke(): array Tracing\Aspect\KafkaProducerAspect::class, Tracing\Aspect\RpcAspect::class, Tracing\Aspect\RpcEndpointAspect::class, + // Tracing\Aspect\RedisConnectionAspect::class, Tracing\Aspect\RedisAspect::class, Tracing\Aspect\TraceAnnotationAspect::class, Tracing\Aspect\ViewRenderAspect::class, diff --git a/src/sentry/src/Tracing/Aspect/RedisConnectionAspect.php b/src/sentry/src/Tracing/Aspect/RedisConnectionAspect.php new file mode 100644 index 000000000..289e9cda8 --- /dev/null +++ b/src/sentry/src/Tracing/Aspect/RedisConnectionAspect.php @@ -0,0 +1,40 @@ +process(), function ($connection) use ($proceedingJoinPoint) { + $instance = $proceedingJoinPoint->getInstance(); + $config = (fn () => $this->config ?? [])->call($instance); + + match ($proceedingJoinPoint->methodName) { + 'createRedis' => ConnectionContainer::set($connection, $config), + 'createRedisCluster' => ConnectionContainer::set($connection, $config['cluster'] ?? []), + 'createRedisSentinel' => ConnectionContainer::set($connection, $config['sentinel'] ?? []), + default => null, + }; + }); + } +} diff --git a/src/sentry/src/Tracing/Listener/EventHandleListener.php b/src/sentry/src/Tracing/Listener/EventHandleListener.php index 490b3b993..5daa4945c 100644 --- a/src/sentry/src/Tracing/Listener/EventHandleListener.php +++ b/src/sentry/src/Tracing/Listener/EventHandleListener.php @@ -466,6 +466,11 @@ protected function handleRedisCommandExecuted(RedisEvent\CommandExecuted $event) $pool = $this->container->get(RedisPoolFactory::class)->getPool($event->connectionName); $config = $this->config->get('redis.' . $event->connectionName, []); + $serverData = match (true) { + $config['cluster']['enable'] ?? false => ['server.address' => $config['cluster']['nodes'] ?? null], + $config['sentinel']['enable'] ?? false => ['server.address' => $config['sentinel']['nodes'] ?? null], + default => ['server.address' => $config['host'] ?? null, 'server.port' => $config['port'] ?? null], + }; $redisStatement = (string) new RedisCommand($event->command, $event->parameters); trace( @@ -497,6 +502,7 @@ function (Scope $scope) use ($event) { 'db.redis.pool.idle' => $pool->getConnectionsInChannel(), 'db.redis.pool.using' => $pool->getCurrentConnections(), 'duration' => $event->time * 1000, + ...$serverData, ]) ->setStartTimestamp(microtime(true) - $event->time / 1000) ); diff --git a/src/sentry/src/Util/ConnectionContainer.php b/src/sentry/src/Util/ConnectionContainer.php new file mode 100644 index 000000000..46befa0db --- /dev/null +++ b/src/sentry/src/Util/ConnectionContainer.php @@ -0,0 +1,49 @@ + Date: Thu, 20 Nov 2025 14:21:51 +0800 Subject: [PATCH 2/7] feat: improve server address handling for Redis connections --- .../src/Tracing/Listener/EventHandleListener.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/sentry/src/Tracing/Listener/EventHandleListener.php b/src/sentry/src/Tracing/Listener/EventHandleListener.php index 5daa4945c..78c2c886a 100644 --- a/src/sentry/src/Tracing/Listener/EventHandleListener.php +++ b/src/sentry/src/Tracing/Listener/EventHandleListener.php @@ -467,9 +467,16 @@ protected function handleRedisCommandExecuted(RedisEvent\CommandExecuted $event) $pool = $this->container->get(RedisPoolFactory::class)->getPool($event->connectionName); $config = $this->config->get('redis.' . $event->connectionName, []); $serverData = match (true) { - $config['cluster']['enable'] ?? false => ['server.address' => $config['cluster']['nodes'] ?? null], - $config['sentinel']['enable'] ?? false => ['server.address' => $config['sentinel']['nodes'] ?? null], - default => ['server.address' => $config['host'] ?? null, 'server.port' => $config['port'] ?? null], + $config['cluster']['enable'] ?? false => [ + 'server.address' => implode(',', $config['cluster']['nodes'] ?? []), + ], + $config['sentinel']['enable'] ?? false => [ + 'server.address' => implode(',', $config['sentinel']['nodes'] ?? []), + ], + default => [ + 'server.address' => $config['host'] ?? '', + 'server.port' => $config['port'] ?? 0, + ], }; $redisStatement = (string) new RedisCommand($event->command, $event->parameters); From db8c96ed54f773f94528cc90fb57af8fa066c0f4 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Fri, 21 Nov 2025 11:40:23 +0800 Subject: [PATCH 3/7] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20Redis=20?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=99=A8=E5=9C=B0=E5=9D=80=E5=92=8C=E7=AB=AF?= =?UTF-8?q?=E5=8F=A3=E8=BF=BD=E8=B8=AA=E5=B8=B8=E9=87=8F=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=9B=B8=E5=85=B3=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sentry/src/Constants.php | 4 ++ .../Tracing/Aspect/RedisConnectionAspect.php | 28 ++++++----- .../Tracing/Listener/EventHandleListener.php | 15 +----- src/sentry/src/Util/ConnectionContainer.php | 49 ------------------- 4 files changed, 23 insertions(+), 73 deletions(-) delete mode 100644 src/sentry/src/Util/ConnectionContainer.php diff --git a/src/sentry/src/Constants.php b/src/sentry/src/Constants.php index a2d937381..40e8dff2a 100644 --- a/src/sentry/src/Constants.php +++ b/src/sentry/src/Constants.php @@ -15,6 +15,10 @@ class Constants { public const TRACE_CARRIER = 'sentry.tracing.trace_carrier'; + public const TRACE_REDIS_SERVER_ADDRESS = 'sentry.tracing.redis.server.address'; + + public const TRACE_REDIS_SERVER_PORT = 'sentry.tracing.redis.server.port'; + public const TRACE_RPC_SERVER_ADDRESS = 'sentry.tracing.rpc.server.address'; public const TRACE_RPC_SERVER_PORT = 'sentry.tracing.rpc.server.port'; diff --git a/src/sentry/src/Tracing/Aspect/RedisConnectionAspect.php b/src/sentry/src/Tracing/Aspect/RedisConnectionAspect.php index 289e9cda8..963bdda28 100644 --- a/src/sentry/src/Tracing/Aspect/RedisConnectionAspect.php +++ b/src/sentry/src/Tracing/Aspect/RedisConnectionAspect.php @@ -11,30 +11,36 @@ namespace FriendsOfHyperf\Sentry\Tracing\Aspect; -use FriendsOfHyperf\Sentry\Util\ConnectionContainer; +use FriendsOfHyperf\Sentry\Constants; +use Hyperf\Context\Context; use Hyperf\Di\Aop\AbstractAspect; use Hyperf\Di\Aop\ProceedingJoinPoint; +use Redis; +use RedisCluster; use function Hyperf\Tappable\tap; class RedisConnectionAspect extends AbstractAspect { public array $classes = [ - 'Hyperf\Redis\RedisConnection::create*', + 'Hyperf\Redis\RedisConnection::__call', ]; public function process(ProceedingJoinPoint $proceedingJoinPoint) { return tap($proceedingJoinPoint->process(), function ($connection) use ($proceedingJoinPoint) { - $instance = $proceedingJoinPoint->getInstance(); - $config = (fn () => $this->config ?? [])->call($instance); - - match ($proceedingJoinPoint->methodName) { - 'createRedis' => ConnectionContainer::set($connection, $config), - 'createRedisCluster' => ConnectionContainer::set($connection, $config['cluster'] ?? []), - 'createRedisSentinel' => ConnectionContainer::set($connection, $config['sentinel'] ?? []), - default => null, - }; + $redisConnection = $proceedingJoinPoint->getInstance(); + $config = (fn () => $this->config ?? [])->call($redisConnection); + $connection = (fn () => $this->connection ?? null)->call($redisConnection); + + if ($connection instanceof Redis) { + Context::set(Constants::TRACE_REDIS_SERVER_ADDRESS, $config['host'] ?? 'localhost'); + Context::set(Constants::TRACE_REDIS_SERVER_PORT, $config['port'] ?? 6379); + } + + if ($connection instanceof RedisCluster) { + // TODO: support RedisCluster + } }); } } diff --git a/src/sentry/src/Tracing/Listener/EventHandleListener.php b/src/sentry/src/Tracing/Listener/EventHandleListener.php index 78c2c886a..0a4159229 100644 --- a/src/sentry/src/Tracing/Listener/EventHandleListener.php +++ b/src/sentry/src/Tracing/Listener/EventHandleListener.php @@ -466,18 +466,6 @@ protected function handleRedisCommandExecuted(RedisEvent\CommandExecuted $event) $pool = $this->container->get(RedisPoolFactory::class)->getPool($event->connectionName); $config = $this->config->get('redis.' . $event->connectionName, []); - $serverData = match (true) { - $config['cluster']['enable'] ?? false => [ - 'server.address' => implode(',', $config['cluster']['nodes'] ?? []), - ], - $config['sentinel']['enable'] ?? false => [ - 'server.address' => implode(',', $config['sentinel']['nodes'] ?? []), - ], - default => [ - 'server.address' => $config['host'] ?? '', - 'server.port' => $config['port'] ?? 0, - ], - }; $redisStatement = (string) new RedisCommand($event->command, $event->parameters); trace( @@ -509,7 +497,8 @@ function (Scope $scope) use ($event) { 'db.redis.pool.idle' => $pool->getConnectionsInChannel(), 'db.redis.pool.using' => $pool->getCurrentConnections(), 'duration' => $event->time * 1000, - ...$serverData, + 'server.address' => (string) Context::get(Constants::TRACE_REDIS_SERVER_ADDRESS, 'localhost'), + 'server.port' => (int) Context::get(Constants::TRACE_REDIS_SERVER_PORT, 6379), ]) ->setStartTimestamp(microtime(true) - $event->time / 1000) ); diff --git a/src/sentry/src/Util/ConnectionContainer.php b/src/sentry/src/Util/ConnectionContainer.php deleted file mode 100644 index 46befa0db..000000000 --- a/src/sentry/src/Util/ConnectionContainer.php +++ /dev/null @@ -1,49 +0,0 @@ - Date: Fri, 21 Nov 2025 11:51:19 +0800 Subject: [PATCH 4/7] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=20Redis=20?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E8=BF=BD=E8=B8=AA=E9=80=BB=E8=BE=91=E4=BB=A5?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E8=8E=B7=E5=8F=96=E6=9C=8D=E5=8A=A1=E5=99=A8?= =?UTF-8?q?=E5=9C=B0=E5=9D=80=E5=92=8C=E7=AB=AF=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sentry/src/Tracing/Aspect/RedisConnectionAspect.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/sentry/src/Tracing/Aspect/RedisConnectionAspect.php b/src/sentry/src/Tracing/Aspect/RedisConnectionAspect.php index 963bdda28..7befb1891 100644 --- a/src/sentry/src/Tracing/Aspect/RedisConnectionAspect.php +++ b/src/sentry/src/Tracing/Aspect/RedisConnectionAspect.php @@ -30,12 +30,11 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) { return tap($proceedingJoinPoint->process(), function ($connection) use ($proceedingJoinPoint) { $redisConnection = $proceedingJoinPoint->getInstance(); - $config = (fn () => $this->config ?? [])->call($redisConnection); $connection = (fn () => $this->connection ?? null)->call($redisConnection); if ($connection instanceof Redis) { - Context::set(Constants::TRACE_REDIS_SERVER_ADDRESS, $config['host'] ?? 'localhost'); - Context::set(Constants::TRACE_REDIS_SERVER_PORT, $config['port'] ?? 6379); + Context::set(Constants::TRACE_REDIS_SERVER_ADDRESS, $connection->getHost()); + Context::set(Constants::TRACE_REDIS_SERVER_PORT, $connection->getPort()); } if ($connection instanceof RedisCluster) { From ca6a665f49cac55698d1dbe5ae696581102c57e0 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Fri, 21 Nov 2025 11:52:06 +0800 Subject: [PATCH 5/7] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=20Redis=20?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E8=BF=BD=E8=B8=AA=E9=80=BB=E8=BE=91=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A=E4=BB=A5=E5=8C=BA=E5=88=86?= =?UTF-8?q?=20Redis=20=E5=92=8C=20RedisCluster?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sentry/src/Tracing/Aspect/RedisConnectionAspect.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sentry/src/Tracing/Aspect/RedisConnectionAspect.php b/src/sentry/src/Tracing/Aspect/RedisConnectionAspect.php index 7befb1891..b5a06ac84 100644 --- a/src/sentry/src/Tracing/Aspect/RedisConnectionAspect.php +++ b/src/sentry/src/Tracing/Aspect/RedisConnectionAspect.php @@ -32,12 +32,12 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) $redisConnection = $proceedingJoinPoint->getInstance(); $connection = (fn () => $this->connection ?? null)->call($redisConnection); - if ($connection instanceof Redis) { + if ($connection instanceof Redis) { // Redis or RedisSentinel Context::set(Constants::TRACE_REDIS_SERVER_ADDRESS, $connection->getHost()); Context::set(Constants::TRACE_REDIS_SERVER_PORT, $connection->getPort()); } - if ($connection instanceof RedisCluster) { + if ($connection instanceof RedisCluster) { // RedisCluster // TODO: support RedisCluster } }); From a6d7b995958c1038e5ebcc5bd1211a035fec58fe Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Fri, 21 Nov 2025 11:56:43 +0800 Subject: [PATCH 6/7] =?UTF-8?q?fix:=20=E4=BF=AE=E6=AD=A3=20RedisConnection?= =?UTF-8?q?Aspect=20=E4=B8=AD=E7=9A=84=E5=8F=98=E9=87=8F=E5=91=BD=E5=90=8D?= =?UTF-8?q?=E4=BB=A5=E6=8F=90=E9=AB=98=E4=BB=A3=E7=A0=81=E5=8F=AF=E8=AF=BB?= =?UTF-8?q?=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sentry/src/Tracing/Aspect/RedisConnectionAspect.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sentry/src/Tracing/Aspect/RedisConnectionAspect.php b/src/sentry/src/Tracing/Aspect/RedisConnectionAspect.php index b5a06ac84..9763e67fa 100644 --- a/src/sentry/src/Tracing/Aspect/RedisConnectionAspect.php +++ b/src/sentry/src/Tracing/Aspect/RedisConnectionAspect.php @@ -28,7 +28,7 @@ class RedisConnectionAspect extends AbstractAspect public function process(ProceedingJoinPoint $proceedingJoinPoint) { - return tap($proceedingJoinPoint->process(), function ($connection) use ($proceedingJoinPoint) { + return tap($proceedingJoinPoint->process(), function ($result) use ($proceedingJoinPoint) { $redisConnection = $proceedingJoinPoint->getInstance(); $connection = (fn () => $this->connection ?? null)->call($redisConnection); From 0b31cb9a69ee66965c5be883a51198b029a2cf5e Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Fri, 21 Nov 2025 12:09:39 +0800 Subject: [PATCH 7/7] =?UTF-8?q?feat:=20=E5=90=AF=E7=94=A8=20RedisConnectio?= =?UTF-8?q?nAspect=20=E4=BB=A5=E6=94=AF=E6=8C=81=20Redis=20=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E8=BF=BD=E8=B8=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sentry/src/ConfigProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sentry/src/ConfigProvider.php b/src/sentry/src/ConfigProvider.php index 1212d55d9..5a24ed040 100644 --- a/src/sentry/src/ConfigProvider.php +++ b/src/sentry/src/ConfigProvider.php @@ -41,7 +41,7 @@ public function __invoke(): array Tracing\Aspect\KafkaProducerAspect::class, Tracing\Aspect\RpcAspect::class, Tracing\Aspect\RpcEndpointAspect::class, - // Tracing\Aspect\RedisConnectionAspect::class, + Tracing\Aspect\RedisConnectionAspect::class, Tracing\Aspect\RedisAspect::class, Tracing\Aspect\TraceAnnotationAspect::class, Tracing\Aspect\ViewRenderAspect::class,