Skip to content
1 change: 1 addition & 0 deletions src/sentry/src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions src/sentry/src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
45 changes: 45 additions & 0 deletions src/sentry/src/Tracing/Aspect/RedisConnectionAspect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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\Tracing\Aspect;

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::__call',
];

public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
return tap($proceedingJoinPoint->process(), function ($result) use ($proceedingJoinPoint) {
$redisConnection = $proceedingJoinPoint->getInstance();
$connection = (fn () => $this->connection ?? null)->call($redisConnection);

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) { // RedisCluster
// TODO: support RedisCluster
}
});
}
}
2 changes: 2 additions & 0 deletions src/sentry/src/Tracing/Listener/EventHandleListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,8 @@ function (Scope $scope) use ($event) {
'db.redis.pool.idle' => $pool->getConnectionsInChannel(),
'db.redis.pool.using' => $pool->getCurrentConnections(),
'duration' => $event->time * 1000,
'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)
);
Expand Down