Skip to content

Commit 9313c69

Browse files
authored
Set HTTP client breadcrumb level based on status code (#4090)
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
1 parent f0664f7 commit 9313c69

File tree

8 files changed

+35
-34
lines changed

8 files changed

+35
-34
lines changed

sentry_sdk/integrations/aiohttp.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
capture_internal_exceptions,
2828
ensure_integration_enabled,
2929
event_from_exception,
30+
http_client_status_to_breadcrumb_level,
3031
logger,
3132
parse_url,
3233
parse_version,
@@ -277,13 +278,15 @@ async def on_request_end(session, trace_config_ctx, params):
277278
return
278279

279280
span_data = trace_config_ctx.span_data or {}
280-
span_data[SPANDATA.HTTP_STATUS_CODE] = int(params.response.status)
281+
status_code = int(params.response.status)
282+
span_data[SPANDATA.HTTP_STATUS_CODE] = status_code
281283
span_data["reason"] = params.response.reason
282284

283285
sentry_sdk.add_breadcrumb(
284286
type="http",
285287
category="httplib",
286288
data=span_data,
289+
level=http_client_status_to_breadcrumb_level(status_code),
287290
)
288291

289292
span = trace_config_ctx.span

sentry_sdk/integrations/httpx.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
SENSITIVE_DATA_SUBSTITUTE,
88
capture_internal_exceptions,
99
ensure_integration_enabled,
10+
http_client_status_to_breadcrumb_level,
1011
logger,
1112
parse_url,
1213
)
@@ -101,6 +102,7 @@ def send(self, request, **kwargs):
101102
type="http",
102103
category="httplib",
103104
data=data,
105+
level=http_client_status_to_breadcrumb_level(rv.status_code),
104106
)
105107

106108
return rv

sentry_sdk/integrations/stdlib.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
capture_internal_exceptions,
1515
ensure_integration_enabled,
1616
get_current_thread_meta,
17+
http_client_status_to_breadcrumb_level,
1718
is_sentry_url,
1819
logger,
1920
safe_repr,
@@ -144,14 +145,16 @@ def getresponse(self, *args, **kwargs):
144145
span_data[SPANDATA.HTTP_STATUS_CODE] = int(rv.status)
145146
span_data["reason"] = rv.reason
146147

148+
status_code = int(rv.status)
149+
span.set_http_status(status_code)
150+
span.set_data("reason", rv.reason)
151+
147152
sentry_sdk.add_breadcrumb(
148153
type="http",
149154
category="httplib",
150155
data=span_data,
156+
level=http_client_status_to_breadcrumb_level(status_code),
151157
)
152-
153-
span.set_http_status(int(rv.status))
154-
span.set_data("reason", rv.reason)
155158
finally:
156159
span.__exit__(None, None, None)
157160

sentry_sdk/utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,3 +1886,14 @@ def should_be_treated_as_error(ty, value):
18861886
return False
18871887

18881888
return True
1889+
1890+
1891+
def http_client_status_to_breadcrumb_level(status_code):
1892+
# type: (Optional[int]) -> str
1893+
if status_code is not None:
1894+
if 500 <= status_code <= 599:
1895+
return "error"
1896+
elif 400 <= status_code <= 499:
1897+
return "warning"
1898+
1899+
return "info"

tests/integrations/aiohttp/test_aiohttp.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -534,8 +534,8 @@ async def handler(request):
534534
@pytest.mark.parametrize(
535535
"status_code,level",
536536
[
537-
(200, None),
538-
(301, None),
537+
(200, "info"),
538+
(301, "info"),
539539
(403, "warning"),
540540
(405, "warning"),
541541
(500, "error"),
@@ -570,10 +570,7 @@ async def handler(request):
570570

571571
crumb = event["breadcrumbs"]["values"][0]
572572
assert crumb["type"] == "http"
573-
if level is None:
574-
assert "level" not in crumb
575-
else:
576-
assert crumb["level"] == level
573+
assert crumb["level"] == level
577574
assert crumb["category"] == "httplib"
578575
assert crumb["data"] == ApproxDict(
579576
{

tests/integrations/httpx/test_httpx.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ def before_breadcrumb(crumb, hint):
6464
@pytest.mark.parametrize(
6565
"status_code,level",
6666
[
67-
(200, None),
68-
(301, None),
67+
(200, "info"),
68+
(301, "info"),
6969
(403, "warning"),
7070
(405, "warning"),
7171
(500, "error"),
@@ -98,12 +98,7 @@ def test_crumb_capture_client_error(
9898
crumb = event["breadcrumbs"]["values"][0]
9999
assert crumb["type"] == "http"
100100
assert crumb["category"] == "httplib"
101-
102-
if level is None:
103-
assert "level" not in crumb
104-
else:
105-
assert crumb["level"] == level
106-
101+
assert crumb["level"] == level
107102
assert crumb["data"] == ApproxDict(
108103
{
109104
"url": url,

tests/integrations/requests/test_requests.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ def test_crumb_capture(sentry_init, capture_events):
4343
@pytest.mark.parametrize(
4444
"status_code,level",
4545
[
46-
(200, None),
47-
(301, None),
46+
(200, "info"),
47+
(301, "info"),
4848
(403, "warning"),
4949
(405, "warning"),
5050
(500, "error"),
@@ -66,12 +66,7 @@ def test_crumb_capture_client_error(sentry_init, capture_events, status_code, le
6666
(crumb,) = event["breadcrumbs"]["values"]
6767
assert crumb["type"] == "http"
6868
assert crumb["category"] == "httplib"
69-
70-
if level is None:
71-
assert "level" not in crumb
72-
else:
73-
assert crumb["level"] == level
74-
69+
assert crumb["level"] == level
7570
assert crumb["data"] == ApproxDict(
7671
{
7772
"url": url,

tests/integrations/stdlib/test_httplib.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ def test_crumb_capture(sentry_init, capture_events):
7070
@pytest.mark.parametrize(
7171
"status_code,level",
7272
[
73-
(200, None),
74-
(301, None),
73+
(200, "info"),
74+
(301, "info"),
7575
(403, "warning"),
7676
(405, "warning"),
7777
(500, "error"),
@@ -94,12 +94,7 @@ def test_crumb_capture_client_error(sentry_init, capture_events, status_code, le
9494

9595
assert crumb["type"] == "http"
9696
assert crumb["category"] == "httplib"
97-
98-
if level is None:
99-
assert "level" not in crumb
100-
else:
101-
assert crumb["level"] == level
102-
97+
assert crumb["level"] == level
10398
assert crumb["data"] == ApproxDict(
10499
{
105100
"url": url,

0 commit comments

Comments
 (0)