|
| 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 OpenTelemetry\API\Instrumentation\CachedInstrumentation; |
| 13 | +use OpenTelemetry\API\Trace\SpanKind; |
| 14 | +use OpenTelemetry\Contrib\Instrumentation\Laravel\Watchers\Watcher; |
| 15 | +use OpenTelemetry\SemConv\TraceAttributes; |
| 16 | +use OpenTelemetry\SemConv\TraceAttributeValues; |
| 17 | +use Throwable; |
| 18 | + |
| 19 | +/** |
| 20 | + * Watch the Redis Command event |
| 21 | + * |
| 22 | + * Call facade `Redis::enableEvents()` before using this watcher |
| 23 | + */ |
| 24 | +class RedisCommandWatcher extends Watcher |
| 25 | +{ |
| 26 | + public function __construct( |
| 27 | + private CachedInstrumentation $instrumentation, |
| 28 | + ) { |
| 29 | + } |
| 30 | + |
| 31 | + /** @psalm-suppress UndefinedInterfaceMethod */ |
| 32 | + public function register(Application $app): void |
| 33 | + { |
| 34 | + /** @phan-suppress-next-line PhanTypeArraySuspicious */ |
| 35 | + $app['events']->listen(CommandExecuted::class, [$this, 'recordRedisCommand']); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Record a Redis command. |
| 40 | + */ |
| 41 | + /** @psalm-suppress UndefinedThisPropertyFetch */ |
| 42 | + public function recordRedisCommand(CommandExecuted $event): void |
| 43 | + { |
| 44 | + $nowInNs = (int) (microtime(true) * 1E9); |
| 45 | + |
| 46 | + $operationName = strtoupper($event->command); |
| 47 | + |
| 48 | + /** @psalm-suppress ArgumentTypeCoercion */ |
| 49 | + $span = $this->instrumentation->tracer() |
| 50 | + ->spanBuilder($operationName) |
| 51 | + ->setSpanKind(SpanKind::KIND_CLIENT) |
| 52 | + ->setStartTimestamp($this->calculateQueryStartTime($nowInNs, $event->time)) |
| 53 | + ->startSpan(); |
| 54 | + |
| 55 | + // See https://opentelemetry.io/docs/specs/semconv/database/redis/ |
| 56 | + $attributes = [ |
| 57 | + TraceAttributes::DB_SYSTEM => TraceAttributeValues::DB_SYSTEM_REDIS, |
| 58 | + TraceAttributes::DB_NAME => $this->fetchDbIndex($event->connection), |
| 59 | + TraceAttributes::DB_OPERATION => $operationName, |
| 60 | + TraceAttributes::DB_STATEMENT => Serializer::serializeCommand($event->command, $event->parameters), |
| 61 | + TraceAttributes::SERVER_ADDRESS => $this->fetchDbHost($event->connection), |
| 62 | + ]; |
| 63 | + |
| 64 | + /** @psalm-suppress PossiblyInvalidArgument */ |
| 65 | + $span->setAttributes($attributes); |
| 66 | + $span->end($nowInNs); |
| 67 | + } |
| 68 | + |
| 69 | + private function calculateQueryStartTime(int $nowInNs, float $queryTimeMs): int |
| 70 | + { |
| 71 | + return (int) ($nowInNs - ($queryTimeMs * 1E6)); |
| 72 | + } |
| 73 | + |
| 74 | + private function fetchDbIndex(Connection $connection): ?int |
| 75 | + { |
| 76 | + try { |
| 77 | + if ($connection instanceof PhpRedisConnection) { |
| 78 | + return $connection->client()->getDbNum(); |
| 79 | + } elseif ($connection instanceof PredisConnection) { |
| 80 | + /** @psalm-suppress PossiblyUndefinedMethod */ |
| 81 | + return $connection->client()->getConnection()->getParameters()->database; |
| 82 | + } |
| 83 | + |
| 84 | + return null; |
| 85 | + } catch (Throwable $e) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private function fetchDbHost(Connection $connection): ?string |
| 91 | + { |
| 92 | + try { |
| 93 | + if ($connection instanceof PhpRedisConnection) { |
| 94 | + return $connection->client()->getHost(); |
| 95 | + } elseif ($connection instanceof PredisConnection) { |
| 96 | + /** @psalm-suppress PossiblyUndefinedMethod */ |
| 97 | + return $connection->client()->getConnection()->getParameters()->host; |
| 98 | + } |
| 99 | + |
| 100 | + return null; |
| 101 | + } catch (Throwable $e) { |
| 102 | + return null; |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments