Skip to content

Commit

Permalink
Added support for predis client. Fix #23
Browse files Browse the repository at this point in the history
  • Loading branch information
cappuc committed Apr 22, 2024
1 parent 06adcae commit 6523aaf
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan": "^1.10.51",
"phpstan/phpstan-deprecation-rules": "^1.1",
"predis/predis": "^2.2",
"spatie/invade": "^2.0",
"spatie/test-time": "^1.3",
"spatie/valuestore": "^1.3",
Expand Down
23 changes: 20 additions & 3 deletions src/Instrumentation/RedisInstrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,33 @@ public function recordCommand(CommandExecuted $event): void
if ($span->isRecording()) {
$span->setAttribute(TraceAttributes::DB_SYSTEM, 'redis')
->setAttribute(TraceAttributes::DB_STATEMENT, $this->formatCommand($event->command, $event->parameters))
->setAttribute(TraceAttributes::SERVER_ADDRESS, $event->connection->client()->getHost());
->setAttribute(TraceAttributes::SERVER_ADDRESS, $this->resolveRedisAddress($event->connection->client()));
}

$span->end();
}

protected function resolveRedisAddress(mixed $client): ?string
{
if ($client instanceof \Redis) {
return $client->getHost() ?: null;
}

if ($client instanceof \Predis\Client) {
$connection = $client->getConnection();

return $connection instanceof \Predis\Connection\NodeConnectionInterface
? ($connection->getParameters()->host ?? null)
: null;
}

return null;
}

/**
* Format the given Redis command.
*/
private function formatCommand(string $command, array $parameters): string
protected function formatCommand(string $command, array $parameters): string
{
$parameters = collect($parameters)->map(function ($parameter) {
if (is_array($parameter)) {
Expand All @@ -63,7 +80,7 @@ private function formatCommand(string $command, array $parameters): string
return sprintf('%s %s', $command, $parameters);
}

private function registerRedisEvents(mixed $redis): void
protected function registerRedisEvents(mixed $redis): void
{
if ($redis instanceof RedisManager) {
foreach ((array) $redis->connections() as $connection) {
Expand Down
9 changes: 7 additions & 2 deletions tests/Instrumentation/RedisInstrumentationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Trace\ImmutableSpan;

it('can watch a redis call', function () {
it('can watch a redis call', function (string $client) {
config()->set('database.redis.client', $client);

\Illuminate\Support\Facades\Redis::connection('default')->get('test');

$span = getRecordedSpans()->last();
Expand All @@ -23,4 +25,7 @@

expect($span->getEndEpochNanos() - $span->getStartEpochNanos())
->toBeGreaterThan(0);
});
})->with([
'predis',
'phpredis',
]);

0 comments on commit 6523aaf

Please sign in to comment.