From a1fc21fec9268c704009d4ccf3037bcee93314a0 Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Wed, 13 May 2020 13:13:47 +0200 Subject: [PATCH 1/4] fix(pyramid): Set transaction name eagerly This is needed for APM and also fixes #683 --- sentry_sdk/integrations/pyramid.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/sentry_sdk/integrations/pyramid.py b/sentry_sdk/integrations/pyramid.py index ee9682343a..beb907646b 100644 --- a/sentry_sdk/integrations/pyramid.py +++ b/sentry_sdk/integrations/pyramid.py @@ -72,8 +72,17 @@ def sentry_patched_handle_request(self, request, *args, **kwargs): # type: (Any, Request, *Any, **Any) -> Response hub = Hub.current integration = hub.get_integration(PyramidIntegration) + if integration is not None: with hub.configure_scope() as scope: + try: + if integration.transaction_style == "route_name": + scope.transaction = request.matched_route.name + elif integration.transaction_style == "route_pattern": + scope.transaction = request.matched_route.pattern + except Exception: + pass + scope.add_event_processor( _make_event_processor(weakref.ref(request), integration) ) @@ -196,14 +205,6 @@ def event_processor(event, hint): if request is None: return event - try: - if integration.transaction_style == "route_name": - event["transaction"] = request.matched_route.name - elif integration.transaction_style == "route_pattern": - event["transaction"] = request.matched_route.pattern - except Exception: - pass - with capture_internal_exceptions(): PyramidRequestExtractor(request).extract_into_event(event) From 345b1a479a5340772ab973d301c3425c8a7cc9cc Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Mon, 18 May 2020 15:13:04 +0200 Subject: [PATCH 2/4] try to patch call_view instead --- sentry_sdk/integrations/pyramid.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sentry_sdk/integrations/pyramid.py b/sentry_sdk/integrations/pyramid.py index beb907646b..f3d1bee732 100644 --- a/sentry_sdk/integrations/pyramid.py +++ b/sentry_sdk/integrations/pyramid.py @@ -63,12 +63,12 @@ def __init__(self, transaction_style="route_name"): @staticmethod def setup_once(): # type: () -> None - from pyramid.router import Router + from pyramid import router from pyramid.request import Request - old_handle_request = Router.handle_request + old_call_view = router._call_view - def sentry_patched_handle_request(self, request, *args, **kwargs): + def sentry_patched_call_view(registry, request, *args, **kwargs): # type: (Any, Request, *Any, **Any) -> Response hub = Hub.current integration = hub.get_integration(PyramidIntegration) @@ -81,15 +81,15 @@ def sentry_patched_handle_request(self, request, *args, **kwargs): elif integration.transaction_style == "route_pattern": scope.transaction = request.matched_route.pattern except Exception: - pass + raise scope.add_event_processor( _make_event_processor(weakref.ref(request), integration) ) - return old_handle_request(self, request, *args, **kwargs) + return old_call_view(self, request, *args, **kwargs) - Router.handle_request = sentry_patched_handle_request + router._call_view = sentry_patched_call_view if hasattr(Request, "invoke_exception_view"): old_invoke_exception_view = Request.invoke_exception_view From b055f993841897c1c2501873e0d20257919daa3d Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Mon, 18 May 2020 19:32:00 +0200 Subject: [PATCH 3/4] drop support for pyramid 1.3-1.5 --- tox.ini | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tox.ini b/tox.ini index 67e957d2ae..7fdba0dacb 100644 --- a/tox.ini +++ b/tox.ini @@ -44,7 +44,7 @@ envlist = # The aws_lambda tests deploy to the real AWS and have their own matrix of Python versions. py3.7-aws_lambda - {pypy,py2.7,py3.5,py3.6,py3.7,py3.8}-pyramid-{1.3,1.4,1.5,1.6,1.7,1.8,1.9,1.10} + {pypy,py2.7,py3.5,py3.6,py3.7,py3.8}-pyramid-{1.6,1.7,1.8,1.9,1.10} {pypy,py2.7,py3.5,py3.6}-rq-{0.6,0.7,0.8,0.9,0.10,0.11} {pypy,py2.7,py3.5,py3.6,py3.7,py3.8}-rq-{0.12,0.13,1.0,1.1,1.2,1.3} @@ -127,9 +127,6 @@ deps = aws_lambda: boto3 - pyramid-1.3: pyramid>=1.3,<1.4 - pyramid-1.4: pyramid>=1.4,<1.5 - pyramid-1.5: pyramid>=1.5,<1.6 pyramid-1.6: pyramid>=1.6,<1.7 pyramid-1.7: pyramid>=1.7,<1.8 pyramid-1.8: pyramid>=1.8,<1.9 From 01659381e14bebe513cf1a6cd28f3fcd55b53158 Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Mon, 18 May 2020 22:54:48 +0200 Subject: [PATCH 4/4] fix build --- sentry_sdk/integrations/pyramid.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sentry_sdk/integrations/pyramid.py b/sentry_sdk/integrations/pyramid.py index f3d1bee732..657b697052 100644 --- a/sentry_sdk/integrations/pyramid.py +++ b/sentry_sdk/integrations/pyramid.py @@ -87,7 +87,7 @@ def sentry_patched_call_view(registry, request, *args, **kwargs): _make_event_processor(weakref.ref(request), integration) ) - return old_call_view(self, request, *args, **kwargs) + return old_call_view(registry, request, *args, **kwargs) router._call_view = sentry_patched_call_view @@ -110,7 +110,7 @@ def sentry_patched_invoke_exception_view(self, *args, **kwargs): Request.invoke_exception_view = sentry_patched_invoke_exception_view - old_wsgi_call = Router.__call__ + old_wsgi_call = router.Router.__call__ def sentry_patched_wsgi_call(self, environ, start_response): # type: (Any, Dict[str, str], Callable[..., Any]) -> _ScopedResponse @@ -132,7 +132,7 @@ def sentry_patched_inner_wsgi_call(environ, start_response): environ, start_response ) - Router.__call__ = sentry_patched_wsgi_call + router.Router.__call__ = sentry_patched_wsgi_call def _capture_exception(exc_info):