|
| 1 | +import logging |
| 2 | + |
| 3 | +from functools import wraps |
| 4 | + |
| 5 | +from django.conf import settings as django_settings |
| 6 | + |
| 7 | +from .conf import settings, import_from_string |
| 8 | +from .utils import quantize_key_values, _resource_from_cache_prefix |
| 9 | + |
| 10 | + |
| 11 | +log = logging.getLogger(__name__) |
| 12 | + |
| 13 | +# code instrumentation |
| 14 | +DATADOG_NAMESPACE = '__datadog_original_{method}' |
| 15 | +TRACED_METHODS = [ |
| 16 | + 'get', |
| 17 | + 'set', |
| 18 | + 'add', |
| 19 | + 'delete', |
| 20 | + 'incr', |
| 21 | + 'decr', |
| 22 | + 'get_many', |
| 23 | + 'set_many', |
| 24 | + 'delete_many', |
| 25 | +] |
| 26 | + |
| 27 | +# standard tags |
| 28 | +TYPE = 'cache' |
| 29 | +CACHE_BACKEND = 'django.cache.backend' |
| 30 | +CACHE_COMMAND_KEY = 'django.cache.key' |
| 31 | + |
| 32 | + |
| 33 | +def patch_cache(tracer): |
| 34 | + """ |
| 35 | + Function that patches the inner cache system. Because the cache backend |
| 36 | + can have different implementations and connectors, this function must |
| 37 | + handle all possible interactions with the Django cache. What follows |
| 38 | + is currently traced: |
| 39 | + * in-memory cache |
| 40 | + * the cache client wrapper that could use any of the common |
| 41 | + Django supported cache servers (Redis, Memcached, Database, Custom) |
| 42 | + """ |
| 43 | + # discover used cache backends |
| 44 | + cache_backends = [cache['BACKEND'] for cache in django_settings.CACHES.values()] |
| 45 | + |
| 46 | + def _trace_operation(fn, method_name): |
| 47 | + """ |
| 48 | + Return a wrapped function that traces a cache operation |
| 49 | + """ |
| 50 | + @wraps(fn) |
| 51 | + def wrapped(self, *args, **kwargs): |
| 52 | + # get the original function method |
| 53 | + method = getattr(self, DATADOG_NAMESPACE.format(method=method_name)) |
| 54 | + with tracer.trace('django.cache', |
| 55 | + span_type=TYPE, service=settings.DEFAULT_SERVICE) as span: |
| 56 | + # update the resource name and tag the cache backend |
| 57 | + span.resource = _resource_from_cache_prefix(method_name, self) |
| 58 | + cache_backend = '{}.{}'.format(self.__module__, self.__class__.__name__) |
| 59 | + span.set_tag(CACHE_BACKEND, cache_backend) |
| 60 | + |
| 61 | + if args: |
| 62 | + keys = quantize_key_values(args[0]) |
| 63 | + span.set_tag(CACHE_COMMAND_KEY, keys) |
| 64 | + |
| 65 | + return method(*args, **kwargs) |
| 66 | + return wrapped |
| 67 | + |
| 68 | + def _wrap_method(cls, method_name): |
| 69 | + """ |
| 70 | + For the given class, wraps the method name with a traced operation |
| 71 | + so that the original method is executed, while the span is properly |
| 72 | + created |
| 73 | + """ |
| 74 | + # check if the backend owns the given bounded method |
| 75 | + if not hasattr(cls, method_name): |
| 76 | + return |
| 77 | + |
| 78 | + # prevent patching each backend's method more than once |
| 79 | + if hasattr(cls, DATADOG_NAMESPACE.format(method=method_name)): |
| 80 | + log.debug('{} already traced'.format(method_name)) |
| 81 | + else: |
| 82 | + method = getattr(cls, method_name) |
| 83 | + setattr(cls, DATADOG_NAMESPACE.format(method=method_name), method) |
| 84 | + setattr(cls, method_name, _trace_operation(method, method_name)) |
| 85 | + |
| 86 | + # trace all backends |
| 87 | + for cache_module in cache_backends: |
| 88 | + cache = import_from_string(cache_module, cache_module) |
| 89 | + |
| 90 | + for method in TRACED_METHODS: |
| 91 | + _wrap_method(cache, method) |
0 commit comments