|
| 1 | +import sys |
| 2 | + |
| 3 | +import aioredis |
| 4 | + |
| 5 | +from ddtrace import config |
| 6 | +from ddtrace.internal.utils.wrappers import unwrap as _u |
| 7 | +from ddtrace.pin import Pin |
| 8 | +from ddtrace.vendor.wrapt import wrap_function_wrapper as _w |
| 9 | + |
| 10 | +from .. import trace_utils |
| 11 | +from ...constants import ANALYTICS_SAMPLE_RATE_KEY |
| 12 | +from ...constants import SPAN_MEASURED_KEY |
| 13 | +from ...ext import SpanTypes |
| 14 | +from ...ext import net |
| 15 | +from ...ext import redis as redisx |
| 16 | +from ..redis.util import _trace_redis_cmd |
| 17 | +from ..redis.util import _trace_redis_execute_pipeline |
| 18 | +from ..redis.util import format_command_args |
| 19 | + |
| 20 | + |
| 21 | +try: |
| 22 | + from aioredis.commands.transaction import _RedisBuffer |
| 23 | +except ImportError: |
| 24 | + _RedisBuffer = None |
| 25 | + |
| 26 | +config._add("aioredis", dict(_default_service="redis")) |
| 27 | + |
| 28 | +aioredis_version_str = getattr(aioredis, "__version__", "0.0.0") |
| 29 | +aioredis_version = tuple([int(i) for i in aioredis_version_str.split(".")]) |
| 30 | + |
| 31 | + |
| 32 | +def patch(): |
| 33 | + if getattr(aioredis, "_datadog_patch", False): |
| 34 | + return |
| 35 | + setattr(aioredis, "_datadog_patch", True) |
| 36 | + pin = Pin() |
| 37 | + if aioredis_version >= (2, 0): |
| 38 | + _w("aioredis.client", "Redis.execute_command", traced_execute_command) |
| 39 | + _w("aioredis.client", "Redis.pipeline", traced_pipeline) |
| 40 | + _w("aioredis.client", "Pipeline.execute", traced_execute_pipeline) |
| 41 | + pin.onto(aioredis.client.Redis) |
| 42 | + else: |
| 43 | + _w("aioredis", "Redis.execute", traced_13_execute_command) |
| 44 | + _w("aioredis", "Redis.pipeline", traced_13_pipeline) |
| 45 | + _w("aioredis.commands.transaction", "Pipeline.execute", traced_13_execute_pipeline) |
| 46 | + pin.onto(aioredis.Redis) |
| 47 | + |
| 48 | + |
| 49 | +def unpatch(): |
| 50 | + if not getattr(aioredis, "_datadog_patch", False): |
| 51 | + return |
| 52 | + |
| 53 | + setattr(aioredis, "_datadog_patch", False) |
| 54 | + if aioredis_version >= (2, 0): |
| 55 | + _u(aioredis.client.Redis, "execute_command") |
| 56 | + _u(aioredis.client.Redis, "pipeline") |
| 57 | + _u(aioredis.client.Pipeline, "execute") |
| 58 | + else: |
| 59 | + _u(aioredis.Redis, "execute") |
| 60 | + _u(aioredis.Redis, "pipeline") |
| 61 | + _u(aioredis.commands.transaction.Pipeline, "execute") |
| 62 | + |
| 63 | + |
| 64 | +async def traced_execute_command(func, instance, args, kwargs): |
| 65 | + pin = Pin.get_from(instance) |
| 66 | + if not pin or not pin.enabled(): |
| 67 | + return await func(*args, **kwargs) |
| 68 | + |
| 69 | + decoded_args = [arg.decode() if isinstance(arg, bytes) else arg for arg in args] |
| 70 | + with _trace_redis_cmd(pin, config.aioredis, instance, decoded_args): |
| 71 | + return await func(*args, **kwargs) |
| 72 | + |
| 73 | + |
| 74 | +async def traced_pipeline(func, instance, args, kwargs): |
| 75 | + pipeline = await func(*args, **kwargs) |
| 76 | + pin = Pin.get_from(instance) |
| 77 | + if pin: |
| 78 | + pin.onto(pipeline) |
| 79 | + return pipeline |
| 80 | + |
| 81 | + |
| 82 | +async def traced_execute_pipeline(func, instance, args, kwargs): |
| 83 | + pin = Pin.get_from(instance) |
| 84 | + if not pin or not pin.enabled(): |
| 85 | + return await func(*args, **kwargs) |
| 86 | + |
| 87 | + cmds = [format_command_args(c) for c, _ in instance.command_stack] |
| 88 | + resource = "\n".join(cmds) |
| 89 | + with _trace_redis_execute_pipeline(pin, config.aioredis, resource, instance): |
| 90 | + return await func(*args, **kwargs) |
| 91 | + |
| 92 | + |
| 93 | +def traced_13_pipeline(func, instance, args, kwargs): |
| 94 | + pipeline = func(*args, **kwargs) |
| 95 | + pin = Pin.get_from(instance) |
| 96 | + if pin: |
| 97 | + pin.onto(pipeline) |
| 98 | + return pipeline |
| 99 | + |
| 100 | + |
| 101 | +def traced_13_execute_command(func, instance, args, kwargs): |
| 102 | + # If we have a _RedisBuffer then we are in a pipeline |
| 103 | + if isinstance(instance.connection, _RedisBuffer): |
| 104 | + return func(*args, **kwargs) |
| 105 | + |
| 106 | + pin = Pin.get_from(instance) |
| 107 | + if not pin or not pin.enabled(): |
| 108 | + return func(*args, **kwargs) |
| 109 | + |
| 110 | + decoded_args = [arg.decode() if isinstance(arg, bytes) else arg for arg in args] |
| 111 | + # Don't activate the span since this operation is performed as a future which concludes sometime later on in |
| 112 | + # execution so subsequent operations in the stack are not necessarily semantically related |
| 113 | + # (we don't want this span to be the parent of all other spans created before the future is resolved) |
| 114 | + span = pin.tracer.start_span( |
| 115 | + redisx.CMD, service=trace_utils.ext_service(pin, config.aioredis), span_type=SpanTypes.REDIS, activate=False |
| 116 | + ) |
| 117 | + |
| 118 | + span.set_tag(SPAN_MEASURED_KEY) |
| 119 | + query = format_command_args(decoded_args) |
| 120 | + span.resource = query |
| 121 | + span.set_tag(redisx.RAWCMD, query) |
| 122 | + if pin.tags: |
| 123 | + span.set_tags(pin.tags) |
| 124 | + |
| 125 | + span.set_tags( |
| 126 | + { |
| 127 | + net.TARGET_HOST: instance.address[0], |
| 128 | + net.TARGET_PORT: instance.address[1], |
| 129 | + redisx.DB: instance.db or 0, |
| 130 | + } |
| 131 | + ) |
| 132 | + span.set_metric(redisx.ARGS_LEN, len(decoded_args)) |
| 133 | + # set analytics sample rate if enabled |
| 134 | + span.set_tag(ANALYTICS_SAMPLE_RATE_KEY, config.aioredis.get_analytics_sample_rate()) |
| 135 | + |
| 136 | + def _finish_span(future): |
| 137 | + try: |
| 138 | + # Accessing the result will raise an exception if: |
| 139 | + # - The future was cancelled |
| 140 | + # - There was an error executing the future (`future.exception()`) |
| 141 | + # - The future is in an invalid state |
| 142 | + future.result() |
| 143 | + except Exception: |
| 144 | + span.set_exc_info(*sys.exc_info()) |
| 145 | + finally: |
| 146 | + span.finish() |
| 147 | + |
| 148 | + task = func(*args, **kwargs) |
| 149 | + task.add_done_callback(_finish_span) |
| 150 | + return task |
| 151 | + |
| 152 | + |
| 153 | +async def traced_13_execute_pipeline(func, instance, args, kwargs): |
| 154 | + pin = Pin.get_from(instance) |
| 155 | + if not pin or not pin.enabled(): |
| 156 | + return await func(*args, **kwargs) |
| 157 | + |
| 158 | + cmds = [] |
| 159 | + for _, cmd, cmd_args, _ in instance._pipeline: |
| 160 | + parts = [cmd.decode() if isinstance(cmd, bytes) else cmd] |
| 161 | + parts.extend(cmd_args) |
| 162 | + cmds.append(format_command_args(parts)) |
| 163 | + resource = "\n".join(cmds) |
| 164 | + with pin.tracer.trace( |
| 165 | + redisx.CMD, |
| 166 | + resource=resource, |
| 167 | + service=trace_utils.ext_service(pin, config.aioredis), |
| 168 | + span_type=SpanTypes.REDIS, |
| 169 | + ) as span: |
| 170 | + |
| 171 | + span.set_tags( |
| 172 | + { |
| 173 | + net.TARGET_HOST: instance._pool_or_conn.address[0], |
| 174 | + net.TARGET_PORT: instance._pool_or_conn.address[1], |
| 175 | + redisx.DB: instance._pool_or_conn.db or 0, |
| 176 | + } |
| 177 | + ) |
| 178 | + |
| 179 | + span.set_tag(SPAN_MEASURED_KEY) |
| 180 | + span.set_tag(redisx.RAWCMD, resource) |
| 181 | + span.set_metric(redisx.PIPELINE_LEN, len(instance._pipeline)) |
| 182 | + # set analytics sample rate if enabled |
| 183 | + span.set_tag(ANALYTICS_SAMPLE_RATE_KEY, config.aioredis.get_analytics_sample_rate()) |
| 184 | + |
| 185 | + return await func(*args, **kwargs) |
0 commit comments