Skip to content

Commit aec801a

Browse files
committed
fix(django): Set transaction lazily in event processor
Fix crash when app returns a 404 response See #19
1 parent 2310270 commit aec801a

File tree

5 files changed

+11
-24
lines changed

5 files changed

+11
-24
lines changed

sentry_sdk/integrations/django.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
except ImportError:
99
from django.core.urlresolvers import resolve
1010

11-
from sentry_sdk import get_current_hub, configure_scope, capture_exception
11+
from sentry_sdk import get_current_hub, capture_exception
1212
from sentry_sdk.hub import _internal_exceptions, _should_send_default_pii
1313
from ._wsgi import RequestExtractor, get_client_ip
1414
from . import Integration
@@ -45,10 +45,10 @@ def sentry_patched_get_response(self, request):
4545
lambda: make_event_processor(request)
4646
)
4747

48-
with configure_scope() as scope:
49-
scope.transaction = resolve(request.path).func.__name__
50-
51-
return old_get_response(self, request)
48+
try:
49+
return old_get_response(self, request)
50+
except Exception:
51+
capture_exception()
5252

5353
BaseHandler.get_response = sentry_patched_get_response
5454

@@ -58,6 +58,10 @@ def _make_event_processor(self, request):
5858
client_options = get_current_hub().client.options
5959

6060
def processor(event):
61+
if "transaction" not in event:
62+
with _internal_exceptions():
63+
event["transaction"] = resolve(request.path).func.__name__
64+
6165
with _internal_exceptions():
6266
DjangoRequestExtractor(request).extract_into_event(
6367
event, client_options

tests/integrations/django/myapp/settings.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,7 @@ def process_request(self, request):
5656
if "middleware-exc" in request.path:
5757
1 / 0
5858

59-
with sentry_sdk.configure_scope() as scope:
60-
assert scope._data["transaction"] is not None
61-
6259
def process_response(self, request, response):
63-
with sentry_sdk.configure_scope() as scope:
64-
assert scope._data["transaction"] is not None
65-
6660
return response
6761

6862

tests/integrations/django/myapp/urls.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@
2323
from . import views
2424

2525
urlpatterns = [
26-
path("self-check", views.self_check, name="self_check"),
2726
path("view-exc", views.view_exc, name="view_exc"),
28-
path("middleware-exc", views.self_check, name="middleware_exc"),
27+
path("middleware-exc", views.message, name="middleware_exc"),
2928
path("message", views.message, name="message"),
3029
path("mylogin", views.mylogin, name="mylogin"),
3130
]

tests/integrations/django/myapp/views.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@
55
import sentry_sdk
66

77

8-
def self_check(request):
9-
with sentry_sdk.configure_scope() as scope:
10-
assert scope._data["transaction"] == "self_check"
11-
return HttpResponse("ok")
12-
13-
148
def view_exc(request):
159
1 / 0
1610

tests/integrations/django/test_basic.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ def client(monkeypatch_test_transport):
1919
return Client()
2020

2121

22-
def test_scope_working(client):
23-
response = client.get(reverse("self_check"))
24-
assert response.status_code == 200
25-
26-
2722
def test_view_exceptions(client, capture_exceptions):
2823
exceptions = capture_exceptions()
2924
with pytest.raises(ZeroDivisionError) as exc:
@@ -46,6 +41,7 @@ def test_request_captured(client, capture_events):
4641
assert response.content == b"ok"
4742

4843
event, = events
44+
assert event["transaction"] == "message"
4945
assert event["request"] == {
5046
"cookies": {},
5147
"env": {

0 commit comments

Comments
 (0)