Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MIGRATION_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Looking to upgrade from Sentry SDK 2.x to 3.x? Here's a comprehensive list of wh
- Dropped support for Django versions below 2.0.
- Dropped support for gevent versions below 20.9.
- Dropped support for greenlet versions below 0.4.17.
- Dropped support for Falcon versions below 3.0.
- Dropped support for Flask below 2.0.
- The `enable_tracing` option was removed. Use `traces_sample_rate=1.0` instead.
- The deprecated `push_scope` and `configure_scope` APIs have been removed. Use `with new_scope():` to push a new scope and `scope = get_current_scope()` to retrieve the current scope instead.
Expand Down
315 changes: 315 additions & 0 deletions scripts/populate_tox/package_dependencies.jsonl

Large diffs are not rendered by default.

852 changes: 845 additions & 7 deletions scripts/populate_tox/releases.jsonl

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sentry_sdk/integrations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def iter_default_integrations(
"cohere": (5, 4, 0),
"django": (2, 0),
"dramatiq": (1, 9),
"falcon": (1, 4),
"falcon": (3, 0),
"fastapi": (0, 79, 0),
"flask": (2, 0, 0),
"gql": (3, 4, 1),
Expand Down
55 changes: 13 additions & 42 deletions sentry_sdk/integrations/falcon.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,12 @@

try:
import falcon # type: ignore
from falcon import App, app_helpers
from falcon import __version__ as FALCON_VERSION
from falcon.request import _UNSET as _FALCON_UNSET # type: ignore
except ImportError:
Comment thread
sentrivana marked this conversation as resolved.
raise DidNotEnable("Falcon not installed")

try:
import falcon.app_helpers # type: ignore

falcon_helpers = falcon.app_helpers
falcon_app_class = falcon.App
FALCON3 = True
except ImportError:
import falcon.api_helpers # type: ignore

falcon_helpers = falcon.api_helpers
falcon_app_class = falcon.API
FALCON3 = False


_FALCON_UNSET: "Optional[object]" = None
if FALCON3: # falcon.request._UNSET is only available in Falcon 3.0+
with capture_internal_exceptions():
from falcon.request import ( # type: ignore[import-not-found, no-redef]
_UNSET as _FALCON_UNSET,
)


class FalconRequestExtractor(RequestExtractor):
def env(self) -> "Dict[str, Any]":
Expand Down Expand Up @@ -137,23 +118,22 @@ class FalconIntegration(Integration):
def __init__(self, transaction_style: str = "uri_template") -> None:
if transaction_style not in TRANSACTION_STYLE_VALUES:
raise ValueError(
"Invalid value for transaction_style: %s (must be in %s)"
% (transaction_style, TRANSACTION_STYLE_VALUES)
f"Invalid value for transaction_style: {transaction_style} "
f"(must be in {TRANSACTION_STYLE_VALUES})"
)
self.transaction_style = transaction_style

@staticmethod
def setup_once() -> None:
version = parse_version(FALCON_VERSION)
_check_minimum_version(FalconIntegration, version)
_check_minimum_version(FalconIntegration, parse_version(FALCON_VERSION))

_patch_wsgi_app()
_patch_handle_exception()
_patch_prepare_middleware()


def _patch_wsgi_app() -> None:
original_wsgi_app = falcon_app_class.__call__
original_wsgi_app = App.__call__

def sentry_patched_wsgi_app(
self: "falcon.API", env: "Any", start_response: "Any"
Expand All @@ -169,11 +149,11 @@ def sentry_patched_wsgi_app(

return sentry_wrapped(env, start_response)

falcon_app_class.__call__ = sentry_patched_wsgi_app
App.__call__ = sentry_patched_wsgi_app


def _patch_handle_exception() -> None:
original_handle_exception = falcon_app_class._handle_exception
original_handle_exception = App._handle_exception

@ensure_integration_enabled(FalconIntegration, original_handle_exception)
def sentry_patched_handle_exception(self: "falcon.API", *args: "Any") -> "Any":
Expand Down Expand Up @@ -205,11 +185,11 @@ def sentry_patched_handle_exception(self: "falcon.API", *args: "Any") -> "Any":

return was_handled

falcon_app_class._handle_exception = sentry_patched_handle_exception
App._handle_exception = sentry_patched_handle_exception


def _patch_prepare_middleware() -> None:
original_prepare_middleware = falcon_helpers.prepare_middleware
original_prepare_middleware = app_helpers.prepare_middleware

def sentry_patched_prepare_middleware(
middleware: "Any" = None,
Expand All @@ -224,11 +204,9 @@ def sentry_patched_prepare_middleware(
if integration is not None:
middleware = [SentryFalconMiddleware()] + (middleware or [])

# We intentionally omit the asgi argument here, since the default is False anyways,
# and this way, we remain backwards-compatible with pre-3.0.0 Falcon versions.
return original_prepare_middleware(middleware, independent_middleware)
return original_prepare_middleware(middleware, independent_middleware, asgi)

falcon_helpers.prepare_middleware = sentry_patched_prepare_middleware
app_helpers.prepare_middleware = sentry_patched_prepare_middleware


def _exception_leads_to_http_5xx(ex: Exception, response: "falcon.Response") -> bool:
Expand All @@ -239,14 +217,7 @@ def _exception_leads_to_http_5xx(ex: Exception, response: "falcon.Response") ->
ex, (falcon.HTTPError, falcon.http_status.HTTPStatus)
)

# We only check the HTTP status on Falcon 3 because in Falcon 2, the status on the response
# at the stage where we capture it is listed as 200, even though we would expect to see a 500
# status. Since at the time of this change, Falcon 2 is ca. 4 years old, we have decided to
# only perform this check on Falcon 3+, despite the risk that some handled errors might be
# reported to Sentry as unhandled on Falcon 2.
return (is_server_error or is_unhandled_error) and (
not FALCON3 or _has_http_5xx_status(response)
)
return (is_server_error or is_unhandled_error) and _has_http_5xx_status(response)


def _has_http_5xx_status(response: "falcon.Response") -> bool:
Expand Down
4 changes: 2 additions & 2 deletions tox.ini

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading