|
8 | 8 | try: |
9 | 9 | import redis |
10 | 10 |
|
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 |
12 | 14 |
|
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: |
14 | 41 | 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]) |
83 | 45 |
|
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) |
85 | 85 | 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") |
88 | 90 | except ImportError: |
89 | 91 | pass |
0 commit comments