Skip to content

Commit 8bd4384

Browse files
committed
Instrument async Redis clients similarly to sync clients
1 parent f2628b4 commit 8bd4384

File tree

1 file changed

+25
-0
lines changed
  • instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis

1 file changed

+25
-0
lines changed

instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/__init__.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ def response_hook(span, instance, response):
102102
typing.Callable[[Span, redis.connection.Connection, Any], None]
103103
]
104104

105+
_REDIS_ASYNCIO_VERSION = (4, 2, 0)
106+
if redis.VERSION >= _REDIS_ASYNCIO_VERSION:
107+
import redis.asyncio
108+
105109

106110
def _set_connection_attributes(span, conn):
107111
if not span.is_recording():
@@ -176,6 +180,22 @@ def _traced_execute_pipeline(func, instance, args, kwargs):
176180
f"{pipeline_class}.immediate_execute_command",
177181
_traced_execute_command,
178182
)
183+
if redis.VERSION >= _REDIS_ASYNCIO_VERSION:
184+
wrap_function_wrapper(
185+
"redis.asyncio",
186+
f"{redis_class}.execute_command",
187+
_traced_execute_command,
188+
)
189+
wrap_function_wrapper(
190+
"redis.asyncio.client",
191+
f"{pipeline_class}.execute",
192+
_traced_execute_pipeline,
193+
)
194+
wrap_function_wrapper(
195+
"redis.asyncio.client",
196+
f"{pipeline_class}.immediate_execute_command",
197+
_traced_execute_command,
198+
)
179199

180200

181201
class RedisInstrumentor(BaseInstrumentor):
@@ -222,3 +242,8 @@ def _uninstrument(self, **kwargs):
222242
unwrap(redis.Redis, "pipeline")
223243
unwrap(redis.client.Pipeline, "execute")
224244
unwrap(redis.client.Pipeline, "immediate_execute_command")
245+
if redis.VERSION >= _REDIS_ASYNCIO_VERSION:
246+
unwrap(redis.asyncio.Redis, "execute_command")
247+
unwrap(redis.asyncio.Redis, "pipeline")
248+
unwrap(redis.asyncio.client.Pipeline, "execute")
249+
unwrap(redis.asyncio.client.Pipeline, "immediate_execute_command")

0 commit comments

Comments
 (0)