bottle.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import functools
  2. import sentry_sdk
  3. from sentry_sdk.tracing import SOURCE_FOR_STYLE
  4. from sentry_sdk.utils import (
  5. capture_internal_exceptions,
  6. ensure_integration_enabled,
  7. event_from_exception,
  8. parse_version,
  9. transaction_from_function,
  10. )
  11. from sentry_sdk.integrations import (
  12. Integration,
  13. DidNotEnable,
  14. _DEFAULT_FAILED_REQUEST_STATUS_CODES,
  15. _check_minimum_version,
  16. )
  17. from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
  18. from sentry_sdk.integrations._wsgi_common import RequestExtractor
  19. from typing import TYPE_CHECKING
  20. if TYPE_CHECKING:
  21. from collections.abc import Set
  22. from sentry_sdk.integrations.wsgi import _ScopedResponse
  23. from typing import Any
  24. from typing import Dict
  25. from typing import Callable
  26. from typing import Optional
  27. from bottle import FileUpload, FormsDict, LocalRequest # type: ignore
  28. from sentry_sdk._types import EventProcessor, Event
  29. try:
  30. from bottle import (
  31. Bottle,
  32. HTTPResponse,
  33. Route,
  34. request as bottle_request,
  35. __version__ as BOTTLE_VERSION,
  36. )
  37. except ImportError:
  38. raise DidNotEnable("Bottle not installed")
  39. TRANSACTION_STYLE_VALUES = ("endpoint", "url")
  40. class BottleIntegration(Integration):
  41. identifier = "bottle"
  42. origin = f"auto.http.{identifier}"
  43. transaction_style = ""
  44. def __init__(
  45. self,
  46. transaction_style="endpoint", # type: str
  47. *,
  48. failed_request_status_codes=_DEFAULT_FAILED_REQUEST_STATUS_CODES, # type: Set[int]
  49. ):
  50. # type: (...) -> None
  51. if transaction_style not in TRANSACTION_STYLE_VALUES:
  52. raise ValueError(
  53. "Invalid value for transaction_style: %s (must be in %s)"
  54. % (transaction_style, TRANSACTION_STYLE_VALUES)
  55. )
  56. self.transaction_style = transaction_style
  57. self.failed_request_status_codes = failed_request_status_codes
  58. @staticmethod
  59. def setup_once():
  60. # type: () -> None
  61. version = parse_version(BOTTLE_VERSION)
  62. _check_minimum_version(BottleIntegration, version)
  63. old_app = Bottle.__call__
  64. @ensure_integration_enabled(BottleIntegration, old_app)
  65. def sentry_patched_wsgi_app(self, environ, start_response):
  66. # type: (Any, Dict[str, str], Callable[..., Any]) -> _ScopedResponse
  67. middleware = SentryWsgiMiddleware(
  68. lambda *a, **kw: old_app(self, *a, **kw),
  69. span_origin=BottleIntegration.origin,
  70. )
  71. return middleware(environ, start_response)
  72. Bottle.__call__ = sentry_patched_wsgi_app
  73. old_handle = Bottle._handle
  74. @functools.wraps(old_handle)
  75. def _patched_handle(self, environ):
  76. # type: (Bottle, Dict[str, Any]) -> Any
  77. integration = sentry_sdk.get_client().get_integration(BottleIntegration)
  78. if integration is None:
  79. return old_handle(self, environ)
  80. scope = sentry_sdk.get_isolation_scope()
  81. scope._name = "bottle"
  82. scope.add_event_processor(
  83. _make_request_event_processor(self, bottle_request, integration)
  84. )
  85. res = old_handle(self, environ)
  86. return res
  87. Bottle._handle = _patched_handle
  88. old_make_callback = Route._make_callback
  89. @functools.wraps(old_make_callback)
  90. def patched_make_callback(self, *args, **kwargs):
  91. # type: (Route, *object, **object) -> Any
  92. prepared_callback = old_make_callback(self, *args, **kwargs)
  93. integration = sentry_sdk.get_client().get_integration(BottleIntegration)
  94. if integration is None:
  95. return prepared_callback
  96. def wrapped_callback(*args, **kwargs):
  97. # type: (*object, **object) -> Any
  98. try:
  99. res = prepared_callback(*args, **kwargs)
  100. except Exception as exception:
  101. _capture_exception(exception, handled=False)
  102. raise exception
  103. if (
  104. isinstance(res, HTTPResponse)
  105. and res.status_code in integration.failed_request_status_codes
  106. ):
  107. _capture_exception(res, handled=True)
  108. return res
  109. return wrapped_callback
  110. Route._make_callback = patched_make_callback
  111. class BottleRequestExtractor(RequestExtractor):
  112. def env(self):
  113. # type: () -> Dict[str, str]
  114. return self.request.environ
  115. def cookies(self):
  116. # type: () -> Dict[str, str]
  117. return self.request.cookies
  118. def raw_data(self):
  119. # type: () -> bytes
  120. return self.request.body.read()
  121. def form(self):
  122. # type: () -> FormsDict
  123. if self.is_json():
  124. return None
  125. return self.request.forms.decode()
  126. def files(self):
  127. # type: () -> Optional[Dict[str, str]]
  128. if self.is_json():
  129. return None
  130. return self.request.files
  131. def size_of_file(self, file):
  132. # type: (FileUpload) -> int
  133. return file.content_length
  134. def _set_transaction_name_and_source(event, transaction_style, request):
  135. # type: (Event, str, Any) -> None
  136. name = ""
  137. if transaction_style == "url":
  138. try:
  139. name = request.route.rule or ""
  140. except RuntimeError:
  141. pass
  142. elif transaction_style == "endpoint":
  143. try:
  144. name = (
  145. request.route.name
  146. or transaction_from_function(request.route.callback)
  147. or ""
  148. )
  149. except RuntimeError:
  150. pass
  151. event["transaction"] = name
  152. event["transaction_info"] = {"source": SOURCE_FOR_STYLE[transaction_style]}
  153. def _make_request_event_processor(app, request, integration):
  154. # type: (Bottle, LocalRequest, BottleIntegration) -> EventProcessor
  155. def event_processor(event, hint):
  156. # type: (Event, dict[str, Any]) -> Event
  157. _set_transaction_name_and_source(event, integration.transaction_style, request)
  158. with capture_internal_exceptions():
  159. BottleRequestExtractor(request).extract_into_event(event)
  160. return event
  161. return event_processor
  162. def _capture_exception(exception, handled):
  163. # type: (BaseException, bool) -> None
  164. event, hint = event_from_exception(
  165. exception,
  166. client_options=sentry_sdk.get_client().options,
  167. mechanism={"type": "bottle", "handled": handled},
  168. )
  169. sentry_sdk.capture_event(event, hint=hint)