Skip to content

Commit 51363fa

Browse files
committed
properly await in asyncio redis instrumentation
1 parent 7898206 commit 51363fa

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

elasticapm/instrumentation/packages/asyncio/redis_asyncio.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,49 +31,49 @@
3131
from __future__ import absolute_import
3232

3333
from elasticapm.contrib.asyncio.traces import async_capture_span
34-
from elasticapm.instrumentation.packages.base import AbstractInstrumentedModule
34+
from elasticapm.instrumentation.packages.asyncio.base import AsyncAbstractInstrumentedModule
3535

3636

37-
class RedisAsyncioInstrumentation(AbstractInstrumentedModule):
37+
class RedisAsyncioInstrumentation(AsyncAbstractInstrumentedModule):
3838
name = "redis"
3939

4040
instrument_list = [
4141
("redis.asyncio.client", "Redis.execute_command"),
4242
("redis.asyncio.client", "PubSub.execute_command"),
4343
]
4444

45-
def call(self, module, method, wrapped, instance, args, kwargs):
45+
async def call(self, module, method, wrapped, instance, args, kwargs):
4646
if len(args) > 0:
4747
wrapped_name = args[0]
4848
if isinstance(wrapped_name, bytes):
4949
wrapped_name = wrapped_name.decode()
5050
else:
5151
wrapped_name = self.get_wrapped_name(wrapped, instance, method)
5252

53-
with async_capture_span(
53+
async with async_capture_span(
5454
wrapped_name, span_type="db", span_subtype="redis", span_action="query", leaf=True
5555
) as span:
5656
if span.context is not None:
5757
span.context["destination"] = _get_destination_info(instance)
5858

59-
return wrapped(*args, **kwargs)
59+
return await wrapped(*args, **kwargs)
6060

6161

62-
class RedisPipelineInstrumentation(AbstractInstrumentedModule):
62+
class RedisPipelineInstrumentation(AsyncAbstractInstrumentedModule):
6363
name = "redis"
6464

6565
instrument_list = [("redis.asyncio.client", "Pipeline.execute")]
6666

67-
def call(self, module, method, wrapped, instance, args, kwargs):
67+
async def call(self, module, method, wrapped, instance, args, kwargs):
6868
wrapped_name = self.get_wrapped_name(wrapped, instance, method)
6969

70-
with async_capture_span(
70+
async with async_capture_span(
7171
wrapped_name, span_type="db", span_subtype="redis", span_action="query", leaf=True
7272
) as span:
7373
if span.context is not None:
7474
span.context["destination"] = _get_destination_info(instance)
7575

76-
return wrapped(*args, **kwargs)
76+
return await wrapped(*args, **kwargs)
7777

7878

7979
def _get_destination_info(connection):

tests/instrumentation/asyncio_tests/redis_asyncio_tests.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ async def test_ping(instrument, elasticapm_client, redis_async_conn):
6868
transaction = elasticapm_client.events[TRANSACTION][0]
6969
span = elasticapm_client.spans_for_transaction(transaction)[0]
7070
assert span["name"] == "PING"
71+
assert span["duration"] > 0.2 # sanity test to ensure we measure the actual call
7172

7273

7374
@pytest.mark.integrationtest
@@ -92,6 +93,7 @@ async def test_pipeline(instrument, elasticapm_client, redis_async_conn):
9293
"port": int(os.environ.get("REDIS_PORT", 6379)),
9394
"service": {"name": "", "resource": "redis", "type": ""},
9495
}
96+
assert spans[0]["duration"] > 0.2 # sanity test to ensure we measure the actual call
9597

9698
assert spans[1]["name"] == "test_pipeline"
9799
assert spans[1]["type"] == "test"
@@ -123,6 +125,7 @@ async def test_redis_client(instrument, elasticapm_client, redis_async_conn):
123125
"port": int(os.environ.get("REDIS_PORT", 6379)),
124126
"service": {"name": "", "resource": "redis", "type": ""},
125127
}
128+
assert spans[0]["duration"] > 0.2 # sanity test to ensure we measure the actual call
126129

127130
assert spans[1]["name"] == "RPUSH"
128131
assert spans[1]["type"] == "db"
@@ -133,6 +136,7 @@ async def test_redis_client(instrument, elasticapm_client, redis_async_conn):
133136
"port": int(os.environ.get("REDIS_PORT", 6379)),
134137
"service": {"name": "", "resource": "redis", "type": ""},
135138
}
139+
assert spans[1]["duration"] > 0.2 # sanity test to ensure we measure the actual call
136140

137141
assert spans[2]["name"] == "test_redis_client"
138142
assert spans[2]["type"] == "test"
@@ -169,6 +173,7 @@ async def test_publish_subscribe_async(instrument, elasticapm_client, redis_asyn
169173
"port": int(os.environ.get("REDIS_PORT", 6379)),
170174
"service": {"name": "", "resource": "redis", "type": ""},
171175
}
176+
assert spans[0]["duration"] > 0.2 # sanity test to ensure we measure the actual call
172177

173178
assert spans[1]["name"] == "SUBSCRIBE"
174179
assert spans[1]["type"] == "db"
@@ -179,6 +184,7 @@ async def test_publish_subscribe_async(instrument, elasticapm_client, redis_asyn
179184
"port": int(os.environ.get("REDIS_PORT", 6379)),
180185
"service": {"name": "", "resource": "redis", "type": ""},
181186
}
187+
assert spans[1]["duration"] > 0.2 # sanity test to ensure we measure the actual call
182188

183189
assert spans[2]["name"] == "test_publish_subscribe"
184190
assert spans[2]["type"] == "test"

0 commit comments

Comments
 (0)