|
| 1 | +import os |
| 2 | + |
| 3 | +import wrapt |
| 4 | +import yaaredis |
| 5 | + |
| 6 | +from ddtrace import config |
| 7 | +from ddtrace._trace.utils_redis import _instrument_redis_cmd |
| 8 | +from ddtrace._trace.utils_redis import _instrument_redis_execute_pipeline |
| 9 | +from ddtrace.contrib.internal.redis_utils import _run_redis_command_async |
| 10 | +from ddtrace.internal.schema import schematize_service_name |
| 11 | +from ddtrace.internal.utils.deprecations import DDTraceDeprecationWarning |
| 12 | +from ddtrace.internal.utils.formats import CMD_MAX_LEN |
| 13 | +from ddtrace.internal.utils.formats import asbool |
| 14 | +from ddtrace.internal.utils.formats import stringify_cache_args |
| 15 | +from ddtrace.internal.utils.wrappers import unwrap |
| 16 | +from ddtrace.trace import Pin |
| 17 | +from ddtrace.vendor.debtcollector import deprecate |
| 18 | + |
| 19 | + |
| 20 | +config._add( |
| 21 | + "yaaredis", |
| 22 | + dict( |
| 23 | + _default_service=schematize_service_name("redis"), |
| 24 | + cmd_max_length=int(os.getenv("DD_YAAREDIS_CMD_MAX_LENGTH", CMD_MAX_LEN)), |
| 25 | + resource_only_command=asbool(os.getenv("DD_REDIS_RESOURCE_ONLY_COMMAND", True)), |
| 26 | + ), |
| 27 | +) |
| 28 | + |
| 29 | + |
| 30 | +def get_version(): |
| 31 | + # type: () -> str |
| 32 | + return getattr(yaaredis, "__version__", "") |
| 33 | + |
| 34 | + |
| 35 | +def patch(): |
| 36 | + """Patch the instrumented methods""" |
| 37 | + deprecate( |
| 38 | + prefix="The yaaredis module is deprecated.", |
| 39 | + message="The yaaredis module is deprecated and will be deleted.", |
| 40 | + category=DDTraceDeprecationWarning, |
| 41 | + removal_version="3.0.0", |
| 42 | + ) |
| 43 | + |
| 44 | + if getattr(yaaredis, "_datadog_patch", False): |
| 45 | + return |
| 46 | + yaaredis._datadog_patch = True |
| 47 | + |
| 48 | + _w = wrapt.wrap_function_wrapper |
| 49 | + |
| 50 | + _w("yaaredis.client", "StrictRedis.execute_command", traced_execute_command) |
| 51 | + _w("yaaredis.client", "StrictRedis.pipeline", traced_pipeline) |
| 52 | + _w("yaaredis.pipeline", "StrictPipeline.execute", traced_execute_pipeline) |
| 53 | + _w("yaaredis.pipeline", "StrictPipeline.immediate_execute_command", traced_execute_command) |
| 54 | + Pin().onto(yaaredis.StrictRedis) |
| 55 | + |
| 56 | + |
| 57 | +def unpatch(): |
| 58 | + if getattr(yaaredis, "_datadog_patch", False): |
| 59 | + yaaredis._datadog_patch = False |
| 60 | + |
| 61 | + unwrap(yaaredis.client.StrictRedis, "execute_command") |
| 62 | + unwrap(yaaredis.client.StrictRedis, "pipeline") |
| 63 | + unwrap(yaaredis.pipeline.StrictPipeline, "execute") |
| 64 | + unwrap(yaaredis.pipeline.StrictPipeline, "immediate_execute_command") |
| 65 | + |
| 66 | + |
| 67 | +async def traced_execute_command(func, instance, args, kwargs): |
| 68 | + pin = Pin.get_from(instance) |
| 69 | + if not pin or not pin.enabled(): |
| 70 | + return await func(*args, **kwargs) |
| 71 | + |
| 72 | + with _instrument_redis_cmd(pin, config.yaaredis, instance, args) as ctx: |
| 73 | + return await _run_redis_command_async(ctx=ctx, func=func, args=args, kwargs=kwargs) |
| 74 | + |
| 75 | + |
| 76 | +async def traced_pipeline(func, instance, args, kwargs): |
| 77 | + pipeline = await func(*args, **kwargs) |
| 78 | + pin = Pin.get_from(instance) |
| 79 | + if pin: |
| 80 | + pin.onto(pipeline) |
| 81 | + return pipeline |
| 82 | + |
| 83 | + |
| 84 | +async def traced_execute_pipeline(func, instance, args, kwargs): |
| 85 | + pin = Pin.get_from(instance) |
| 86 | + if not pin or not pin.enabled(): |
| 87 | + return await func(*args, **kwargs) |
| 88 | + |
| 89 | + cmds = [stringify_cache_args(c, cmd_max_len=config.yaaredis.cmd_max_length) for c, _ in instance.command_stack] |
| 90 | + with _instrument_redis_execute_pipeline(pin, config.yaaredis, cmds, instance): |
| 91 | + return await func(*args, **kwargs) |
0 commit comments