asgi.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. """
  2. An ASGI middleware.
  3. Based on Tom Christie's `sentry-asgi <https://github.com/encode/sentry-asgi>`.
  4. """
  5. import asyncio
  6. import inspect
  7. from copy import deepcopy
  8. from functools import partial
  9. import sentry_sdk
  10. from sentry_sdk.api import continue_trace
  11. from sentry_sdk.consts import OP
  12. from sentry_sdk.integrations._asgi_common import (
  13. _get_headers,
  14. _get_request_data,
  15. _get_url,
  16. )
  17. from sentry_sdk.integrations._wsgi_common import (
  18. DEFAULT_HTTP_METHODS_TO_CAPTURE,
  19. nullcontext,
  20. )
  21. from sentry_sdk.sessions import track_session
  22. from sentry_sdk.tracing import (
  23. SOURCE_FOR_STYLE,
  24. TransactionSource,
  25. )
  26. from sentry_sdk.utils import (
  27. ContextVar,
  28. event_from_exception,
  29. HAS_REAL_CONTEXTVARS,
  30. CONTEXTVARS_ERROR_MESSAGE,
  31. logger,
  32. transaction_from_function,
  33. _get_installed_modules,
  34. )
  35. from sentry_sdk.tracing import Transaction
  36. from typing import TYPE_CHECKING
  37. if TYPE_CHECKING:
  38. from typing import Any
  39. from typing import Dict
  40. from typing import Optional
  41. from typing import Tuple
  42. from sentry_sdk._types import Event, Hint
  43. _asgi_middleware_applied = ContextVar("sentry_asgi_middleware_applied")
  44. _DEFAULT_TRANSACTION_NAME = "generic ASGI request"
  45. TRANSACTION_STYLE_VALUES = ("endpoint", "url")
  46. def _capture_exception(exc, mechanism_type="asgi"):
  47. # type: (Any, str) -> None
  48. event, hint = event_from_exception(
  49. exc,
  50. client_options=sentry_sdk.get_client().options,
  51. mechanism={"type": mechanism_type, "handled": False},
  52. )
  53. sentry_sdk.capture_event(event, hint=hint)
  54. def _looks_like_asgi3(app):
  55. # type: (Any) -> bool
  56. """
  57. Try to figure out if an application object supports ASGI3.
  58. This is how uvicorn figures out the application version as well.
  59. """
  60. if inspect.isclass(app):
  61. return hasattr(app, "__await__")
  62. elif inspect.isfunction(app):
  63. return asyncio.iscoroutinefunction(app)
  64. else:
  65. call = getattr(app, "__call__", None) # noqa
  66. return asyncio.iscoroutinefunction(call)
  67. class SentryAsgiMiddleware:
  68. __slots__ = (
  69. "app",
  70. "__call__",
  71. "transaction_style",
  72. "mechanism_type",
  73. "span_origin",
  74. "http_methods_to_capture",
  75. )
  76. def __init__(
  77. self,
  78. app, # type: Any
  79. unsafe_context_data=False, # type: bool
  80. transaction_style="endpoint", # type: str
  81. mechanism_type="asgi", # type: str
  82. span_origin="manual", # type: str
  83. http_methods_to_capture=DEFAULT_HTTP_METHODS_TO_CAPTURE, # type: Tuple[str, ...]
  84. asgi_version=None, # type: Optional[int]
  85. ):
  86. # type: (...) -> None
  87. """
  88. Instrument an ASGI application with Sentry. Provides HTTP/websocket
  89. data to sent events and basic handling for exceptions bubbling up
  90. through the middleware.
  91. :param unsafe_context_data: Disable errors when a proper contextvars installation could not be found. We do not recommend changing this from the default.
  92. """
  93. if not unsafe_context_data and not HAS_REAL_CONTEXTVARS:
  94. # We better have contextvars or we're going to leak state between
  95. # requests.
  96. raise RuntimeError(
  97. "The ASGI middleware for Sentry requires Python 3.7+ "
  98. "or the aiocontextvars package." + CONTEXTVARS_ERROR_MESSAGE
  99. )
  100. if transaction_style not in TRANSACTION_STYLE_VALUES:
  101. raise ValueError(
  102. "Invalid value for transaction_style: %s (must be in %s)"
  103. % (transaction_style, TRANSACTION_STYLE_VALUES)
  104. )
  105. asgi_middleware_while_using_starlette_or_fastapi = (
  106. mechanism_type == "asgi" and "starlette" in _get_installed_modules()
  107. )
  108. if asgi_middleware_while_using_starlette_or_fastapi:
  109. logger.warning(
  110. "The Sentry Python SDK can now automatically support ASGI frameworks like Starlette and FastAPI. "
  111. "Please remove 'SentryAsgiMiddleware' from your project. "
  112. "See https://docs.sentry.io/platforms/python/guides/asgi/ for more information."
  113. )
  114. self.transaction_style = transaction_style
  115. self.mechanism_type = mechanism_type
  116. self.span_origin = span_origin
  117. self.app = app
  118. self.http_methods_to_capture = http_methods_to_capture
  119. if asgi_version is None:
  120. if _looks_like_asgi3(app):
  121. asgi_version = 3
  122. else:
  123. asgi_version = 2
  124. if asgi_version == 3:
  125. self.__call__ = self._run_asgi3
  126. elif asgi_version == 2:
  127. self.__call__ = self._run_asgi2 # type: ignore
  128. def _capture_lifespan_exception(self, exc):
  129. # type: (Exception) -> None
  130. """Capture exceptions raise in application lifespan handlers.
  131. The separate function is needed to support overriding in derived integrations that use different catching mechanisms.
  132. """
  133. return _capture_exception(exc=exc, mechanism_type=self.mechanism_type)
  134. def _capture_request_exception(self, exc):
  135. # type: (Exception) -> None
  136. """Capture exceptions raised in incoming request handlers.
  137. The separate function is needed to support overriding in derived integrations that use different catching mechanisms.
  138. """
  139. return _capture_exception(exc=exc, mechanism_type=self.mechanism_type)
  140. def _run_asgi2(self, scope):
  141. # type: (Any) -> Any
  142. async def inner(receive, send):
  143. # type: (Any, Any) -> Any
  144. return await self._run_app(scope, receive, send, asgi_version=2)
  145. return inner
  146. async def _run_asgi3(self, scope, receive, send):
  147. # type: (Any, Any, Any) -> Any
  148. return await self._run_app(scope, receive, send, asgi_version=3)
  149. async def _run_app(self, scope, receive, send, asgi_version):
  150. # type: (Any, Any, Any, int) -> Any
  151. is_recursive_asgi_middleware = _asgi_middleware_applied.get(False)
  152. is_lifespan = scope["type"] == "lifespan"
  153. if is_recursive_asgi_middleware or is_lifespan:
  154. try:
  155. if asgi_version == 2:
  156. return await self.app(scope)(receive, send)
  157. else:
  158. return await self.app(scope, receive, send)
  159. except Exception as exc:
  160. self._capture_lifespan_exception(exc)
  161. raise exc from None
  162. _asgi_middleware_applied.set(True)
  163. try:
  164. with sentry_sdk.isolation_scope() as sentry_scope:
  165. with track_session(sentry_scope, session_mode="request"):
  166. sentry_scope.clear_breadcrumbs()
  167. sentry_scope._name = "asgi"
  168. processor = partial(self.event_processor, asgi_scope=scope)
  169. sentry_scope.add_event_processor(processor)
  170. ty = scope["type"]
  171. (
  172. transaction_name,
  173. transaction_source,
  174. ) = self._get_transaction_name_and_source(
  175. self.transaction_style,
  176. scope,
  177. )
  178. method = scope.get("method", "").upper()
  179. transaction = None
  180. if ty in ("http", "websocket"):
  181. if ty == "websocket" or method in self.http_methods_to_capture:
  182. transaction = continue_trace(
  183. _get_headers(scope),
  184. op="{}.server".format(ty),
  185. name=transaction_name,
  186. source=transaction_source,
  187. origin=self.span_origin,
  188. )
  189. else:
  190. transaction = Transaction(
  191. op=OP.HTTP_SERVER,
  192. name=transaction_name,
  193. source=transaction_source,
  194. origin=self.span_origin,
  195. )
  196. if transaction:
  197. transaction.set_tag("asgi.type", ty)
  198. transaction_context = (
  199. sentry_sdk.start_transaction(
  200. transaction,
  201. custom_sampling_context={"asgi_scope": scope},
  202. )
  203. if transaction is not None
  204. else nullcontext()
  205. )
  206. with transaction_context:
  207. try:
  208. async def _sentry_wrapped_send(event):
  209. # type: (Dict[str, Any]) -> Any
  210. if transaction is not None:
  211. is_http_response = (
  212. event.get("type") == "http.response.start"
  213. and "status" in event
  214. )
  215. if is_http_response:
  216. transaction.set_http_status(event["status"])
  217. return await send(event)
  218. if asgi_version == 2:
  219. return await self.app(scope)(
  220. receive, _sentry_wrapped_send
  221. )
  222. else:
  223. return await self.app(
  224. scope, receive, _sentry_wrapped_send
  225. )
  226. except Exception as exc:
  227. self._capture_request_exception(exc)
  228. raise exc from None
  229. finally:
  230. _asgi_middleware_applied.set(False)
  231. def event_processor(self, event, hint, asgi_scope):
  232. # type: (Event, Hint, Any) -> Optional[Event]
  233. request_data = event.get("request", {})
  234. request_data.update(_get_request_data(asgi_scope))
  235. event["request"] = deepcopy(request_data)
  236. # Only set transaction name if not already set by Starlette or FastAPI (or other frameworks)
  237. transaction = event.get("transaction")
  238. transaction_source = (event.get("transaction_info") or {}).get("source")
  239. already_set = (
  240. transaction is not None
  241. and transaction != _DEFAULT_TRANSACTION_NAME
  242. and transaction_source
  243. in [
  244. TransactionSource.COMPONENT,
  245. TransactionSource.ROUTE,
  246. TransactionSource.CUSTOM,
  247. ]
  248. )
  249. if not already_set:
  250. name, source = self._get_transaction_name_and_source(
  251. self.transaction_style, asgi_scope
  252. )
  253. event["transaction"] = name
  254. event["transaction_info"] = {"source": source}
  255. return event
  256. # Helper functions.
  257. #
  258. # Note: Those functions are not public API. If you want to mutate request
  259. # data to your liking it's recommended to use the `before_send` callback
  260. # for that.
  261. def _get_transaction_name_and_source(self, transaction_style, asgi_scope):
  262. # type: (SentryAsgiMiddleware, str, Any) -> Tuple[str, str]
  263. name = None
  264. source = SOURCE_FOR_STYLE[transaction_style]
  265. ty = asgi_scope.get("type")
  266. if transaction_style == "endpoint":
  267. endpoint = asgi_scope.get("endpoint")
  268. # Webframeworks like Starlette mutate the ASGI env once routing is
  269. # done, which is sometime after the request has started. If we have
  270. # an endpoint, overwrite our generic transaction name.
  271. if endpoint:
  272. name = transaction_from_function(endpoint) or ""
  273. else:
  274. name = _get_url(asgi_scope, "http" if ty == "http" else "ws", host=None)
  275. source = TransactionSource.URL
  276. elif transaction_style == "url":
  277. # FastAPI includes the route object in the scope to let Sentry extract the
  278. # path from it for the transaction name
  279. route = asgi_scope.get("route")
  280. if route:
  281. path = getattr(route, "path", None)
  282. if path is not None:
  283. name = path
  284. else:
  285. name = _get_url(asgi_scope, "http" if ty == "http" else "ws", host=None)
  286. source = TransactionSource.URL
  287. if name is None:
  288. name = _DEFAULT_TRANSACTION_NAME
  289. source = TransactionSource.ROUTE
  290. return name, source
  291. return name, source