From 84215ce4b85a96eb584de990b8ec46142c743f61 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sat, 22 Nov 2025 15:18:16 +0800 Subject: [PATCH 1/3] feat: enhance DB connection tracing with real-time server address detection This change improves database tracing by capturing and tracking the actual server addresses for database connections. Key improvements include: - Implement WeakMap-based caching to store connection configurations - Intercept reconnect and getConnection methods to capture server details - Track server host and port information in tracing context - Optimize aspect to capture connection details at the right lifecycle point This enables more accurate distributed tracing with actual database server addresses instead of placeholder values. --- src/sentry/src/Tracing/Aspect/DbAspect.php | 48 ++++++++++++++++++---- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/src/sentry/src/Tracing/Aspect/DbAspect.php b/src/sentry/src/Tracing/Aspect/DbAspect.php index f8e97902e..a5b382086 100644 --- a/src/sentry/src/Tracing/Aspect/DbAspect.php +++ b/src/sentry/src/Tracing/Aspect/DbAspect.php @@ -11,31 +11,42 @@ namespace FriendsOfHyperf\Sentry\Tracing\Aspect; +use FriendsOfHyperf\Sentry\Constants; use FriendsOfHyperf\Sentry\Feature; use FriendsOfHyperf\Sentry\Util\SqlParser; -use Hyperf\DB\DB; +use Hyperf\Context\Context; use Hyperf\DB\Pool\PoolFactory; use Hyperf\Di\Aop\AbstractAspect; use Hyperf\Di\Aop\ProceedingJoinPoint; +use PDO; use Psr\Container\ContainerInterface; use Sentry\State\Scope; use Sentry\Tracing\SpanContext; +use WeakMap; use function FriendsOfHyperf\Sentry\trace; +use function Hyperf\Tappable\tap; /** * @property string $poolName + * @property PDO $connection + * @property array $config */ class DbAspect extends AbstractAspect { public array $classes = [ - DB::class . '::__call', + 'Hyperf\DB\MySQLConnection::reconnect', + 'Hyperf\DB\DB::getConnection', + 'Hyperf\DB\DB::__call', ]; + private WeakMap $serverCache; + public function __construct( protected ContainerInterface $container, protected Feature $feature ) { + $this->serverCache = new WeakMap(); } public function process(ProceedingJoinPoint $proceedingJoinPoint) @@ -44,14 +55,34 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) return $proceedingJoinPoint->process(); } + if ($proceedingJoinPoint->methodName === 'reconnect') { + return tap($proceedingJoinPoint->process(), function () use ($proceedingJoinPoint) { + /** @var \Hyperf\DB\AbstractConnection $connection */ + $connection = $proceedingJoinPoint->getInstance(); + $this->serverCache[$connection] = $connection->getConfig(); + }); + } + + if ($proceedingJoinPoint->methodName === 'getConnection') { + return tap($proceedingJoinPoint->process(), function ($connection) { + $server = $this->serverCache[$connection] ?? null; + if ($server !== null) { + Context::set(Constants::TRACE_DB_SERVER_ADDRESS, $server['host']); + Context::set(Constants::TRACE_DB_SERVER_PORT, $server['port'] ?? 3306); + } + }); + } + $arguments = $proceedingJoinPoint->arguments['keys']; - $poolName = (fn () => $this->poolName)->call($proceedingJoinPoint->getInstance()); - /** @var \Hyperf\Pool\Pool $pool */ - $pool = $this->container->get(PoolFactory::class)->getPool($poolName); $operation = $arguments['name']; $database = ''; $driver = 'unknown'; - $table = ''; + + /** @var \Hyperf\DB\DB $instance */ + $instance = $proceedingJoinPoint->getInstance(); + $poolName = (fn () => $this->poolName)->call($instance); + /** @var \Hyperf\Pool\Pool $pool */ + $pool = $this->container->get(PoolFactory::class)->getPool($poolName); if ($pool instanceof \Hyperf\DB\Pool\Pool) { $config = $pool->getConfig(); $database = $config['database'] ?? ''; @@ -62,6 +93,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) $sqlParse = SqlParser::parse($sql); $table = $sqlParse['table']; $operation = $sqlParse['operation']; + $data = [ 'db.system' => $driver, 'db.name' => $database, @@ -72,8 +104,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' => '', - // 'server.port' => '', + 'server.host' => Context::get(Constants::TRACE_DB_SERVER_ADDRESS, 'localhost'), + 'server.port' => Context::get(Constants::TRACE_DB_SERVER_PORT, 3306), ]; if ($this->feature->isTracingTagEnabled('db.sql.bindings', true)) { From b8e5a06c283d8228c83293b300635a523bc7dd2c Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sat, 22 Nov 2025 15:23:23 +0800 Subject: [PATCH 2/3] feat: improve server address tracing in DbAspect with default values --- src/sentry/src/Tracing/Aspect/DbAspect.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/sentry/src/Tracing/Aspect/DbAspect.php b/src/sentry/src/Tracing/Aspect/DbAspect.php index a5b382086..2f7082c33 100644 --- a/src/sentry/src/Tracing/Aspect/DbAspect.php +++ b/src/sentry/src/Tracing/Aspect/DbAspect.php @@ -40,6 +40,9 @@ class DbAspect extends AbstractAspect 'Hyperf\DB\DB::__call', ]; + /** + * @var WeakMap<\Hyperf\DB\AbstractConnection,array> + */ private WeakMap $serverCache; public function __construct( @@ -65,9 +68,10 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) if ($proceedingJoinPoint->methodName === 'getConnection') { return tap($proceedingJoinPoint->process(), function ($connection) { + /** @var \Hyperf\DB\AbstractConnection $connection */ $server = $this->serverCache[$connection] ?? null; if ($server !== null) { - Context::set(Constants::TRACE_DB_SERVER_ADDRESS, $server['host']); + Context::set(Constants::TRACE_DB_SERVER_ADDRESS, $server['host'] ?? 'localhost'); Context::set(Constants::TRACE_DB_SERVER_PORT, $server['port'] ?? 3306); } }); From 3417d53b81afe5d1b3931986f0e5c2a399e80055 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sat, 22 Nov 2025 15:26:03 +0800 Subject: [PATCH 3/3] feat: improve driver detection logic in DbAspect for database connections --- src/sentry/src/Tracing/Aspect/DbAspect.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sentry/src/Tracing/Aspect/DbAspect.php b/src/sentry/src/Tracing/Aspect/DbAspect.php index 2f7082c33..a0e149272 100644 --- a/src/sentry/src/Tracing/Aspect/DbAspect.php +++ b/src/sentry/src/Tracing/Aspect/DbAspect.php @@ -90,7 +90,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) if ($pool instanceof \Hyperf\DB\Pool\Pool) { $config = $pool->getConfig(); $database = $config['database'] ?? ''; - $driver = $config['driver'] ?? 'unknown'; + $driver = $config['driver'] ?? $driver; } $sql = $arguments['arguments']['query'] ?? '';