Skip to content

Commit 419d36a

Browse files
authored
Redis: Add support for versions > 3.0.0 (#184)
1 parent 3c81c09 commit 419d36a

File tree

2 files changed

+76
-74
lines changed

2 files changed

+76
-74
lines changed

instana/instrumentation/redis.py

Lines changed: 75 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -8,82 +8,84 @@
88
try:
99
import redis
1010

11-
if ((redis.VERSION >= (2, 10, 6)) and (redis.VERSION < (3, 0, 0))):
11+
def collect_tags(span, instance, args, kwargs):
12+
try:
13+
ckw = instance.connection_pool.connection_kwargs
1214

13-
def collect_tags(span, instance, args, kwargs):
15+
span.set_tag("driver", "redis-py")
16+
17+
host = ckw.get('host', None)
18+
port = ckw.get('port', '6379')
19+
db = ckw.get('db', None)
20+
21+
if host is not None:
22+
url = "redis://%s:%s" % (host, port)
23+
if db is not None:
24+
url = url + "/%s" % db
25+
span.set_tag('connection', url)
26+
27+
except:
28+
logger.debug("redis.collect_tags non-fatal error", exc_info=True)
29+
finally:
30+
return span
31+
32+
33+
def execute_command_with_instana(wrapped, instance, args, kwargs):
34+
parent_span = tracer.active_span
35+
36+
# If we're not tracing, just return
37+
if parent_span is None or parent_span.operation_name == "redis":
38+
return wrapped(*args, **kwargs)
39+
40+
with tracer.start_active_span("redis", child_of=parent_span) as scope:
1441
try:
15-
ckw = instance.connection_pool.connection_kwargs
16-
17-
span.set_tag("driver", "redis-py")
18-
19-
host = ckw.get('host', None)
20-
port = ckw.get('port', '6379')
21-
db = ckw.get('db', None)
22-
23-
if host is not None:
24-
url = "redis://%s:%s" % (host, port)
25-
if db is not None:
26-
url = url + "/%s" % db
27-
span.set_tag('connection', url)
28-
29-
except:
30-
logger.debug("redis.collect_tags non-fatal error", exc_info=True)
31-
finally:
32-
return span
33-
34-
@wrapt.patch_function_wrapper('redis.client','StrictRedis.execute_command')
35-
def execute_command_with_instana(wrapped, instance, args, kwargs):
36-
parent_span = tracer.active_span
37-
38-
# If we're not tracing, just return
39-
if parent_span is None or parent_span.operation_name == "redis":
40-
return wrapped(*args, **kwargs)
41-
42-
with tracer.start_active_span("redis", child_of=parent_span) as scope:
43-
try:
44-
collect_tags(scope.span, instance, args, kwargs)
45-
if (len(args) > 0):
46-
scope.span.set_tag("command", args[0])
47-
48-
rv = wrapped(*args, **kwargs)
49-
except Exception as e:
50-
scope.span.log_exception(e)
51-
raise
52-
else:
53-
return rv
54-
55-
@wrapt.patch_function_wrapper('redis.client','BasePipeline.execute')
56-
def execute_with_instana(wrapped, instance, args, kwargs):
57-
parent_span = tracer.active_span
58-
59-
# If we're not tracing, just return
60-
if parent_span is None or parent_span.operation_name == "redis":
61-
return wrapped(*args, **kwargs)
62-
63-
with tracer.start_active_span("redis", child_of=parent_span) as scope:
64-
try:
65-
collect_tags(scope.span, instance, args, kwargs)
66-
scope.span.set_tag("command", 'PIPELINE')
67-
68-
pipe_cmds = []
69-
for e in instance.command_stack:
70-
pipe_cmds.append(e[0][0])
71-
scope.span.set_tag("subCommands", pipe_cmds)
72-
except Exception as e:
73-
# If anything breaks during K/V collection, just log a debug message
74-
logger.debug("Error collecting pipeline commands", exc_info=True)
75-
76-
try:
77-
rv = wrapped(*args, **kwargs)
78-
except Exception as e:
79-
scope.span.log_exception(e)
80-
raise
81-
else:
82-
return rv
42+
collect_tags(scope.span, instance, args, kwargs)
43+
if (len(args) > 0):
44+
scope.span.set_tag("command", args[0])
8345

84-
logger.debug("Instrumenting redis")
46+
rv = wrapped(*args, **kwargs)
47+
except Exception as e:
48+
scope.span.log_exception(e)
49+
raise
50+
else:
51+
return rv
52+
53+
54+
def execute_with_instana(wrapped, instance, args, kwargs):
55+
parent_span = tracer.active_span
56+
57+
# If we're not tracing, just return
58+
if parent_span is None or parent_span.operation_name == "redis":
59+
return wrapped(*args, **kwargs)
60+
61+
with tracer.start_active_span("redis", child_of=parent_span) as scope:
62+
try:
63+
collect_tags(scope.span, instance, args, kwargs)
64+
scope.span.set_tag("command", 'PIPELINE')
65+
66+
pipe_cmds = []
67+
for e in instance.command_stack:
68+
pipe_cmds.append(e[0][0])
69+
scope.span.set_tag("subCommands", pipe_cmds)
70+
except Exception as e:
71+
# If anything breaks during K/V collection, just log a debug message
72+
logger.debug("Error collecting pipeline commands", exc_info=True)
73+
74+
try:
75+
rv = wrapped(*args, **kwargs)
76+
except Exception as e:
77+
scope.span.log_exception(e)
78+
raise
79+
else:
80+
return rv
81+
82+
if redis.VERSION < (3,0,0):
83+
wrapt.wrap_function_wrapper('redis.client', 'BasePipeline.execute', execute_with_instana)
84+
wrapt.wrap_function_wrapper('redis.client', 'StrictRedis.execute_command', execute_command_with_instana)
8585
else:
86-
logger.debug("redis <= 2.10.5 >=3.0.0 not supported.")
87-
logger.debug(" --> https://docs.instana.io/ecosystem/python/supported-versions/#tracing")
86+
wrapt.wrap_function_wrapper('redis.client', 'Pipeline.execute', execute_with_instana)
87+
wrapt.wrap_function_wrapper('redis.client', 'Redis.execute_command', execute_command_with_instana)
88+
89+
logger.debug("Instrumenting redis")
8890
except ImportError:
8991
pass

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def check_setuptools():
8181
'pyOpenSSL>=16.1.0;python_version<="2.7"',
8282
'pytest>=3.0.1',
8383
'psycopg2>=2.7.1',
84-
'redis<3.0.0',
84+
'redis>3.0.0',
8585
'requests>=2.17.1',
8686
'sqlalchemy>=1.1.15',
8787
'spyne>=2.9,<=2.12.14',

0 commit comments

Comments
 (0)