falcon.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import sentry_sdk
  2. from sentry_sdk.integrations import _check_minimum_version, Integration, DidNotEnable
  3. from sentry_sdk.integrations._wsgi_common import RequestExtractor
  4. from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
  5. from sentry_sdk.tracing import SOURCE_FOR_STYLE
  6. from sentry_sdk.utils import (
  7. capture_internal_exceptions,
  8. ensure_integration_enabled,
  9. event_from_exception,
  10. parse_version,
  11. )
  12. from typing import TYPE_CHECKING
  13. if TYPE_CHECKING:
  14. from typing import Any
  15. from typing import Dict
  16. from typing import Optional
  17. from sentry_sdk._types import Event, EventProcessor
  18. # In Falcon 3.0 `falcon.api_helpers` is renamed to `falcon.app_helpers`
  19. # and `falcon.API` to `falcon.App`
  20. try:
  21. import falcon # type: ignore
  22. from falcon import __version__ as FALCON_VERSION
  23. except ImportError:
  24. raise DidNotEnable("Falcon not installed")
  25. try:
  26. import falcon.app_helpers # type: ignore
  27. falcon_helpers = falcon.app_helpers
  28. falcon_app_class = falcon.App
  29. FALCON3 = True
  30. except ImportError:
  31. import falcon.api_helpers # type: ignore
  32. falcon_helpers = falcon.api_helpers
  33. falcon_app_class = falcon.API
  34. FALCON3 = False
  35. _FALCON_UNSET: "Optional[object]" = None
  36. if FALCON3: # falcon.request._UNSET is only available in Falcon 3.0+
  37. with capture_internal_exceptions():
  38. from falcon.request import _UNSET as _FALCON_UNSET # type: ignore[import-not-found, no-redef]
  39. class FalconRequestExtractor(RequestExtractor):
  40. def env(self) -> "Dict[str, Any]":
  41. return self.request.env
  42. def cookies(self) -> "Dict[str, Any]":
  43. return self.request.cookies
  44. def form(self) -> None:
  45. return None # No such concept in Falcon
  46. def files(self) -> None:
  47. return None # No such concept in Falcon
  48. def raw_data(self) -> "Optional[str]":
  49. # As request data can only be read once we won't make this available
  50. # to Sentry. Just send back a dummy string in case there was a
  51. # content length.
  52. # TODO(jmagnusson): Figure out if there's a way to support this
  53. content_length = self.content_length()
  54. if content_length > 0:
  55. return "[REQUEST_CONTAINING_RAW_DATA]"
  56. else:
  57. return None
  58. def json(self) -> "Optional[Dict[str, Any]]":
  59. # fallback to cached_media = None if self.request._media is not available
  60. cached_media = None
  61. with capture_internal_exceptions():
  62. # self.request._media is the cached self.request.media
  63. # value. It is only available if self.request.media
  64. # has already been accessed. Therefore, reading
  65. # self.request._media will not exhaust the raw request
  66. # stream (self.request.bounded_stream) because it has
  67. # already been read if self.request._media is set.
  68. cached_media = self.request._media
  69. if cached_media is not _FALCON_UNSET:
  70. return cached_media
  71. return None
  72. class SentryFalconMiddleware:
  73. """Captures exceptions in Falcon requests and send to Sentry"""
  74. def process_request(
  75. self, req: "Any", resp: "Any", *args: "Any", **kwargs: "Any"
  76. ) -> None:
  77. integration = sentry_sdk.get_client().get_integration(FalconIntegration)
  78. if integration is None:
  79. return
  80. scope = sentry_sdk.get_isolation_scope()
  81. scope._name = "falcon"
  82. scope.add_event_processor(_make_request_event_processor(req, integration))
  83. TRANSACTION_STYLE_VALUES = ("uri_template", "path")
  84. class FalconIntegration(Integration):
  85. identifier = "falcon"
  86. origin = f"auto.http.{identifier}"
  87. transaction_style = ""
  88. def __init__(self, transaction_style: str = "uri_template") -> None:
  89. if transaction_style not in TRANSACTION_STYLE_VALUES:
  90. raise ValueError(
  91. "Invalid value for transaction_style: %s (must be in %s)"
  92. % (transaction_style, TRANSACTION_STYLE_VALUES)
  93. )
  94. self.transaction_style = transaction_style
  95. @staticmethod
  96. def setup_once() -> None:
  97. version = parse_version(FALCON_VERSION)
  98. _check_minimum_version(FalconIntegration, version)
  99. _patch_wsgi_app()
  100. _patch_handle_exception()
  101. _patch_prepare_middleware()
  102. def _patch_wsgi_app() -> None:
  103. original_wsgi_app = falcon_app_class.__call__
  104. def sentry_patched_wsgi_app(
  105. self: "falcon.API", env: "Any", start_response: "Any"
  106. ) -> "Any":
  107. integration = sentry_sdk.get_client().get_integration(FalconIntegration)
  108. if integration is None:
  109. return original_wsgi_app(self, env, start_response)
  110. sentry_wrapped = SentryWsgiMiddleware(
  111. lambda envi, start_resp: original_wsgi_app(self, envi, start_resp),
  112. span_origin=FalconIntegration.origin,
  113. )
  114. return sentry_wrapped(env, start_response)
  115. falcon_app_class.__call__ = sentry_patched_wsgi_app
  116. def _patch_handle_exception() -> None:
  117. original_handle_exception = falcon_app_class._handle_exception
  118. @ensure_integration_enabled(FalconIntegration, original_handle_exception)
  119. def sentry_patched_handle_exception(self: "falcon.API", *args: "Any") -> "Any":
  120. # NOTE(jmagnusson): falcon 2.0 changed falcon.API._handle_exception
  121. # method signature from `(ex, req, resp, params)` to
  122. # `(req, resp, ex, params)`
  123. ex = response = None
  124. with capture_internal_exceptions():
  125. ex = next(argument for argument in args if isinstance(argument, Exception))
  126. response = next(
  127. argument for argument in args if isinstance(argument, falcon.Response)
  128. )
  129. was_handled = original_handle_exception(self, *args)
  130. if ex is None or response is None:
  131. # Both ex and response should have a non-None value at this point; otherwise,
  132. # there is an error with the SDK that will have been captured in the
  133. # capture_internal_exceptions block above.
  134. return was_handled
  135. if _exception_leads_to_http_5xx(ex, response):
  136. event, hint = event_from_exception(
  137. ex,
  138. client_options=sentry_sdk.get_client().options,
  139. mechanism={"type": "falcon", "handled": False},
  140. )
  141. sentry_sdk.capture_event(event, hint=hint)
  142. return was_handled
  143. falcon_app_class._handle_exception = sentry_patched_handle_exception
  144. def _patch_prepare_middleware() -> None:
  145. original_prepare_middleware = falcon_helpers.prepare_middleware
  146. def sentry_patched_prepare_middleware(
  147. middleware: "Any" = None,
  148. independent_middleware: "Any" = False,
  149. asgi: bool = False,
  150. ) -> "Any":
  151. if asgi:
  152. # We don't support ASGI Falcon apps, so we don't patch anything here
  153. return original_prepare_middleware(middleware, independent_middleware, asgi)
  154. integration = sentry_sdk.get_client().get_integration(FalconIntegration)
  155. if integration is not None:
  156. middleware = [SentryFalconMiddleware()] + (middleware or [])
  157. # We intentionally omit the asgi argument here, since the default is False anyways,
  158. # and this way, we remain backwards-compatible with pre-3.0.0 Falcon versions.
  159. return original_prepare_middleware(middleware, independent_middleware)
  160. falcon_helpers.prepare_middleware = sentry_patched_prepare_middleware
  161. def _exception_leads_to_http_5xx(ex: Exception, response: "falcon.Response") -> bool:
  162. is_server_error = isinstance(ex, falcon.HTTPError) and (ex.status or "").startswith(
  163. "5"
  164. )
  165. is_unhandled_error = not isinstance(
  166. ex, (falcon.HTTPError, falcon.http_status.HTTPStatus)
  167. )
  168. # We only check the HTTP status on Falcon 3 because in Falcon 2, the status on the response
  169. # at the stage where we capture it is listed as 200, even though we would expect to see a 500
  170. # status. Since at the time of this change, Falcon 2 is ca. 4 years old, we have decided to
  171. # only perform this check on Falcon 3+, despite the risk that some handled errors might be
  172. # reported to Sentry as unhandled on Falcon 2.
  173. return (is_server_error or is_unhandled_error) and (
  174. not FALCON3 or _has_http_5xx_status(response)
  175. )
  176. def _has_http_5xx_status(response: "falcon.Response") -> bool:
  177. return response.status.startswith("5")
  178. def _set_transaction_name_and_source(
  179. event: "Event", transaction_style: str, request: "falcon.Request"
  180. ) -> None:
  181. name_for_style = {
  182. "uri_template": request.uri_template,
  183. "path": request.path,
  184. }
  185. event["transaction"] = name_for_style[transaction_style]
  186. event["transaction_info"] = {"source": SOURCE_FOR_STYLE[transaction_style]}
  187. def _make_request_event_processor(
  188. req: "falcon.Request", integration: "FalconIntegration"
  189. ) -> "EventProcessor":
  190. def event_processor(event: "Event", hint: "dict[str, Any]") -> "Event":
  191. _set_transaction_name_and_source(event, integration.transaction_style, req)
  192. with capture_internal_exceptions():
  193. FalconRequestExtractor(req).extract_into_event(event)
  194. return event
  195. return event_processor