sanic.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. import sys
  2. import weakref
  3. from inspect import isawaitable
  4. from urllib.parse import urlsplit
  5. import sentry_sdk
  6. from sentry_sdk import continue_trace
  7. from sentry_sdk.consts import OP
  8. from sentry_sdk.integrations import _check_minimum_version, Integration, DidNotEnable
  9. from sentry_sdk.integrations._wsgi_common import RequestExtractor, _filter_headers
  10. from sentry_sdk.integrations.logging import ignore_logger
  11. from sentry_sdk.tracing import TransactionSource
  12. from sentry_sdk.utils import (
  13. capture_internal_exceptions,
  14. ensure_integration_enabled,
  15. event_from_exception,
  16. HAS_REAL_CONTEXTVARS,
  17. CONTEXTVARS_ERROR_MESSAGE,
  18. parse_version,
  19. reraise,
  20. )
  21. from typing import TYPE_CHECKING
  22. if TYPE_CHECKING:
  23. from collections.abc import Container
  24. from typing import Any
  25. from typing import Callable
  26. from typing import Optional
  27. from typing import Union
  28. from typing import Dict
  29. from sanic.request import Request, RequestParameters
  30. from sanic.response import BaseHTTPResponse
  31. from sentry_sdk._types import Event, EventProcessor, ExcInfo, Hint
  32. from sanic.router import Route
  33. try:
  34. from sanic import Sanic, __version__ as SANIC_VERSION
  35. from sanic.exceptions import SanicException
  36. from sanic.router import Router
  37. from sanic.handlers import ErrorHandler
  38. except ImportError:
  39. raise DidNotEnable("Sanic not installed")
  40. old_error_handler_lookup = ErrorHandler.lookup
  41. old_handle_request = Sanic.handle_request
  42. old_router_get = Router.get
  43. try:
  44. # This method was introduced in Sanic v21.9
  45. old_startup = Sanic._startup
  46. except AttributeError:
  47. pass
  48. class SanicIntegration(Integration):
  49. identifier = "sanic"
  50. origin = f"auto.http.{identifier}"
  51. version = None
  52. def __init__(
  53. self, unsampled_statuses: "Optional[Container[int]]" = frozenset({404})
  54. ) -> None:
  55. """
  56. The unsampled_statuses parameter can be used to specify for which HTTP statuses the
  57. transactions should not be sent to Sentry. By default, transactions are sent for all
  58. HTTP statuses, except 404. Set unsampled_statuses to None to send transactions for all
  59. HTTP statuses, including 404.
  60. """
  61. self._unsampled_statuses = unsampled_statuses or set()
  62. @staticmethod
  63. def setup_once() -> None:
  64. SanicIntegration.version = parse_version(SANIC_VERSION)
  65. _check_minimum_version(SanicIntegration, SanicIntegration.version)
  66. if not HAS_REAL_CONTEXTVARS:
  67. # We better have contextvars or we're going to leak state between
  68. # requests.
  69. raise DidNotEnable(
  70. "The sanic integration for Sentry requires Python 3.7+ "
  71. " or the aiocontextvars package." + CONTEXTVARS_ERROR_MESSAGE
  72. )
  73. if SANIC_VERSION.startswith("0.8."):
  74. # Sanic 0.8 and older creates a logger named "root" and puts a
  75. # stringified version of every exception in there (without exc_info),
  76. # which our error deduplication can't detect.
  77. #
  78. # We explicitly check the version here because it is a very
  79. # invasive step to ignore this logger and not necessary in newer
  80. # versions at all.
  81. #
  82. # https://github.com/huge-success/sanic/issues/1332
  83. ignore_logger("root")
  84. if SanicIntegration.version is not None and SanicIntegration.version < (21, 9):
  85. _setup_legacy_sanic()
  86. return
  87. _setup_sanic()
  88. class SanicRequestExtractor(RequestExtractor):
  89. def content_length(self) -> int:
  90. if self.request.body is None:
  91. return 0
  92. return len(self.request.body)
  93. def cookies(self) -> "Dict[str, str]":
  94. return dict(self.request.cookies)
  95. def raw_data(self) -> bytes:
  96. return self.request.body
  97. def form(self) -> "RequestParameters":
  98. return self.request.form
  99. def is_json(self) -> bool:
  100. raise NotImplementedError()
  101. def json(self) -> "Optional[Any]":
  102. return self.request.json
  103. def files(self) -> "RequestParameters":
  104. return self.request.files
  105. def size_of_file(self, file: "Any") -> int:
  106. return len(file.body or ())
  107. def _setup_sanic() -> None:
  108. Sanic._startup = _startup
  109. ErrorHandler.lookup = _sentry_error_handler_lookup
  110. def _setup_legacy_sanic() -> None:
  111. Sanic.handle_request = _legacy_handle_request
  112. Router.get = _legacy_router_get
  113. ErrorHandler.lookup = _sentry_error_handler_lookup
  114. async def _startup(self: "Sanic") -> None:
  115. # This happens about as early in the lifecycle as possible, just after the
  116. # Request object is created. The body has not yet been consumed.
  117. self.signal("http.lifecycle.request")(_context_enter)
  118. # This happens after the handler is complete. In v21.9 this signal is not
  119. # dispatched when there is an exception. Therefore we need to close out
  120. # and call _context_exit from the custom exception handler as well.
  121. # See https://github.com/sanic-org/sanic/issues/2297
  122. self.signal("http.lifecycle.response")(_context_exit)
  123. # This happens inside of request handling immediately after the route
  124. # has been identified by the router.
  125. self.signal("http.routing.after")(_set_transaction)
  126. # The above signals need to be declared before this can be called.
  127. await old_startup(self)
  128. async def _context_enter(request: "Request") -> None:
  129. request.ctx._sentry_do_integration = (
  130. sentry_sdk.get_client().get_integration(SanicIntegration) is not None
  131. )
  132. if not request.ctx._sentry_do_integration:
  133. return
  134. weak_request = weakref.ref(request)
  135. request.ctx._sentry_scope = sentry_sdk.isolation_scope()
  136. scope = request.ctx._sentry_scope.__enter__()
  137. scope.clear_breadcrumbs()
  138. scope.add_event_processor(_make_request_processor(weak_request))
  139. transaction = continue_trace(
  140. dict(request.headers),
  141. op=OP.HTTP_SERVER,
  142. # Unless the request results in a 404 error, the name and source will get overwritten in _set_transaction
  143. name=request.path,
  144. source=TransactionSource.URL,
  145. origin=SanicIntegration.origin,
  146. )
  147. request.ctx._sentry_transaction = sentry_sdk.start_transaction(
  148. transaction
  149. ).__enter__()
  150. async def _context_exit(
  151. request: "Request", response: "Optional[BaseHTTPResponse]" = None
  152. ) -> None:
  153. with capture_internal_exceptions():
  154. if not request.ctx._sentry_do_integration:
  155. return
  156. integration = sentry_sdk.get_client().get_integration(SanicIntegration)
  157. response_status = None if response is None else response.status
  158. # This capture_internal_exceptions block has been intentionally nested here, so that in case an exception
  159. # happens while trying to end the transaction, we still attempt to exit the hub.
  160. with capture_internal_exceptions():
  161. request.ctx._sentry_transaction.set_http_status(response_status)
  162. request.ctx._sentry_transaction.sampled &= (
  163. isinstance(integration, SanicIntegration)
  164. and response_status not in integration._unsampled_statuses
  165. )
  166. request.ctx._sentry_transaction.__exit__(None, None, None)
  167. request.ctx._sentry_scope.__exit__(None, None, None)
  168. async def _set_transaction(request: "Request", route: "Route", **_: "Any") -> None:
  169. if request.ctx._sentry_do_integration:
  170. with capture_internal_exceptions():
  171. scope = sentry_sdk.get_current_scope()
  172. route_name = route.name.replace(request.app.name, "").strip(".")
  173. scope.set_transaction_name(route_name, source=TransactionSource.COMPONENT)
  174. def _sentry_error_handler_lookup(
  175. self: "Any", exception: Exception, *args: "Any", **kwargs: "Any"
  176. ) -> "Optional[object]":
  177. _capture_exception(exception)
  178. old_error_handler = old_error_handler_lookup(self, exception, *args, **kwargs)
  179. if old_error_handler is None:
  180. return None
  181. if sentry_sdk.get_client().get_integration(SanicIntegration) is None:
  182. return old_error_handler
  183. async def sentry_wrapped_error_handler(
  184. request: "Request", exception: Exception
  185. ) -> "Any":
  186. try:
  187. response = old_error_handler(request, exception)
  188. if isawaitable(response):
  189. response = await response
  190. return response
  191. except Exception:
  192. # Report errors that occur in Sanic error handler. These
  193. # exceptions will not even show up in Sanic's
  194. # `sanic.exceptions` logger.
  195. exc_info = sys.exc_info()
  196. _capture_exception(exc_info)
  197. reraise(*exc_info)
  198. finally:
  199. # As mentioned in previous comment in _startup, this can be removed
  200. # after https://github.com/sanic-org/sanic/issues/2297 is resolved
  201. if SanicIntegration.version and SanicIntegration.version == (21, 9):
  202. await _context_exit(request)
  203. return sentry_wrapped_error_handler
  204. async def _legacy_handle_request(
  205. self: "Any", request: "Request", *args: "Any", **kwargs: "Any"
  206. ) -> "Any":
  207. if sentry_sdk.get_client().get_integration(SanicIntegration) is None:
  208. return await old_handle_request(self, request, *args, **kwargs)
  209. weak_request = weakref.ref(request)
  210. with sentry_sdk.isolation_scope() as scope:
  211. scope.clear_breadcrumbs()
  212. scope.add_event_processor(_make_request_processor(weak_request))
  213. response = old_handle_request(self, request, *args, **kwargs)
  214. if isawaitable(response):
  215. response = await response
  216. return response
  217. def _legacy_router_get(self: "Any", *args: "Union[Any, Request]") -> "Any":
  218. rv = old_router_get(self, *args)
  219. if sentry_sdk.get_client().get_integration(SanicIntegration) is not None:
  220. with capture_internal_exceptions():
  221. scope = sentry_sdk.get_isolation_scope()
  222. if SanicIntegration.version and SanicIntegration.version >= (21, 3):
  223. # Sanic versions above and including 21.3 append the app name to the
  224. # route name, and so we need to remove it from Route name so the
  225. # transaction name is consistent across all versions
  226. sanic_app_name = self.ctx.app.name
  227. sanic_route = rv[0].name
  228. if sanic_route.startswith("%s." % sanic_app_name):
  229. # We add a 1 to the len of the sanic_app_name because there is a dot
  230. # that joins app name and the route name
  231. # Format: app_name.route_name
  232. sanic_route = sanic_route[len(sanic_app_name) + 1 :]
  233. scope.set_transaction_name(
  234. sanic_route, source=TransactionSource.COMPONENT
  235. )
  236. else:
  237. scope.set_transaction_name(
  238. rv[0].__name__, source=TransactionSource.COMPONENT
  239. )
  240. return rv
  241. @ensure_integration_enabled(SanicIntegration)
  242. def _capture_exception(exception: "Union[ExcInfo, BaseException]") -> None:
  243. with capture_internal_exceptions():
  244. event, hint = event_from_exception(
  245. exception,
  246. client_options=sentry_sdk.get_client().options,
  247. mechanism={"type": "sanic", "handled": False},
  248. )
  249. if hint and hasattr(hint["exc_info"][0], "quiet") and hint["exc_info"][0].quiet:
  250. return
  251. sentry_sdk.capture_event(event, hint=hint)
  252. def _make_request_processor(weak_request: "Callable[[], Request]") -> "EventProcessor":
  253. def sanic_processor(event: "Event", hint: "Optional[Hint]") -> "Optional[Event]":
  254. try:
  255. if hint and issubclass(hint["exc_info"][0], SanicException):
  256. return None
  257. except KeyError:
  258. pass
  259. request = weak_request()
  260. if request is None:
  261. return event
  262. with capture_internal_exceptions():
  263. extractor = SanicRequestExtractor(request)
  264. extractor.extract_into_event(event)
  265. request_info = event["request"]
  266. urlparts = urlsplit(request.url)
  267. request_info["url"] = "%s://%s%s" % (
  268. urlparts.scheme,
  269. urlparts.netloc,
  270. urlparts.path,
  271. )
  272. request_info["query_string"] = urlparts.query
  273. request_info["method"] = request.method
  274. request_info["env"] = {"REMOTE_ADDR": request.remote_addr}
  275. request_info["headers"] = _filter_headers(dict(request.headers))
  276. return event
  277. return sanic_processor