Skip to content

Commit

Permalink
Set HTTP client breadcrumb level based on status code (#4090)
Browse files Browse the repository at this point in the history
On potel-base, we got rid of the `maybe_create_breadcrumbs` function in
favor of creating breadcrumbs manually, so the new breadcrumb level
logic needs to go directly in the affected integrations.

Closes #4066
  • Loading branch information
sentrivana authored Feb 26, 2025
1 parent f0664f7 commit 9313c69
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 34 deletions.
5 changes: 4 additions & 1 deletion sentry_sdk/integrations/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
capture_internal_exceptions,
ensure_integration_enabled,
event_from_exception,
http_client_status_to_breadcrumb_level,
logger,
parse_url,
parse_version,
Expand Down Expand Up @@ -277,13 +278,15 @@ async def on_request_end(session, trace_config_ctx, params):
return

span_data = trace_config_ctx.span_data or {}
span_data[SPANDATA.HTTP_STATUS_CODE] = int(params.response.status)
status_code = int(params.response.status)
span_data[SPANDATA.HTTP_STATUS_CODE] = status_code
span_data["reason"] = params.response.reason

sentry_sdk.add_breadcrumb(
type="http",
category="httplib",
data=span_data,
level=http_client_status_to_breadcrumb_level(status_code),
)

span = trace_config_ctx.span
Expand Down
2 changes: 2 additions & 0 deletions sentry_sdk/integrations/httpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
SENSITIVE_DATA_SUBSTITUTE,
capture_internal_exceptions,
ensure_integration_enabled,
http_client_status_to_breadcrumb_level,
logger,
parse_url,
)
Expand Down Expand Up @@ -101,6 +102,7 @@ def send(self, request, **kwargs):
type="http",
category="httplib",
data=data,
level=http_client_status_to_breadcrumb_level(rv.status_code),
)

return rv
Expand Down
9 changes: 6 additions & 3 deletions sentry_sdk/integrations/stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
capture_internal_exceptions,
ensure_integration_enabled,
get_current_thread_meta,
http_client_status_to_breadcrumb_level,
is_sentry_url,
logger,
safe_repr,
Expand Down Expand Up @@ -144,14 +145,16 @@ def getresponse(self, *args, **kwargs):
span_data[SPANDATA.HTTP_STATUS_CODE] = int(rv.status)
span_data["reason"] = rv.reason

status_code = int(rv.status)
span.set_http_status(status_code)
span.set_data("reason", rv.reason)

sentry_sdk.add_breadcrumb(
type="http",
category="httplib",
data=span_data,
level=http_client_status_to_breadcrumb_level(status_code),
)

span.set_http_status(int(rv.status))
span.set_data("reason", rv.reason)
finally:
span.__exit__(None, None, None)

Expand Down
11 changes: 11 additions & 0 deletions sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1886,3 +1886,14 @@ def should_be_treated_as_error(ty, value):
return False

return True


def http_client_status_to_breadcrumb_level(status_code):
# type: (Optional[int]) -> str
if status_code is not None:
if 500 <= status_code <= 599:
return "error"
elif 400 <= status_code <= 499:
return "warning"

return "info"
9 changes: 3 additions & 6 deletions tests/integrations/aiohttp/test_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,8 @@ async def handler(request):
@pytest.mark.parametrize(
"status_code,level",
[
(200, None),
(301, None),
(200, "info"),
(301, "info"),
(403, "warning"),
(405, "warning"),
(500, "error"),
Expand Down Expand Up @@ -570,10 +570,7 @@ async def handler(request):

crumb = event["breadcrumbs"]["values"][0]
assert crumb["type"] == "http"
if level is None:
assert "level" not in crumb
else:
assert crumb["level"] == level
assert crumb["level"] == level
assert crumb["category"] == "httplib"
assert crumb["data"] == ApproxDict(
{
Expand Down
11 changes: 3 additions & 8 deletions tests/integrations/httpx/test_httpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ def before_breadcrumb(crumb, hint):
@pytest.mark.parametrize(
"status_code,level",
[
(200, None),
(301, None),
(200, "info"),
(301, "info"),
(403, "warning"),
(405, "warning"),
(500, "error"),
Expand Down Expand Up @@ -98,12 +98,7 @@ def test_crumb_capture_client_error(
crumb = event["breadcrumbs"]["values"][0]
assert crumb["type"] == "http"
assert crumb["category"] == "httplib"

if level is None:
assert "level" not in crumb
else:
assert crumb["level"] == level

assert crumb["level"] == level
assert crumb["data"] == ApproxDict(
{
"url": url,
Expand Down
11 changes: 3 additions & 8 deletions tests/integrations/requests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def test_crumb_capture(sentry_init, capture_events):
@pytest.mark.parametrize(
"status_code,level",
[
(200, None),
(301, None),
(200, "info"),
(301, "info"),
(403, "warning"),
(405, "warning"),
(500, "error"),
Expand All @@ -66,12 +66,7 @@ def test_crumb_capture_client_error(sentry_init, capture_events, status_code, le
(crumb,) = event["breadcrumbs"]["values"]
assert crumb["type"] == "http"
assert crumb["category"] == "httplib"

if level is None:
assert "level" not in crumb
else:
assert crumb["level"] == level

assert crumb["level"] == level
assert crumb["data"] == ApproxDict(
{
"url": url,
Expand Down
11 changes: 3 additions & 8 deletions tests/integrations/stdlib/test_httplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def test_crumb_capture(sentry_init, capture_events):
@pytest.mark.parametrize(
"status_code,level",
[
(200, None),
(301, None),
(200, "info"),
(301, "info"),
(403, "warning"),
(405, "warning"),
(500, "error"),
Expand All @@ -94,12 +94,7 @@ def test_crumb_capture_client_error(sentry_init, capture_events, status_code, le

assert crumb["type"] == "http"
assert crumb["category"] == "httplib"

if level is None:
assert "level" not in crumb
else:
assert crumb["level"] == level

assert crumb["level"] == level
assert crumb["data"] == ApproxDict(
{
"url": url,
Expand Down

0 comments on commit 9313c69

Please sign in to comment.