sentry-python
sentry-python copied to clipboard
Add support for falcon 3.0
Currently, Falcon ≥ 3.0.0-ALPHA is not supported by Sentry.
Falcon 3.0 renames the API class to APP. This trips the Sentry Falcon integration detection: the import of api_helpers
is renamed to app_helpers
.
https://github.com/getsentry/sentry-python/blob/41120009fa7d6cb88d9219cb20874c9dd705639d/sentry_sdk/integrations/falcon.py#L18-L24
Should be a simple fix, roughly:
try:
import falcon # type: ignore
from falcon import __version__ as FALCON_VERSION
try: # from L102
version = tuple(map(int, FALCON_VERSION.split(".")))
except (ValueError, TypeError):
raise DidNotEnable("Unparseable Falcon version: {}".format(FALCON_VERSION))
if version < (3, 0)
import falcon.api_helpers # type: ignore
falcon_helpers = falcon.api_helpers
else:
import falcon.app_helpers # type: ignore
falcon_helpers = falcon.app_helpers
except ImportError:
raise DidNotEnable("Falcon not installed")
and renaming all references to falcon.api_helpers
to falcon_helpers
.
A quick glance shows that PrepareMiddleware
has not changed (returns the same tuple). I can go ahead and open a PR and check if other changes are necessary to support Falcon 3.0 before it comes out of alpha.
Feel free to, but I feel like it's wasted time until it comes out of alpha. Preparing support for APIs that change in the next alpha has bitten me in the past.
Fair point, I just looked at the status of the project and it might be a while. Can be picked up in the future.
Falcon 3.0 still hasn't been released so until that happens let's just close this.
@untitaker Falcon 3.0 was released 6 days ago.
I'm using falcon 3.0.1, is this why the following unhandled exception isn't detected by sentry?
sentry_sdk.init(
SENTRY_DSN,
debug=True,
traces_sample_rate=1.0,
integrations=[
ExcepthookIntegration(always_run=True),
LoggingIntegration(event_level=None),
FalconIntegration(),
],
)
app = application = falcon.App(middleware=[CORSMiddleware()])
app.add_route("/health", Health())
class Health(object):
def on_get(self, req, resp):
division_by_zero = 1 / 0 # "ZeroDivisionError: division by zero" (undetected by sentry) => "500 Internal Server Error" (undetected by sentry)
resp.status = falcon.HTTP_200
resp.text = "OK"
Sending it manually (for testing purpose) works:
class Health(object):
def on_get(self, req, resp):
try:
division_by_zero = 1 / 0
except Exception as e:
capture_exception(e) # [sentry] DEBUG: Sending event, type:null level:error ...
resp.status = falcon.HTTP_200
resp.text = "OK"
Any workaround?
any news on that ? It seems that on falcon 3 the exceptions aren't sent to sentry
any news on falcon3 integration ?
Any news on that front? Will be nice to have 3.0 support for Falcon, also will be cool to have support for Falcon async
any news on the falcon3 integration?
This issue has gone three weeks without activity. In another week, I will close it.
But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog
or Status: In Progress
, I will leave it alone ... forever!
"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀
any news on the falcon3 integration?
Will this be re-opened or will a new issue need to be created for this?
As a workaround, I've just put the integration module locally, e.g. utils/sentry_falcon.py
:
from sentry_sdk.hub import Hub
from sentry_sdk.integrations import Integration, DidNotEnable
from sentry_sdk.integrations._wsgi_common import RequestExtractor
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
from sentry_sdk.utils import capture_internal_exceptions, event_from_exception
from sentry_sdk._types import MYPY
if MYPY:
from typing import Any
from typing import Dict
from typing import Optional
from sentry_sdk._types import EventProcessor
try:
import falcon # type: ignore
import falcon.app_helpers # type: ignore
from falcon import __version__ as FALCON_VERSION
except ImportError:
raise DidNotEnable("Falcon not installed")
class FalconRequestExtractor(RequestExtractor):
def env(self):
# type: () -> Dict[str, Any]
return self.request.env
def cookies(self):
# type: () -> Dict[str, Any]
return self.request.cookies
def form(self):
# type: () -> None
return None # No such concept in Falcon
def files(self):
# type: () -> None
return None # No such concept in Falcon
def raw_data(self):
# type: () -> Optional[str]
# As request data can only be read once we won't make this available
# to Sentry. Just send back a dummy string in case there was a
# content length.
# TODO(jmagnusson): Figure out if there's a way to support this
content_length = self.content_length()
if content_length > 0:
return "[REQUEST_CONTAINING_RAW_DATA]"
else:
return None
def json(self):
# type: () -> Optional[Dict[str, Any]]
try:
return self.request.media
except falcon.errors.HTTPBadRequest:
# NOTE(jmagnusson): We return `falcon.Request._media` here because
# falcon 1.4 doesn't do proper type checking in
# `falcon.Request.media`. This has been fixed in 2.0.
# Relevant code: https://github.com/falconry/falcon/blob/1.4.1/falcon/request.py#L953
return self.request._media
class SentryFalconMiddleware(object):
"""Captures exceptions in Falcon requests and send to Sentry"""
def process_request(self, req, resp, *args, **kwargs):
# type: (Any, Any, *Any, **Any) -> None
hub = Hub.current
integration = hub.get_integration(FalconIntegration)
if integration is None:
return
with hub.configure_scope() as scope:
scope._name = "falcon"
scope.add_event_processor(_make_request_event_processor(req, integration))
TRANSACTION_STYLE_VALUES = ("uri_template", "path")
class FalconIntegration(Integration):
identifier = "falcon"
transaction_style = None
def __init__(self, transaction_style="uri_template"):
# type: (str) -> 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)
)
self.transaction_style = transaction_style
@staticmethod
def setup_once():
# type: () -> None
try:
version = tuple(map(int, FALCON_VERSION.split(".")))
except (ValueError, TypeError):
raise DidNotEnable("Unparsable Falcon version: {}".format(FALCON_VERSION))
if version < (1, 4):
raise DidNotEnable("Falcon 1.4 or newer required.")
_patch_wsgi_app()
_patch_handle_exception()
_patch_prepare_middleware()
def _patch_wsgi_app():
# type: () -> None
original_wsgi_app = falcon.App.__call__
def sentry_patched_wsgi_app(self, env, start_response):
# type: (falcon.App, Any, Any) -> Any
hub = Hub.current
integration = hub.get_integration(FalconIntegration)
if integration is None:
return original_wsgi_app(self, env, start_response)
sentry_wrapped = SentryWsgiMiddleware(
lambda envi, start_resp: original_wsgi_app(self, envi, start_resp)
)
return sentry_wrapped(env, start_response)
falcon.App.__call__ = sentry_patched_wsgi_app
def _patch_handle_exception():
# type: () -> None
original_handle_exception = falcon.App._handle_exception
def sentry_patched_handle_exception(self, *args):
# type: (falcon.App, *Any) -> Any
# NOTE(jmagnusson): falcon 2.0 changed falcon.App._handle_exception
# method signature from `(ex, req, resp, params)` to
# `(req, resp, ex, params)`
if isinstance(args[0], Exception):
ex = args[0]
else:
ex = args[2]
was_handled = original_handle_exception(self, *args)
hub = Hub.current
integration = hub.get_integration(FalconIntegration)
if integration is not None and _exception_leads_to_http_5xx(ex):
# If an integration is there, a client has to be there.
client = hub.client # type: Any
event, hint = event_from_exception(
ex,
client_options=client.options,
mechanism={"type": "falcon", "handled": False},
)
hub.capture_event(event, hint=hint)
return was_handled
falcon.App._handle_exception = sentry_patched_handle_exception
def _patch_prepare_middleware():
# type: () -> None
original_prepare_middleware = falcon.app_helpers.prepare_middleware
def sentry_patched_prepare_middleware(
middleware=None, independent_middleware=False
):
# type: (Any, Any) -> Any
hub = Hub.current
integration = hub.get_integration(FalconIntegration)
if integration is not None:
middleware = [SentryFalconMiddleware()] + (middleware or [])
return original_prepare_middleware(middleware, independent_middleware)
falcon.app_helpers.prepare_middleware = sentry_patched_prepare_middleware
def _exception_leads_to_http_5xx(ex):
# type: (Exception) -> bool
is_server_error = isinstance(ex, falcon.HTTPError) and (ex.status or "").startswith(
"5"
)
is_unhandled_error = not isinstance(
ex, (falcon.HTTPError, falcon.http_status.HTTPStatus)
)
return is_server_error or is_unhandled_error
def _make_request_event_processor(req, integration):
# type: (falcon.Request, FalconIntegration) -> EventProcessor
def inner(event, hint):
# type: (Dict[str, Any], Dict[str, Any]) -> Dict[str, Any]
if integration.transaction_style == "uri_template":
event["transaction"] = req.uri_template
elif integration.transaction_style == "path":
event["transaction"] = req.path
with capture_internal_exceptions():
FalconRequestExtractor(req).extract_into_event(event)
return event
return inner
Hello all!
We have two PRs from the community for this: https://github.com/getsentry/sentry-python/pull/1073 (For Falcon 3.0.0) https://github.com/getsentry/sentry-python/pull/1297 (For Falcon 3.0.1)
The current plan is to merge those two PRs into one (or pick one of them) and then bring this into shape so it is ready for review.
This could take some time as we want to make this right!
Falcon deprecated api_helpers
and moved it to app_helpers
in April 2021
see https://github.com/falconry/falcon/issues/1903
would be nice to get #1297 merged (open since Jan 2022) so that sentry stops causing DeprecatedWarning: The api_helpers module was renamed to app_helpers.
to appear in all my cronjobs.
what's need to push #1297 over the finish line?