asgi.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. """
  2. Instrumentation for Django 3.0
  3. Since this file contains `async def` it is conditionally imported in
  4. `sentry_sdk.integrations.django` (depending on the existence of
  5. `django.core.handlers.asgi`.
  6. """
  7. import asyncio
  8. import functools
  9. import inspect
  10. from django.core.handlers.wsgi import WSGIRequest
  11. import sentry_sdk
  12. from sentry_sdk.consts import OP
  13. from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
  14. from sentry_sdk.scope import should_send_default_pii
  15. from sentry_sdk.utils import (
  16. capture_internal_exceptions,
  17. ensure_integration_enabled,
  18. )
  19. from typing import TYPE_CHECKING
  20. if TYPE_CHECKING:
  21. from typing import Any, Callable, Union, TypeVar
  22. from django.core.handlers.asgi import ASGIRequest
  23. from django.http.response import HttpResponse
  24. from sentry_sdk._types import Event, EventProcessor
  25. _F = TypeVar("_F", bound=Callable[..., Any])
  26. # Python 3.12 deprecates asyncio.iscoroutinefunction() as an alias for
  27. # inspect.iscoroutinefunction(), whilst also removing the _is_coroutine marker.
  28. # The latter is replaced with the inspect.markcoroutinefunction decorator.
  29. # Until 3.12 is the minimum supported Python version, provide a shim.
  30. # This was copied from https://github.com/django/asgiref/blob/main/asgiref/sync.py
  31. if hasattr(inspect, "markcoroutinefunction"):
  32. iscoroutinefunction = inspect.iscoroutinefunction
  33. markcoroutinefunction = inspect.markcoroutinefunction
  34. else:
  35. iscoroutinefunction = asyncio.iscoroutinefunction # type: ignore[assignment]
  36. def markcoroutinefunction(func: "_F") -> "_F":
  37. func._is_coroutine = asyncio.coroutines._is_coroutine # type: ignore
  38. return func
  39. def _make_asgi_request_event_processor(request):
  40. # type: (ASGIRequest) -> EventProcessor
  41. def asgi_request_event_processor(event, hint):
  42. # type: (Event, dict[str, Any]) -> Event
  43. # if the request is gone we are fine not logging the data from
  44. # it. This might happen if the processor is pushed away to
  45. # another thread.
  46. from sentry_sdk.integrations.django import (
  47. DjangoRequestExtractor,
  48. _set_user_info,
  49. )
  50. if request is None:
  51. return event
  52. if type(request) == WSGIRequest:
  53. return event
  54. with capture_internal_exceptions():
  55. DjangoRequestExtractor(request).extract_into_event(event)
  56. if should_send_default_pii():
  57. with capture_internal_exceptions():
  58. _set_user_info(request, event)
  59. return event
  60. return asgi_request_event_processor
  61. def patch_django_asgi_handler_impl(cls):
  62. # type: (Any) -> None
  63. from sentry_sdk.integrations.django import DjangoIntegration
  64. old_app = cls.__call__
  65. async def sentry_patched_asgi_handler(self, scope, receive, send):
  66. # type: (Any, Any, Any, Any) -> Any
  67. integration = sentry_sdk.get_client().get_integration(DjangoIntegration)
  68. if integration is None:
  69. return await old_app(self, scope, receive, send)
  70. middleware = SentryAsgiMiddleware(
  71. old_app.__get__(self, cls),
  72. unsafe_context_data=True,
  73. span_origin=DjangoIntegration.origin,
  74. http_methods_to_capture=integration.http_methods_to_capture,
  75. )._run_asgi3
  76. return await middleware(scope, receive, send)
  77. cls.__call__ = sentry_patched_asgi_handler
  78. modern_django_asgi_support = hasattr(cls, "create_request")
  79. if modern_django_asgi_support:
  80. old_create_request = cls.create_request
  81. @ensure_integration_enabled(DjangoIntegration, old_create_request)
  82. def sentry_patched_create_request(self, *args, **kwargs):
  83. # type: (Any, *Any, **Any) -> Any
  84. request, error_response = old_create_request(self, *args, **kwargs)
  85. scope = sentry_sdk.get_isolation_scope()
  86. scope.add_event_processor(_make_asgi_request_event_processor(request))
  87. return request, error_response
  88. cls.create_request = sentry_patched_create_request
  89. def patch_get_response_async(cls, _before_get_response):
  90. # type: (Any, Any) -> None
  91. old_get_response_async = cls.get_response_async
  92. async def sentry_patched_get_response_async(self, request):
  93. # type: (Any, Any) -> Union[HttpResponse, BaseException]
  94. _before_get_response(request)
  95. return await old_get_response_async(self, request)
  96. cls.get_response_async = sentry_patched_get_response_async
  97. def patch_channels_asgi_handler_impl(cls):
  98. # type: (Any) -> None
  99. import channels # type: ignore
  100. from sentry_sdk.integrations.django import DjangoIntegration
  101. if channels.__version__ < "3.0.0":
  102. old_app = cls.__call__
  103. async def sentry_patched_asgi_handler(self, receive, send):
  104. # type: (Any, Any, Any) -> Any
  105. integration = sentry_sdk.get_client().get_integration(DjangoIntegration)
  106. if integration is None:
  107. return await old_app(self, receive, send)
  108. middleware = SentryAsgiMiddleware(
  109. lambda _scope: old_app.__get__(self, cls),
  110. unsafe_context_data=True,
  111. span_origin=DjangoIntegration.origin,
  112. http_methods_to_capture=integration.http_methods_to_capture,
  113. )
  114. return await middleware(self.scope)(receive, send) # type: ignore
  115. cls.__call__ = sentry_patched_asgi_handler
  116. else:
  117. # The ASGI handler in Channels >= 3 has the same signature as
  118. # the Django handler.
  119. patch_django_asgi_handler_impl(cls)
  120. def wrap_async_view(callback):
  121. # type: (Any) -> Any
  122. from sentry_sdk.integrations.django import DjangoIntegration
  123. @functools.wraps(callback)
  124. async def sentry_wrapped_callback(request, *args, **kwargs):
  125. # type: (Any, *Any, **Any) -> Any
  126. current_scope = sentry_sdk.get_current_scope()
  127. if current_scope.transaction is not None:
  128. current_scope.transaction.update_active_thread()
  129. sentry_scope = sentry_sdk.get_isolation_scope()
  130. if sentry_scope.profile is not None:
  131. sentry_scope.profile.update_active_thread_id()
  132. with sentry_sdk.start_span(
  133. op=OP.VIEW_RENDER,
  134. name=request.resolver_match.view_name,
  135. origin=DjangoIntegration.origin,
  136. ):
  137. return await callback(request, *args, **kwargs)
  138. return sentry_wrapped_callback
  139. def _asgi_middleware_mixin_factory(_check_middleware_span):
  140. # type: (Callable[..., Any]) -> Any
  141. """
  142. Mixin class factory that generates a middleware mixin for handling requests
  143. in async mode.
  144. """
  145. class SentryASGIMixin:
  146. if TYPE_CHECKING:
  147. _inner = None
  148. def __init__(self, get_response):
  149. # type: (Callable[..., Any]) -> None
  150. self.get_response = get_response
  151. self._acall_method = None
  152. self._async_check()
  153. def _async_check(self):
  154. # type: () -> None
  155. """
  156. If get_response is a coroutine function, turns us into async mode so
  157. a thread is not consumed during a whole request.
  158. Taken from django.utils.deprecation::MiddlewareMixin._async_check
  159. """
  160. if iscoroutinefunction(self.get_response):
  161. markcoroutinefunction(self)
  162. def async_route_check(self):
  163. # type: () -> bool
  164. """
  165. Function that checks if we are in async mode,
  166. and if we are forwards the handling of requests to __acall__
  167. """
  168. return iscoroutinefunction(self.get_response)
  169. async def __acall__(self, *args, **kwargs):
  170. # type: (*Any, **Any) -> Any
  171. f = self._acall_method
  172. if f is None:
  173. if hasattr(self._inner, "__acall__"):
  174. self._acall_method = f = self._inner.__acall__ # type: ignore
  175. else:
  176. self._acall_method = f = self._inner
  177. middleware_span = _check_middleware_span(old_method=f)
  178. if middleware_span is None:
  179. return await f(*args, **kwargs) # type: ignore
  180. with middleware_span:
  181. return await f(*args, **kwargs) # type: ignore
  182. return SentryASGIMixin