From 692d0990e060af0970eda6ae301a8d73250f138e Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Tue, 6 Jun 2023 10:11:27 +0200 Subject: [PATCH] Align HTTP status code as span data field `http.response.status_code` (#2113) * Save http status code everywhere in same format --------- Co-authored-by: Ivana Kellyerova --- sentry_sdk/consts.py | 6 ++++++ sentry_sdk/integrations/httpx.py | 2 -- sentry_sdk/integrations/stdlib.py | 1 - sentry_sdk/tracing.py | 6 +++++- tests/integrations/httpx/test_httpx.py | 2 +- tests/integrations/opentelemetry/test_span_processor.py | 6 ++---- tests/integrations/requests/test_requests.py | 2 +- tests/integrations/stdlib/test_httplib.py | 6 +++--- tests/tracing/test_noop_span.py | 2 +- 9 files changed, 19 insertions(+), 14 deletions(-) diff --git a/sentry_sdk/consts.py b/sentry_sdk/consts.py index 524d8e0571..0fc94686ea 100644 --- a/sentry_sdk/consts.py +++ b/sentry_sdk/consts.py @@ -101,6 +101,12 @@ class SPANDATA: Example: GET """ + HTTP_STATUS_CODE = "http.response.status_code" + """ + The HTTP status code as an integer. + Example: 418 + """ + class OP: CACHE_GET_ITEM = "cache.get_item" diff --git a/sentry_sdk/integrations/httpx.py b/sentry_sdk/integrations/httpx.py index a7319d9d72..358562f791 100644 --- a/sentry_sdk/integrations/httpx.py +++ b/sentry_sdk/integrations/httpx.py @@ -64,7 +64,6 @@ def send(self, request, **kwargs): rv = real_send(self, request, **kwargs) - span.set_data("status_code", rv.status_code) span.set_http_status(rv.status_code) span.set_data("reason", rv.reason_phrase) @@ -105,7 +104,6 @@ async def send(self, request, **kwargs): rv = await real_send(self, request, **kwargs) - span.set_data("status_code", rv.status_code) span.set_http_status(rv.status_code) span.set_data("reason", rv.reason_phrase) diff --git a/sentry_sdk/integrations/stdlib.py b/sentry_sdk/integrations/stdlib.py index 17b30102b9..0add046bf8 100644 --- a/sentry_sdk/integrations/stdlib.py +++ b/sentry_sdk/integrations/stdlib.py @@ -120,7 +120,6 @@ def getresponse(self, *args, **kwargs): rv = real_getresponse(self, *args, **kwargs) - span.set_data("status_code", rv.status) span.set_http_status(int(rv.status)) span.set_data("reason", rv.reason) span.finish() diff --git a/sentry_sdk/tracing.py b/sentry_sdk/tracing.py index 35d77ae46e..97c3277b65 100644 --- a/sentry_sdk/tracing.py +++ b/sentry_sdk/tracing.py @@ -7,6 +7,7 @@ from sentry_sdk.consts import INSTRUMENTER from sentry_sdk.utils import is_valid_sample_rate, logger, nanosecond_time from sentry_sdk._compat import PY2 +from sentry_sdk.consts import SPANDATA from sentry_sdk._types import TYPE_CHECKING @@ -370,7 +371,10 @@ def set_status(self, value): def set_http_status(self, http_status): # type: (int) -> None - self.set_tag("http.status_code", str(http_status)) + self.set_tag( + "http.status_code", str(http_status) + ) # we keep this for backwards compatability + self.set_data(SPANDATA.HTTP_STATUS_CODE, http_status) if http_status < 400: self.set_status("ok") diff --git a/tests/integrations/httpx/test_httpx.py b/tests/integrations/httpx/test_httpx.py index dd5e752c32..c948901588 100644 --- a/tests/integrations/httpx/test_httpx.py +++ b/tests/integrations/httpx/test_httpx.py @@ -46,7 +46,7 @@ def before_breadcrumb(crumb, hint): SPANDATA.HTTP_METHOD: "GET", SPANDATA.HTTP_FRAGMENT: "", SPANDATA.HTTP_QUERY: "", - "status_code": 200, + SPANDATA.HTTP_STATUS_CODE: 200, "reason": "OK", "extra": "foo", } diff --git a/tests/integrations/opentelemetry/test_span_processor.py b/tests/integrations/opentelemetry/test_span_processor.py index 0467da7673..8659e548a1 100644 --- a/tests/integrations/opentelemetry/test_span_processor.py +++ b/tests/integrations/opentelemetry/test_span_processor.py @@ -190,11 +190,10 @@ def test_update_span_with_otel_data_http_method(): assert sentry_span.op == "http.client" assert sentry_span.description == "GET example.com /" - assert sentry_span._tags["http.status_code"] == "429" assert sentry_span.status == "resource_exhausted" assert sentry_span._data["http.method"] == "GET" - assert sentry_span._data["http.status_code"] == 429 + assert sentry_span._data["http.response.status_code"] == 429 assert sentry_span._data["http.status_text"] == "xxx" assert sentry_span._data["http.user_agent"] == "curl/7.64.1" assert sentry_span._data["net.peer.name"] == "example.com" @@ -220,11 +219,10 @@ def test_update_span_with_otel_data_http_method2(): assert sentry_span.op == "http.server" assert sentry_span.description == "GET https://example.com/status/403" - assert sentry_span._tags["http.status_code"] == "429" assert sentry_span.status == "resource_exhausted" assert sentry_span._data["http.method"] == "GET" - assert sentry_span._data["http.status_code"] == 429 + assert sentry_span._data["http.response.status_code"] == 429 assert sentry_span._data["http.status_text"] == "xxx" assert sentry_span._data["http.user_agent"] == "curl/7.64.1" assert ( diff --git a/tests/integrations/requests/test_requests.py b/tests/integrations/requests/test_requests.py index 324379fc9d..9c77b290d1 100644 --- a/tests/integrations/requests/test_requests.py +++ b/tests/integrations/requests/test_requests.py @@ -28,6 +28,6 @@ def test_crumb_capture(sentry_init, capture_events): SPANDATA.HTTP_METHOD: "GET", SPANDATA.HTTP_FRAGMENT: "", SPANDATA.HTTP_QUERY: "", - "status_code": response.status_code, + SPANDATA.HTTP_STATUS_CODE: response.status_code, "reason": response.reason, } diff --git a/tests/integrations/stdlib/test_httplib.py b/tests/integrations/stdlib/test_httplib.py index 959ad1658b..769d3dfef5 100644 --- a/tests/integrations/stdlib/test_httplib.py +++ b/tests/integrations/stdlib/test_httplib.py @@ -49,7 +49,7 @@ def test_crumb_capture(sentry_init, capture_events): assert crumb["data"] == { "url": url, SPANDATA.HTTP_METHOD: "GET", - "status_code": 200, + SPANDATA.HTTP_STATUS_CODE: 200, "reason": "OK", SPANDATA.HTTP_FRAGMENT: "", SPANDATA.HTTP_QUERY: "", @@ -76,7 +76,7 @@ def before_breadcrumb(crumb, hint): assert crumb["data"] == { "url": url, SPANDATA.HTTP_METHOD: "GET", - "status_code": 200, + SPANDATA.HTTP_STATUS_CODE: 200, "reason": "OK", "extra": "foo", SPANDATA.HTTP_FRAGMENT: "", @@ -134,7 +134,7 @@ def test_httplib_misuse(sentry_init, capture_events, request): assert crumb["data"] == { "url": "http://localhost:{}/200".format(PORT), SPANDATA.HTTP_METHOD: "GET", - "status_code": 200, + SPANDATA.HTTP_STATUS_CODE: 200, "reason": "OK", SPANDATA.HTTP_FRAGMENT: "", SPANDATA.HTTP_QUERY: "", diff --git a/tests/tracing/test_noop_span.py b/tests/tracing/test_noop_span.py index 92cba75a35..9896afb007 100644 --- a/tests/tracing/test_noop_span.py +++ b/tests/tracing/test_noop_span.py @@ -27,7 +27,7 @@ def test_noop_start_span(sentry_init): assert isinstance(span, NoOpSpan) assert sentry_sdk.Hub.current.scope.span is span - span.set_tag("http.status_code", "418") + span.set_tag("http.response.status_code", 418) span.set_data("http.entity_type", "teapot")