Skip to content
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
26 changes: 26 additions & 0 deletions sentry_sdk/integrations/django/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,25 @@ def sentry_patched_get_response(self, request):

BaseHandler.get_response = sentry_patched_get_response

try:
from rest_framework.views import APIView # type: ignore
except ImportError:
pass
else:
# DRF's request type (which wraps the Django request and proxies
# all attrs) has some attributes such as `data` which buffer
# request data. We want to use those in the RequestExtractor to
# get body data more reliably.
old_drf_initial = APIView.initial

def sentry_patched_drf_initial(self, request, *args, **kwargs):
with capture_internal_exceptions():
request._request._sentry_drf_request_backref = weakref.ref(request)
pass
return old_drf_initial(self, request, *args, **kwargs)

APIView.initial = sentry_patched_drf_initial

signals.got_request_exception.connect(_got_request_exception)

@add_global_event_processor
Expand Down Expand Up @@ -197,6 +216,13 @@ def event_processor(event, hint):
if request is None:
return event

try:
drf_request = request._sentry_drf_request_backref()
if drf_request is not None:
request = drf_request
except AttributeError:
pass

try:
if integration.transaction_style == "function_name":
event["transaction"] = transaction_from_function(
Expand Down
7 changes: 7 additions & 0 deletions tests/integrations/django/myapp/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@
urlpatterns.append(
path("rest-framework-exc", views.rest_framework_exc, name="rest_framework_exc")
)
urlpatterns.append(
path(
"rest-framework-read-body-and-exc",
views.rest_framework_read_body_and_exc,
name="rest_framework_read_body_and_exc",
)
)
except AttributeError:
pass

Expand Down
5 changes: 5 additions & 0 deletions tests/integrations/django/myapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
def rest_framework_exc(request):
1 / 0

@api_view(["POST"])
def rest_framework_read_body_and_exc(request):
request.data
1 / 0


except ImportError:
pass
Expand Down
21 changes: 12 additions & 9 deletions tests/integrations/django/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ def test_read_request(sentry_init, client, capture_events):

event, = events

assert event["request"]["data"] == {"hey": 42}
assert "data" not in event["request"]


def test_template_exception(sentry_init, client, capture_events):
Expand Down Expand Up @@ -413,12 +413,15 @@ def test_template_exception(sentry_init, client, capture_events):
]


@pytest.mark.parametrize(
"route", ["rest_framework_exc", "rest_framework_read_body_and_exc"]
)
@pytest.mark.parametrize(
"type,event_request",
[
[
"json",
{
lambda route: {
"cookies": {},
"data": {"foo": "bar"},
"env": {"SERVER_NAME": "localhost", "SERVER_PORT": "80"},
Expand All @@ -429,12 +432,12 @@ def test_template_exception(sentry_init, client, capture_events):
},
"method": "POST",
"query_string": "",
"url": "http://localhost/rest-framework-exc",
"url": "http://localhost/{}".format(route.replace("_", "-")),
},
],
[
"formdata",
{
lambda route: {
"cookies": {},
"data": {"foo": "bar"},
"env": {"SERVER_NAME": "localhost", "SERVER_PORT": "80"},
Expand All @@ -445,13 +448,13 @@ def test_template_exception(sentry_init, client, capture_events):
},
"method": "POST",
"query_string": "",
"url": "http://localhost/rest-framework-exc",
"url": "http://localhost/{}".format(route.replace("_", "-")),
},
],
],
)
def test_rest_framework_basic(
sentry_init, client, capture_events, capture_exceptions, type, event_request
sentry_init, client, capture_events, capture_exceptions, type, event_request, route
):
pytest.importorskip("rest_framework")
sentry_init(integrations=[DjangoIntegration()], send_default_pii=True)
Expand All @@ -460,12 +463,12 @@ def test_rest_framework_basic(

if type == "json":
client.post(
reverse("rest_framework_exc"),
reverse(route),
data=json.dumps({"foo": "bar"}),
content_type="application/json",
)
elif type == "formdata":
client.post(reverse("rest_framework_exc"), data={"foo": "bar"})
client.post(reverse(route), data={"foo": "bar"})
else:
assert False

Expand All @@ -475,4 +478,4 @@ def test_rest_framework_basic(
event, = events
assert event["exception"]["values"][0]["mechanism"]["type"] == "django"

assert event["request"] == event_request
assert event["request"] == event_request(route)