|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace OpenTelemetry\Contrib\Instrumentation\Laravel\Watchers\RedisCommand; |
| 6 | + |
| 7 | +use Illuminate\Contracts\Foundation\Application; |
| 8 | +use Illuminate\Redis\Connections\Connection; |
| 9 | +use Illuminate\Redis\Connections\PhpRedisConnection; |
| 10 | +use Illuminate\Redis\Connections\PredisConnection; |
| 11 | +use Illuminate\Redis\Events\CommandExecuted; |
| 12 | +use Illuminate\Support\Str; |
| 13 | +use OpenTelemetry\API\Instrumentation\CachedInstrumentation; |
| 14 | +use OpenTelemetry\API\Trace\SpanKind; |
| 15 | +use OpenTelemetry\Contrib\Instrumentation\Laravel\Watchers\Watcher; |
| 16 | +use OpenTelemetry\SemConv\TraceAttributes; |
| 17 | +use OpenTelemetry\SemConv\TraceAttributeValues; |
| 18 | +use RangeException; |
| 19 | +use RuntimeException; |
| 20 | + |
| 21 | +/** |
| 22 | + * Watch the Redis Command event |
| 23 | + * |
| 24 | + * Call facade `Redis::enableEvents()` before using this watcher |
| 25 | + */ |
| 26 | +class RedisCommandWatcher extends Watcher |
| 27 | +{ |
| 28 | + public function __construct( |
| 29 | + private CachedInstrumentation $instrumentation, |
| 30 | + ) { |
| 31 | + } |
| 32 | + |
| 33 | + /** @psalm-suppress UndefinedInterfaceMethod */ |
| 34 | + public function register(Application $app): void |
| 35 | + { |
| 36 | + /** @phan-suppress-next-line PhanTypeArraySuspicious */ |
| 37 | + $app['events']->listen(CommandExecuted::class, [$this, 'recordRedisCommand']); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Record a query. |
| 42 | + */ |
| 43 | + /** @psalm-suppress UndefinedThisPropertyFetch */ |
| 44 | + public function recordRedisCommand(CommandExecuted $event): void |
| 45 | + { |
| 46 | + $nowInNs = (int) (microtime(true) * 1E9); |
| 47 | + |
| 48 | + $operationName = Str::upper($event->command); |
| 49 | + |
| 50 | + /** @psalm-suppress ArgumentTypeCoercion */ |
| 51 | + $span = $this->instrumentation->tracer() |
| 52 | + ->spanBuilder($operationName) |
| 53 | + ->setSpanKind(SpanKind::KIND_CLIENT) |
| 54 | + ->setStartTimestamp($this->calculateQueryStartTime($nowInNs, $event->time)) |
| 55 | + ->startSpan(); |
| 56 | + |
| 57 | + // See https://opentelemetry.io/docs/specs/semconv/database/redis/ |
| 58 | + $attributes = [ |
| 59 | + TraceAttributes::DB_SYSTEM => TraceAttributeValues::DB_SYSTEM_REDIS, |
| 60 | + TraceAttributes::DB_NAME => $this->fetchDbIndex($event->connection), |
| 61 | + TraceAttributes::DB_OPERATION => $operationName, |
| 62 | + TraceAttributes::DB_QUERY_TEXT => Serializer::serializeCommand($event->command, $event->parameters), |
| 63 | + TraceAttributes::SERVER_ADDRESS => $this->fetchDbHost($event->connection), |
| 64 | + ]; |
| 65 | + |
| 66 | + /** @psalm-suppress PossiblyInvalidArgument */ |
| 67 | + $span->setAttributes($attributes); |
| 68 | + $span->end($nowInNs); |
| 69 | + } |
| 70 | + |
| 71 | + private function calculateQueryStartTime(int $nowInNs, float $queryTimeMs): int |
| 72 | + { |
| 73 | + return (int) ($nowInNs - ($queryTimeMs * 1E6)); |
| 74 | + } |
| 75 | + |
| 76 | + private function fetchDbIndex(Connection $connection): int |
| 77 | + { |
| 78 | + if ($connection instanceof PhpRedisConnection) { |
| 79 | + $index = $connection->client()->getDbNum(); |
| 80 | + |
| 81 | + if ($index === false) { |
| 82 | + throw new RuntimeException('Cannot fetch database index.'); |
| 83 | + } |
| 84 | + |
| 85 | + return $index; |
| 86 | + } elseif ($connection instanceof PredisConnection) { |
| 87 | + /** @psalm-suppress PossiblyUndefinedMethod */ |
| 88 | + $index = $connection->client()->getConnection()->getParameters()->database; |
| 89 | + |
| 90 | + if (is_int($index)) { |
| 91 | + throw new RuntimeException('Cannot fetch database index.'); |
| 92 | + } |
| 93 | + |
| 94 | + return $index; |
| 95 | + } |
| 96 | + |
| 97 | + throw new RangeException('Unknown Redis connection instance: ' . get_class($connection)); |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | + private function fetchDbHost(Connection $connection): string |
| 102 | + { |
| 103 | + if ($connection instanceof PhpRedisConnection) { |
| 104 | + $host = $connection->client()->getHost(); |
| 105 | + |
| 106 | + if ($host === false) { |
| 107 | + throw new RuntimeException('Cannot fetch database host.'); |
| 108 | + } |
| 109 | + |
| 110 | + return $host; |
| 111 | + } elseif ($connection instanceof PredisConnection) { |
| 112 | + /** @psalm-suppress PossiblyUndefinedMethod */ |
| 113 | + $host = $connection->client()->getConnection()->getParameters()->host; |
| 114 | + |
| 115 | + if (is_int($host)) { |
| 116 | + throw new RuntimeException('Cannot fetch database index.'); |
| 117 | + } |
| 118 | + |
| 119 | + return $host; |
| 120 | + } |
| 121 | + |
| 122 | + throw new RangeException('Unknown Redis connection instance: ' . get_class($connection)); |
| 123 | + |
| 124 | + } |
| 125 | +} |
0 commit comments