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

Skip to content

fix(pyramid): Set transaction name eagerly #686

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 4 commits into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
31 changes: 16 additions & 15 deletions sentry_sdk/integrations/pyramid.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,33 @@ 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)

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:
raise

scope.add_event_processor(
_make_event_processor(weakref.ref(request), integration)
)

return old_handle_request(self, request, *args, **kwargs)
return old_call_view(registry, 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
Expand All @@ -101,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
Expand All @@ -123,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):
Expand Down Expand Up @@ -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)

Expand Down
5 changes: 1 addition & 4 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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
Expand Down
0