-
Notifications
You must be signed in to change notification settings - Fork 580
feat(integration): Add Falcon integration #346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b52d39c
feat(integration): Add Falcon integration
jacobsvante a86ec1c
`if False` typing import style + lint fixes
jacobsvante e4a45c7
Remove redundant scope
jacobsvante 79a5e62
Fix absolute imports errors in pypy and py2.7
jacobsvante c233eb3
Try to return falcon.Request.media in case it wasn’t consumed in the …
jacobsvante 5233e0c
Send request data on non-JSON requests as well
jacobsvante cdc6236
Lint fix
jacobsvante e120d90
Fix mypy failing
jacobsvante f2404d4
Automatically capture internal server errors when an error handler ha…
jacobsvante 813f8f5
Add falcon 2.0 to test suite
jacobsvante File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| from __future__ import absolute_import | ||
|
|
||
| import falcon # type: ignore | ||
| import falcon.api_helpers # type: ignore | ||
| from sentry_sdk.hub import Hub | ||
| from sentry_sdk.integrations import Integration | ||
| 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 | ||
|
|
||
| if False: | ||
| from typing import Any | ||
| from typing import Callable | ||
| from typing import Dict | ||
|
|
||
|
|
||
| class FalconRequestExtractor(RequestExtractor): | ||
| def env(self): | ||
| return self.request.env | ||
|
|
||
| def cookies(self): | ||
| return self.request.cookies | ||
|
|
||
| def form(self): | ||
| return None # No such concept in Falcon | ||
|
|
||
| def files(self): | ||
| return None # No such concept in Falcon | ||
|
|
||
| def raw_data(self): | ||
| # 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): | ||
| 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): | ||
| 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)) | ||
|
|
||
|
|
||
| class FalconIntegration(Integration): | ||
| identifier = "falcon" | ||
|
|
||
| transaction_style = None | ||
|
|
||
| def __init__(self, transaction_style="uri_template"): | ||
| # type: (str) -> None | ||
| TRANSACTION_STYLE_VALUES = ("uri_template", "path") | ||
| 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 | ||
| _patch_wsgi_app() | ||
| _patch_handle_exception() | ||
| _patch_prepare_middleware() | ||
|
|
||
|
|
||
| def _patch_wsgi_app(): | ||
| original_wsgi_app = falcon.API.__call__ | ||
|
|
||
| def sentry_patched_wsgi_app(self, env, start_response): | ||
| 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.API.__call__ = sentry_patched_wsgi_app | ||
|
|
||
|
|
||
| def _patch_handle_exception(): | ||
| original_handle_exception = falcon.API._handle_exception | ||
|
|
||
| def sentry_patched_handle_exception(self, *args): | ||
| # NOTE(jmagnusson): falcon 2.0 changed falcon.API._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 not _is_falcon_http_error(ex): | ||
| event, hint = event_from_exception( | ||
| ex, | ||
| client_options=hub.client.options, | ||
| mechanism={"type": "falcon", "handled": False}, | ||
| ) | ||
| hub.capture_event(event, hint=hint) | ||
|
|
||
| return was_handled | ||
|
|
||
| falcon.API._handle_exception = sentry_patched_handle_exception | ||
|
|
||
|
|
||
| def _patch_prepare_middleware(): | ||
| original_prepare_middleware = falcon.api_helpers.prepare_middleware | ||
|
|
||
| def sentry_patched_prepare_middleware( | ||
| middleware=None, independent_middleware=False | ||
| ): | ||
| 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.api_helpers.prepare_middleware = sentry_patched_prepare_middleware | ||
|
|
||
|
|
||
| def _is_falcon_http_error(ex): | ||
| return isinstance(ex, (falcon.HTTPError, falcon.http_status.HTTPStatus)) | ||
|
|
||
|
|
||
| def _make_request_event_processor(req, integration): | ||
| # type: (falcon.Request, FalconIntegration) -> Callable | ||
|
|
||
| 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.