Skip to content
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

feat(falcon): Update of Falcon Integration #1733

Merged
merged 21 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
a5ee095
Added falcon 3.x to test suite
antonpirker Nov 10, 2022
b6d8131
Updated test matrix for Falcon
antonpirker Nov 10, 2022
e4f7feb
The Falcon integration for Falcon 3.0.1 (#1297)
bartolootrit Nov 10, 2022
979b8c5
Update test matrix
antonpirker Nov 10, 2022
76d511c
Made test matrix according to PyPi supported versions
antonpirker Nov 10, 2022
48b6ff5
Updated test config
antonpirker Nov 10, 2022
462faf5
Merge branch 'master' into antonpirker/falcon3
antonpirker Nov 10, 2022
1787555
Merge branch 'master' into antonpirker/falcon3
antonpirker Nov 24, 2022
96b6d14
Merge branch 'master' into antonpirker/falcon3
antonpirker Jan 25, 2023
2f750b9
Updated test script
antonpirker Jan 25, 2023
2b32178
Merge branch 'master' into antonpirker/falcon3
antonpirker Jan 25, 2023
d53c8a6
Merge branch 'master' into antonpirker/falcon3
antonpirker Jan 30, 2023
caa1fbf
Merge branch 'master' into antonpirker/falcon3
antonpirker Feb 6, 2023
7392877
Merge branch 'master' into antonpirker/falcon3
antonpirker Feb 17, 2023
4ca9ef2
Merge branch 'master' into antonpirker/falcon3
antonpirker Feb 17, 2023
1765639
Mocking http requests instead of doing real ones.
antonpirker Feb 17, 2023
6418e1f
Removed obsolete comment
antonpirker Feb 17, 2023
8469785
Removed usage of httpbin.org and where possible replaced by a mock. (…
antonpirker Feb 17, 2023
f4338f3
Falcon 3 integration code coverage (#1890)
bartolootrit Feb 17, 2023
f051141
Merge branch 'antonpirker/fix-httpx-tests' into antonpirker/falcon3
antonpirker Feb 17, 2023
e3f14a8
Merge branch 'antonpirker/falcon3' of github.com:getsentry/sentry-pyt…
antonpirker Feb 17, 2023
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
2 changes: 1 addition & 1 deletion .github/workflows/test-integration-falcon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["2.7","3.5","3.6","3.7","3.8","3.9","3.10","3.11"]
python-version: ["2.7","3.5","3.6","3.7","3.8","3.9"]
# python3.6 reached EOL and is no longer being supported on
# new versions of hosted runners on Github Actions
# ubuntu-20.04 is the last version that supported python3.6
Expand Down
58 changes: 42 additions & 16 deletions sentry_sdk/integrations/falcon.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@
raise DidNotEnable("Falcon not installed")


# In Falcon 3.0 `falcon.api_helpers` is renamed to `falcon.app_helpers`
# and `falcon.API` to `falcon.App`
try:
import falcon.app # type: ignore
import falcon.app_helpers # type: ignore

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be preferable to attempt to import falcon.app_helpers first, then import falcon.api_helpers if the first fails.
Otherwise, the current code will keep complaining with deprecation warnings that api_helpers has been renamed.


falcon_helpers = falcon.app_helpers
falcon_app_class = falcon.App
FALCON3 = True
except ImportError:
falcon_helpers = falcon.api_helpers
falcon_app_class = falcon.API
FALCON3 = False


class FalconRequestExtractor(RequestExtractor):
def env(self):
# type: () -> Dict[str, Any]
Expand Down Expand Up @@ -58,16 +73,27 @@ def raw_data(self):
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
if FALCON3:

def json(self):
antonpirker marked this conversation as resolved.
Show resolved Hide resolved
# type: () -> Optional[Dict[str, Any]]
try:
return self.request.media
except falcon.errors.HTTPBadRequest:
return None

else:

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):
Expand Down Expand Up @@ -120,7 +146,7 @@ def setup_once():

def _patch_wsgi_app():
# type: () -> None
original_wsgi_app = falcon.API.__call__
original_wsgi_app = falcon_app_class.__call__

def sentry_patched_wsgi_app(self, env, start_response):
# type: (falcon.API, Any, Any) -> Any
Expand All @@ -135,12 +161,12 @@ def sentry_patched_wsgi_app(self, env, start_response):

return sentry_wrapped(env, start_response)

falcon.API.__call__ = sentry_patched_wsgi_app
falcon_app_class.__call__ = sentry_patched_wsgi_app


def _patch_handle_exception():
# type: () -> None
original_handle_exception = falcon.API._handle_exception
original_handle_exception = falcon_app_class._handle_exception

def sentry_patched_handle_exception(self, *args):
# type: (falcon.API, *Any) -> Any
Expand Down Expand Up @@ -170,12 +196,12 @@ def sentry_patched_handle_exception(self, *args):

return was_handled

falcon.API._handle_exception = sentry_patched_handle_exception
falcon_app_class._handle_exception = sentry_patched_handle_exception


def _patch_prepare_middleware():
# type: () -> None
original_prepare_middleware = falcon.api_helpers.prepare_middleware
original_prepare_middleware = falcon_helpers.prepare_middleware

def sentry_patched_prepare_middleware(
middleware=None, independent_middleware=False
Expand All @@ -187,7 +213,7 @@ def sentry_patched_prepare_middleware(
middleware = [SentryFalconMiddleware()] + (middleware or [])
return original_prepare_middleware(middleware, independent_middleware)

falcon.api_helpers.prepare_middleware = sentry_patched_prepare_middleware
falcon_helpers.prepare_middleware = sentry_patched_prepare_middleware


def _exception_leads_to_http_5xx(ex):
Expand Down
6 changes: 4 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ envlist =

# Falcon
{py2.7,py3.5,py3.6,py3.7}-falcon-v{1.4}
{py2.7,py3.5,py3.6,py3.7,py3.8,py3.9,py3.10,py3.11}-falcon-v{2.0}

{py2.7,py3.5,py3.6,py3.7}-falcon-v{2.0}
{py3.5,py3.6,py3.7,py3.8,py3.9}-falcon-v{3.0}

# FastAPI
{py3.7,py3.8,py3.9,py3.10,py3.11}-fastapi

Expand Down Expand Up @@ -245,6 +246,7 @@ deps =
# Falcon
falcon-v1.4: falcon>=1.4,<1.5
falcon-v2.0: falcon>=2.0.0rc3,<3.0
falcon-v3.0: falcon>=3.0.0,<3.1.0

# FastAPI
fastapi: fastapi
Expand Down