Skip to content

Commit 999e913

Browse files
committed
fixed compatibility issues with redis-py 3.0 (#336)
closes #336
1 parent 282cd54 commit 999e913

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+
## v4.0.1
4+
[Check the diff](https://github.com/elastic/apm-agent-python/compare/v4.0.0...v4.0.1)
5+
6+
* fixed an issue with instrumenting redis-py 3.0+
7+
38
## v4.0.0
49
[Check the diff](https://github.com/elastic/apm-agent-python/compare/v3.0.2...v4.0.0)
510

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
@@ -34,11 +34,7 @@ def test_pipeline(instrument, elasticapm_client, redis_conn):
3434
transactions = elasticapm_client.events[TRANSACTION]
3535
spans = elasticapm_client.spans_for_transaction(transactions[0])
3636

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

4440
assert spans[1]["name"] == "test_pipeline"
@@ -64,11 +60,7 @@ def test_rq_patches_redis(instrument, elasticapm_client, redis_conn):
6460
transactions = elasticapm_client.events[TRANSACTION]
6561
spans = elasticapm_client.spans_for_transaction(transactions[0])
6662

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

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

0 commit comments

Comments
 (0)