Skip to content

Commit da7db8d

Browse files
committed
fixed compatibility issues with redis-py 3.0
backport of #336 to the 3.x branch
1 parent d415405 commit da7db8d

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## v3.0.4
4+
[Check the diff](https://github.com/elastic/apm-agent-python/compare/v3.0.3...v3.0.4)
5+
6+
* fixed an issue with instrumenting redis-py 3.0+
7+
38
## v3.0.3
49
[Check the diff](https://github.com/elastic/apm-agent-python/compare/v3.0.2...v3.0.3)
510
* fixed an issue when logging messages that are not strings (#295, #312)

elasticapm/instrumentation/packages/redis.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
1+
from __future__ import absolute_import
2+
13
from elasticapm.instrumentation.packages.base import AbstractInstrumentedModule
24
from elasticapm.traces import capture_span
35

46

5-
class RedisInstrumentation(AbstractInstrumentedModule):
7+
class Redis3CheckMixin(object):
8+
instrument_list_3 = []
9+
instrument_list = []
10+
11+
def get_instrument_list(self):
12+
try:
13+
from redis import VERSION
14+
15+
if VERSION[0] >= 3:
16+
return self.instrument_list_3
17+
return self.instrument_list
18+
except ImportError:
19+
return self.instrument_list
20+
21+
22+
class RedisInstrumentation(Redis3CheckMixin, AbstractInstrumentedModule):
623
name = "redis"
724

25+
# no need to instrument StrictRedis in redis-py >= 3.0
26+
instrument_list_3 = [("redis.client", "Redis.execute_command")]
827
instrument_list = [("redis.client", "Redis.execute_command"), ("redis.client", "StrictRedis.execute_command")]
928

1029
def call(self, module, method, wrapped, instance, args, kwargs):
@@ -17,9 +36,11 @@ def call(self, module, method, wrapped, instance, args, kwargs):
1736
return wrapped(*args, **kwargs)
1837

1938

20-
class RedisPipelineInstrumentation(AbstractInstrumentedModule):
39+
class RedisPipelineInstrumentation(Redis3CheckMixin, AbstractInstrumentedModule):
2140
name = "redis"
2241

42+
# BasePipeline has been renamed to Pipeline in redis-py 3
43+
instrument_list_3 = [("redis.client", "Pipeline.execute")]
2344
instrument_list = [("redis.client", "BasePipeline.execute")]
2445

2546
def call(self, module, method, wrapped, instance, args, kwargs):

tests/instrumentation/redis_tests.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,7 @@ def test_pipeline(instrument, elasticapm_client, redis_conn):
3333
transactions = elasticapm_client.transaction_store.get_all()
3434
spans = transactions[0]["spans"]
3535

36-
expected_signatures = {"test_pipeline", "StrictPipeline.execute"}
37-
38-
assert {t["name"] for t in spans} == expected_signatures
39-
40-
assert spans[0]["name"] == "StrictPipeline.execute"
36+
assert spans[0]["name"] in ("StrictPipeline.execute", "Pipeline.execute")
4137
assert spans[0]["type"] == "cache.redis"
4238

4339
assert spans[1]["name"] == "test_pipeline"
@@ -63,11 +59,7 @@ def test_rq_patches_redis(instrument, elasticapm_client, redis_conn):
6359
transactions = elasticapm_client.transaction_store.get_all()
6460
spans = transactions[0]["spans"]
6561

66-
expected_signatures = {"test_pipeline", "StrictPipeline.execute"}
67-
68-
assert {t["name"] for t in spans} == expected_signatures
69-
70-
assert spans[0]["name"] == "StrictPipeline.execute"
62+
assert spans[0]["name"] in ("StrictPipeline.execute", "Pipeline.execute")
7163
assert spans[0]["type"] == "cache.redis"
7264

7365
assert spans[1]["name"] == "test_pipeline"

0 commit comments

Comments
 (0)