Skip to content

Commit 16d560d

Browse files
authored
Version limit redis until 3.0 is supported (#108)
1 parent e0085cb commit 16d560d

File tree

1 file changed

+66
-64
lines changed

1 file changed

+66
-64
lines changed

instana/instrumentation/redis.py

Lines changed: 66 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from __future__ import absolute_import
22

3-
import opentracing
4-
import opentracing.ext.tags as ext
53
import wrapt
64

75
from ..log import logger
@@ -10,70 +8,74 @@
108
try:
119
import redis
1210

13-
@wrapt.patch_function_wrapper('redis.client','StrictRedis.execute_command')
14-
def execute_command_with_instana(wrapped, instance, args, kwargs):
15-
parent_span = tracer.active_span
16-
17-
# If we're not tracing, just return
18-
if parent_span is None:
19-
return wrapped(*args, **kwargs)
20-
21-
with tracer.start_active_span("redis", child_of=parent_span) as scope:
22-
23-
try:
24-
ckw = instance.connection_pool.connection_kwargs
25-
url = "redis://%s:%d/%d" % (ckw['host'], ckw['port'], ckw['db'])
26-
scope.span.set_tag("connection", url)
27-
scope.span.set_tag("driver", "redis-py")
28-
scope.span.set_tag("command", args[0])
29-
30-
rv = wrapped(*args, **kwargs)
31-
except Exception as e:
32-
scope.span.set_tag("redis.error", str(e))
33-
scope.span.set_tag("error", True)
34-
ec = scope.span.tags.get('ec', 0)
35-
scope.span.set_tag("ec", ec+1)
36-
raise
37-
else:
38-
return rv
39-
40-
@wrapt.patch_function_wrapper('redis.client','BasePipeline.execute')
41-
def execute_with_instana(wrapped, instance, args, kwargs):
42-
parent_span = tracer.active_span
43-
44-
# If we're not tracing, just return
45-
if parent_span is None:
46-
return wrapped(*args, **kwargs)
47-
48-
with tracer.start_active_span("redis", child_of=parent_span) as scope:
49-
50-
try:
51-
ckw = instance.connection_pool.connection_kwargs
52-
url = "redis://%s:%d/%d" % (ckw['host'], ckw['port'], ckw['db'])
53-
scope.span.set_tag("connection", url)
54-
scope.span.set_tag("driver", "redis-py")
55-
scope.span.set_tag("command", 'PIPELINE')
11+
if redis.VERSION < (3, 0, 0):
12+
13+
@wrapt.patch_function_wrapper('redis.client','StrictRedis.execute_command')
14+
def execute_command_with_instana(wrapped, instance, args, kwargs):
15+
parent_span = tracer.active_span
16+
17+
# If we're not tracing, just return
18+
if parent_span is None:
19+
return wrapped(*args, **kwargs)
20+
21+
with tracer.start_active_span("redis", child_of=parent_span) as scope:
5622

5723
try:
58-
pipe_cmds = []
59-
for e in instance.command_stack:
60-
pipe_cmds.append(e[0][0])
61-
scope.span.set_tag("subCommands", pipe_cmds)
24+
ckw = instance.connection_pool.connection_kwargs
25+
url = "redis://%s:%d/%d" % (ckw['host'], ckw['port'], ckw['db'])
26+
scope.span.set_tag("connection", url)
27+
scope.span.set_tag("driver", "redis-py")
28+
scope.span.set_tag("command", args[0])
29+
30+
rv = wrapped(*args, **kwargs)
6231
except Exception as e:
63-
# If anything breaks during cmd collection, just log a
64-
# debug message
65-
logger.debug("Error collecting pipeline commands")
66-
67-
rv = wrapped(*args, **kwargs)
68-
except Exception as e:
69-
scope.span.set_tag("redis.error", str(e))
70-
scope.span.set_tag("error", True)
71-
ec = scope.span.tags.get('ec', 0)
72-
scope.span.set_tag("ec", ec+1)
73-
raise
74-
else:
75-
return rv
76-
77-
logger.debug("Instrumenting redis")
32+
scope.span.set_tag("redis.error", str(e))
33+
scope.span.set_tag("error", True)
34+
ec = scope.span.tags.get('ec', 0)
35+
scope.span.set_tag("ec", ec+1)
36+
raise
37+
else:
38+
return rv
39+
40+
@wrapt.patch_function_wrapper('redis.client','BasePipeline.execute')
41+
def execute_with_instana(wrapped, instance, args, kwargs):
42+
parent_span = tracer.active_span
43+
44+
# If we're not tracing, just return
45+
if parent_span is None:
46+
return wrapped(*args, **kwargs)
47+
48+
with tracer.start_active_span("redis", child_of=parent_span) as scope:
49+
50+
try:
51+
ckw = instance.connection_pool.connection_kwargs
52+
url = "redis://%s:%d/%d" % (ckw['host'], ckw['port'], ckw['db'])
53+
scope.span.set_tag("connection", url)
54+
scope.span.set_tag("driver", "redis-py")
55+
scope.span.set_tag("command", 'PIPELINE')
56+
57+
try:
58+
pipe_cmds = []
59+
for e in instance.command_stack:
60+
pipe_cmds.append(e[0][0])
61+
scope.span.set_tag("subCommands", pipe_cmds)
62+
except Exception as e:
63+
# If anything breaks during cmd collection, just log a
64+
# debug message
65+
logger.debug("Error collecting pipeline commands")
66+
67+
rv = wrapped(*args, **kwargs)
68+
except Exception as e:
69+
scope.span.set_tag("redis.error", str(e))
70+
scope.span.set_tag("error", True)
71+
ec = scope.span.tags.get('ec', 0)
72+
scope.span.set_tag("ec", ec+1)
73+
raise
74+
else:
75+
return rv
76+
77+
logger.debug("Instrumenting redis")
78+
else:
79+
logger.debug("redis >=3.0.0 not supported (yet)")
7880
except ImportError:
7981
pass

0 commit comments

Comments
 (0)