CRC-Mismatch / opentelemetry-auto-redis

Apache License 2.0
2 stars 5 forks source link

Predis host/port extraction generates warning #5

Open enginvardar opened 1 month ago

enginvardar commented 1 month ago

As per documentation of predis connection parameter might be a list of URIs. https://github.com/predis/predis?tab=readme-ov-file#connecting-to-redis

When a list of URIs is passed this line generates the warning below.

PHP Warning: Predis\Client::__construct(): OpenTelemetry: pre hook threw exception, class=Predis\Client function=__construct message=array_key_exists(): Argument #2 ($array) must be of type array, string given in /srv/public/Unknown on line 0

To reproduce the issue; create Predis\Client with a list of URIs

$redis =  new \Predis\Client(['tcp://redis:6379'], []);

Parsing the string with the parameter parser predis provides solves the issue:

diff --git a/src/PredisInstrumentation.php b/src/PredisInstrumentation.php
index 8fdbbe9..80e4c8f 100644
--- a/src/PredisInstrumentation.php
+++ b/src/PredisInstrumentation.php
@@ -16,6 +16,7 @@
 use OpenTelemetry\API\Trace\SpanKind;
 use OpenTelemetry\API\Trace\StatusCode;
 use OpenTelemetry\Context\Context;
+use Predis\Connection\Parameters;
 use function OpenTelemetry\Instrumentation\hook;
 use OpenTelemetry\SemConv\TraceAttributes;
 use Predis\Command\CommandInterface;
@@ -55,6 +56,11 @@ public static function register(): void
                 if (array_is_list($host)) {
                     $host = $host[0];
                 }
+
+                if (is_string($host)) {
+                    $host = Parameters::parse($host);
+                }
+
                 if ($class === \Predis\Client::class) {
                     $builder->setAttribute(TraceAttributes::SERVER_ADDRESS, $host['host'] ?? $host['path'] ?? 'unknown')
                         ->setAttribute(TraceAttributes::NETWORK_TRANSPORT, $host['scheme'] ?? 'unknown');
GrzegorzDrozd commented 1 month ago

Fixed with https://github.com/CRC-Mismatch/opentelemetry-auto-redis/pull/6 :)