8000 fix(pyramid): Set transaction name eagerly (#686) · getsentry/sentry-python@47e3670 · GitHub
[go: up one dir, main page]

Skip to content

Commit 47e3670

Browse files
authored
fix(pyramid): Set transaction name eagerly (#686)
This is needed for APM and also fixes #683
1 parent 45b13a7 commit 47e3670

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

sentry_sdk/integrations/pyramid.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,33 @@ def __init__(self, transaction_style="route_name"):
6363
@staticmethod
6464
def setup_once():
6565
# type: () -> None
66-
from pyramid.router import Router
66+
from pyramid import router
6767
from pyramid.request import Request
6868

69-
old_handle_request = Router.handle_request
69+
old_call_view = router._call_view
7070

71-
def sentry_patched_handle_request(self, request, *args, **kwargs):
71+
def sentry_patched_call_view(registry, request, *args, **kwargs):
7272
# type: (Any, Request, *Any, **Any) -> Response
7373
hub = Hub.current
7474
integration = hub.get_integration(PyramidIntegration)
75+
7576
if integration is not None:
7677
with hub.configure_scope() as scope:
78+
try:
79+
if integration.transaction_style == "route_name":
80+
scope.transaction = request.matched_route.name
81+
elif integration.transaction_style == "route_pattern":
82+
scope.transaction = request.matched_route.pattern
83+
except Exception:
84+
raise
85+
7786
scope.add_event_processor(
7887
_make_event_processor(weakref.ref(request), integration)
7988
)
8089

81-
return old_handle_request(self, request, *args, **kwargs)
90+
return old_call_view(registry, request, *args, **kwargs)
8291

83-
Router.handle_request = sentry_patched_handle_request
92+
router._call_view = sentry_patched_call_view
8493

8594
if hasattr(Request, "invoke_exception_view"):
8695
old_invoke_exception_view = Request.invoke_exception_view
@@ -101,7 +110,7 @@ def sentry_patched_invoke_exception_view(self, *args, **kwargs):
101110

102111
Request.invoke_exception_view = sentry_patched_invoke_exception_view
103112

104-
old_wsgi_call = Router.__call__
113+
old_wsgi_call = router.Router.__call__
105114

106115
def sentry_patched_wsgi_call(self, environ, start_response):
107116
# type: (Any, Dict[str, str], Callable[..., Any]) -> _ScopedResponse
@@ -123,7 +132,7 @@ def sentry_patched_inner_wsgi_call(environ, start_response):
123132
environ, start_response
124133
)
125134

126-
Router.__call__ = sentry_patched_wsgi_call
135+
router.Router.__call__ = sentry_patched_wsgi_call
127136

128137

129138
def _capture_exception(exc_info):
@@ -196,14 +205,6 @@ def event_processor(event, hint):
196205
if request is None:
197206
return event
198207

199-
try:
200-
if integration.transaction_style == "route_name":
201-
event["transaction"] = request.matched_route.name
202-
elif integration.transaction_style == "route_pattern":
203-
event["transaction"] = request.matched_route.pattern
204-
except Exception:
205-
pass
206-
207208
with capture_internal_exceptions():
208209
PyramidRequestExtractor(request).extract_into_event(event)
209210

tox.ini

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ envlist =
4444
# The aws_lambda tests deploy to the real AWS and have their own matrix of Python versions.
4545
py3.7-aws_lambda
4646

47-
{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}
47+
{pypy,py2.7,py3.5,py3.6,py3.7,py3.8}-pyramid-{1.6,1.7,1.8,1.9,1.10}
4848

4949
{pypy,py2.7,py3.5,py3.6}-rq-{0.6,0.7,0.8,0.9,0.10,0.11}
5050
{pypy,py2.7,py3.5,py3.6,py3.7,py3.8}-rq-{0.12,0.13,1.0,1.1,1.2,1.3}
@@ -126,9 +126,6 @@ deps =
126126

127127
aws_lambda: boto3
128128

129-
pyramid-1.3: pyramid>=1.3,<1.4
130-
pyramid-1.4: pyramid>=1.4,<1.5
131-
pyramid-1.5: pyramid>=1.5,<1.6
132129
pyramid-1.6: pyramid>=1.6,<1.7
133130
pyramid-1.7: pyramid>=1.7,<1.8
134131
pyramid-1.8: pyramid>=1.8,<1.9

0 commit comments

Comments
 (0)
0