|
| 1 | +""" |
| 2 | +Create spans from Django middleware invocations |
| 3 | +""" |
| 4 | + |
| 5 | +from functools import wraps |
| 6 | + |
| 7 | +from django import VERSION as DJANGO_VERSION # type: ignore |
| 8 | + |
| 9 | +from sentry_sdk import Hub |
| 10 | +from sentry_sdk.utils import ContextVar, transaction_from_function |
| 11 | + |
| 12 | +_import_string_should_wrap_middleware = ContextVar( |
| 13 | + "import_string_should_wrap_middleware" |
| 14 | +) |
| 15 | + |
| 16 | +if DJANGO_VERSION < (1, 7): |
| 17 | + import_string_name = "import_by_path" |
| 18 | +else: |
| 19 | + import_string_name = "import_string" |
| 20 | + |
| 21 | + |
| 22 | +def patch_django_middlewares(): |
| 23 | + from django.core.handlers import base |
| 24 | + |
| 25 | + old_import_string = getattr(base, import_string_name) |
| 26 | + |
| 27 | + def sentry_patched_import_string(dotted_path): |
| 28 | + rv = old_import_string(dotted_path) |
| 29 | + |
| 30 | + if _import_string_should_wrap_middleware.get(None): |
| 31 | + rv = _wrap_middleware(rv, dotted_path) |
| 32 | + |
| 33 | + return rv |
| 34 | + |
| 35 | + setattr(base, import_string_name, sentry_patched_import_string) |
| 36 | + |
| 37 | + old_load_middleware = base.BaseHandler.load_middleware |
| 38 | + |
| 39 | + def sentry_patched_load_middleware(self): |
| 40 | + _import_string_should_wrap_middleware.set(True) |
| 41 | + try: |
| 42 | + return old_load_middleware(self) |
| 43 | + finally: |
| 44 | + _import_string_should_wrap_middleware.set(False) |
| 45 | + |
| 46 | + base.BaseHandler.load_middleware = sentry_patched_load_middleware |
| 47 | + |
| 48 | + |
| 49 | +def _wrap_middleware(middleware, middleware_name): |
| 50 | + from sentry_sdk.integrations.django import DjangoIntegration |
| 51 | + |
| 52 | + def _get_wrapped_method(old_method): |
| 53 | + @wraps(old_method) |
| 54 | + def sentry_wrapped_method(*args, **kwargs): |
| 55 | + hub = Hub.current |
| 56 | + integration = hub.get_integration(DjangoIntegration) |
| 57 | + if integration is None or not integration.middleware_spans: |
| 58 | + return old_method(*args, **kwargs) |
| 59 | + |
| 60 | + function_name = transaction_from_function(old_method) |
| 61 | + |
| 62 | + description = middleware_name |
| 63 | + function_basename = getattr(old_method, "__name__", None) |
| 64 | + if function_basename: |
| 65 | + description = "{}.{}".format(description, function_basename) |
| 66 | + |
| 67 | + with hub.start_span( |
| 68 | + op="django.middleware", description=description |
| 69 | + ) as span: |
| 70 | + span.set_tag("django.function_name", function_name) |
| 71 | + span.set_tag("django.middleware_name", middleware_name) |
| 72 | + return old_method(*args, **kwargs) |
| 73 | + |
| 74 | + return sentry_wrapped_method |
| 75 | + |
| 76 | + class SentryWrappingMiddleware(object): |
| 77 | + def __init__(self, *args, **kwargs): |
| 78 | + self._inner = middleware(*args, **kwargs) |
| 79 | + self._call_method = None |
| 80 | + |
| 81 | + # We need correct behavior for `hasattr()`, which we can only determine |
| 82 | + # when we have an instance of the middleware we're wrapping. |
| 83 | + def __getattr__(self, method_name): |
| 84 | + if method_name not in ( |
| 85 | + "process_request", |
| 86 | + "process_view", |
| 87 | + "process_template_response", |
| 88 | + "process_response", |
| 89 | + "process_exception", |
| 90 | + ): |
| 91 | + raise AttributeError() |
| 92 | + |
| 93 | + old_method = getattr(self._inner, method_name) |
| 94 | + rv = _get_wrapped_method(old_method) |
| 95 | + self.__dict__[method_name] = rv |
| 96 | + return rv |
| 97 | + |
| 98 | + def __call__(self, *args, **kwargs): |
| 99 | + if self._call_method is None: |
| 100 | + self._call_method = _get_wrapped_method(self._inner.__call__) |
| 101 | + return self._call_method(*args, **kwargs) |
| 102 | + |
| 103 | + if hasattr(middleware, "__name__"): |
| 104 | + SentryWrappingMiddleware.__name__ = middleware.__name__ |
| 105 | + |
| 106 | + return SentryWrappingMiddleware |
0 commit comments