-
Notifications
You must be signed in to change notification settings - Fork 648
feat(django): Add failed_request_status_codes
#7140
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1761,6 +1761,96 @@ def test_does_not_capture_403( | |
| assert not events | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("integration_kwargs", "endpoint", "status", "expected_type"), | ||
| ( | ||
| # Django only turns exceptions into 4xx responses, so with the default | ||
| # (the 5xx range) none of them are reported | ||
| ({}, "permission_denied_exc", "403 forbidden", None), | ||
| ({}, "http404_exc", "404 not found", None), | ||
| ( | ||
| {"failed_request_status_codes": set()}, | ||
| "permission_denied_exc", | ||
| "403 forbidden", | ||
| None, | ||
| ), | ||
| ( | ||
| {"failed_request_status_codes": {403, *range(500, 600)}}, | ||
| "permission_denied_exc", | ||
| "403 forbidden", | ||
| "PermissionDenied", | ||
| ), | ||
| ( | ||
| {"failed_request_status_codes": {404, *range(500, 600)}}, | ||
| "http404_exc", | ||
| "404 not found", | ||
| "Http404", | ||
| ), | ||
| # Only the status codes that were opted into are reported | ||
| ( | ||
| {"failed_request_status_codes": {403}}, | ||
| "http404_exc", | ||
| "404 not found", | ||
| None, | ||
| ), | ||
| ), | ||
| ) | ||
| def test_failed_request_status_codes( | ||
| sentry_init, | ||
| client, | ||
| capture_events, | ||
| integration_kwargs, | ||
| endpoint, | ||
| status, | ||
| expected_type, | ||
| ): | ||
| sentry_init(integrations=[DjangoIntegration(**integration_kwargs)]) | ||
| events = capture_events() | ||
|
|
||
| _, response_status, _ = unpack_werkzeug_response(client.get(reverse(endpoint))) | ||
| assert response_status.lower() == status | ||
|
|
||
| # The test app's handler404 captures a message, ignore it here | ||
| error_events = [event for event in events if "exception" in event] | ||
|
|
||
| if expected_type is None: | ||
| assert not error_events | ||
| else: | ||
| (event,) = error_events | ||
| (exception,) = event["exception"]["values"] | ||
| assert exception["type"] == expected_type | ||
| assert exception["mechanism"]["type"] == "django" | ||
| assert exception["mechanism"]["handled"] is True | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "integration_kwargs", | ||
| ( | ||
| {}, | ||
| {"failed_request_status_codes": set()}, | ||
| {"failed_request_status_codes": {404}}, | ||
| ), | ||
| ) | ||
| def test_failed_request_status_codes_unhandled_exception( | ||
| sentry_init, client, capture_events, integration_kwargs | ||
| ): | ||
| """ | ||
| Exceptions Django gives up on are always reported, exactly once, no matter how | ||
| failed_request_status_codes is set. | ||
| """ | ||
| sentry_init(integrations=[DjangoIntegration(**integration_kwargs)]) | ||
| events = capture_events() | ||
|
|
||
| _, status, _ = unpack_werkzeug_response(client.get(reverse("view_exc"))) | ||
| assert status.lower() == "500 internal server error" | ||
|
|
||
| (event,) = events | ||
| (exception,) = event["exception"]["values"] | ||
| assert exception["type"] == "ZeroDivisionError" | ||
| assert exception["mechanism"]["type"] == "django" | ||
| assert exception["mechanism"]["handled"] is False | ||
|
Comment on lines
+1826
to
+1851
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Although I don't imagine there are many cases where someone would want to do this, if a user provides an empty set for This will mean we need some additional changes to Once that's done, we can look to remove this test case in favour of another parameterized case to the test you've added above ( |
||
|
|
||
|
|
||
| @pytest.mark.parametrize("span_streaming", [True, False]) | ||
| def test_render_spans( | ||
| sentry_init, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can tighten this typing a bit by copying what we do within the
event_from_exceptionmethod that's being invoked within this function:This requires updating the import from
sentry_sdk._typesat the top to importExcInfo: