Skip to content
Merged
Changes from all commits
Commits
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
38 changes: 37 additions & 1 deletion src/sentry/src/Tracing/Aspect/RedisConnectionAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Hyperf\Di\Aop\ProceedingJoinPoint;
use Redis;
use RedisCluster;
use WeakMap;

use function Hyperf\Tappable\tap;

Expand All @@ -26,6 +27,13 @@ class RedisConnectionAspect extends AbstractAspect
'Hyperf\Redis\RedisConnection::__call',
];

private WeakMap $slotNodeCache;

public function __construct()
{
$this->slotNodeCache = new WeakMap();
}

public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
return tap($proceedingJoinPoint->process(), function ($result) use ($proceedingJoinPoint) {
Expand All @@ -38,8 +46,36 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
}

if ($connection instanceof RedisCluster) { // RedisCluster
// TODO: support RedisCluster
$arguments = $proceedingJoinPoint->arguments['keys']['arguments'] ?? [];
$key = $arguments[0] ?? null;
if (is_string($key)) {
$node = $this->getClusterNodeBySlot($connection, $key);
if ($node !== null) {
Context::set(Constants::TRACE_REDIS_SERVER_ADDRESS, $node['host']);
Context::set(Constants::TRACE_REDIS_SERVER_PORT, $node['port']);
}
}
}
});
}

private function getClusterNodeBySlot(RedisCluster $rc, string $key)
{
$slot = $rc->cluster('CLUSTER', 'KEYSLOT', $key);
$slots = ($this->slotNodeCache[$rc] ??= $rc->cluster('CLUSTER', 'SLOTS')); // @phpstan-ignore-line

foreach ($slots as $range) {
[$start, $end, $master] = $range;
if ($slot >= $start && $slot <= $end) {
// $master = [host, port, nodeId]
return [
'host' => $master[0],
'port' => $master[1],
'nodeId' => $master[2] ?? null,
];
}
}

return null;
}
}