|
1 | 1 | from __future__ import absolute_import |
2 | 2 |
|
3 | | -import opentracing |
4 | | -import opentracing.ext.tags as ext |
5 | 3 | import wrapt |
6 | 4 |
|
7 | 5 | from ..log import logger |
|
10 | 8 | try: |
11 | 9 | import redis |
12 | 10 |
|
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: |
56 | 22 |
|
57 | 23 | 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) |
62 | 31 | 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)") |
78 | 80 | except ImportError: |
79 | 81 | pass |
0 commit comments