Skip to content

Commit 692d099

Browse files
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 <[email protected]>
1 parent 4f1f782 commit 692d099

File tree

9 files changed

+19
-14
lines changed

9 files changed

+19
-14
lines changed

sentry_sdk/consts.py

+6
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ class SPANDATA:
101101
Example: GET
102102
"""
103103

104+
HTTP_STATUS_CODE = "http.response.status_code"
105+
"""
106+
The HTTP status code as an integer.
107+
Example: 418
108+
"""
109+
104110

105111
class OP:
106112
CACHE_GET_ITEM = "cache.get_item"

sentry_sdk/integrations/httpx.py

-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ def send(self, request, **kwargs):
6464

6565
rv = real_send(self, request, **kwargs)
6666

67-
span.set_data("status_code", rv.status_code)
6867
span.set_http_status(rv.status_code)
6968
span.set_data("reason", rv.reason_phrase)
7069

@@ -105,7 +104,6 @@ async def send(self, request, **kwargs):
105104

106105
rv = await real_send(self, request, **kwargs)
107106

108-
span.set_data("status_code", rv.status_code)
109107
span.set_http_status(rv.status_code)
110108
span.set_data("reason", rv.reason_phrase)
111109

sentry_sdk/integrations/stdlib.py

-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ def getresponse(self, *args, **kwargs):
120120

121121
rv = real_getresponse(self, *args, **kwargs)
122122

123-
span.set_data("status_code", rv.status)
124123
span.set_http_status(int(rv.status))
125124
span.set_data("reason", rv.reason)
126125
span.finish()

sentry_sdk/tracing.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from sentry_sdk.consts import INSTRUMENTER
88
from sentry_sdk.utils import is_valid_sample_rate, logger, nanosecond_time
99
from sentry_sdk._compat import PY2
10+
from sentry_sdk.consts import SPANDATA
1011
from sentry_sdk._types import TYPE_CHECKING
1112

1213

@@ -370,7 +371,10 @@ def set_status(self, value):
370371

371372
def set_http_status(self, http_status):
372373
# type: (int) -> None
373-
self.set_tag("http.status_code", str(http_status))
374+
self.set_tag(
375+
"http.status_code", str(http_status)
376+
) # we keep this for backwards compatability
377+
self.set_data(SPANDATA.HTTP_STATUS_CODE, http_status)
374378

375379
if http_status < 400:
376380
self.set_status("ok")

tests/integrations/httpx/test_httpx.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def before_breadcrumb(crumb, hint):
4646
SPANDATA.HTTP_METHOD: "GET",
4747
SPANDATA.HTTP_FRAGMENT: "",
4848
SPANDATA.HTTP_QUERY: "",
49-
"status_code": 200,
49+
SPANDATA.HTTP_STATUS_CODE: 200,
5050
"reason": "OK",
5151
"extra": "foo",
5252
}

tests/integrations/opentelemetry/test_span_processor.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,10 @@ def test_update_span_with_otel_data_http_method():
190190

191191
assert sentry_span.op == "http.client"
192192
assert sentry_span.description == "GET example.com /"
193-
assert sentry_span._tags["http.status_code"] == "429"
194193
assert sentry_span.status == "resource_exhausted"
195194

196195
assert sentry_span._data["http.method"] == "GET"
197-
assert sentry_span._data["http.status_code"] == 429
196+
assert sentry_span._data["http.response.status_code"] == 429
198197
assert sentry_span._data["http.status_text"] == "xxx"
199198
assert sentry_span._data["http.user_agent"] == "curl/7.64.1"
200199
assert sentry_span._data["net.peer.name"] == "example.com"
@@ -220,11 +219,10 @@ def test_update_span_with_otel_data_http_method2():
220219

221220
assert sentry_span.op == "http.server"
222221
assert sentry_span.description == "GET https://example.com/status/403"
223-
assert sentry_span._tags["http.status_code"] == "429"
224222
assert sentry_span.status == "resource_exhausted"
225223

226224
assert sentry_span._data["http.method"] == "GET"
227-
assert sentry_span._data["http.status_code"] == 429
225+
assert sentry_span._data["http.response.status_code"] == 429
228226
assert sentry_span._data["http.status_text"] == "xxx"
229227
assert sentry_span._data["http.user_agent"] == "curl/7.64.1"
230228
assert (

tests/integrations/requests/test_requests.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ def test_crumb_capture(sentry_init, capture_events):
2828
SPANDATA.HTTP_METHOD: "GET",
2929
SPANDATA.HTTP_FRAGMENT: "",
3030
SPANDATA.HTTP_QUERY: "",
31-
"status_code": response.status_code,
31+
SPANDATA.HTTP_STATUS_CODE: response.status_code,
3232
"reason": response.reason,
3333
}

tests/integrations/stdlib/test_httplib.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def test_crumb_capture(sentry_init, capture_events):
4949
assert crumb["data"] == {
5050
"url": url,
5151
SPANDATA.HTTP_METHOD: "GET",
52-
"status_code": 200,
52+
SPANDATA.HTTP_STATUS_CODE: 200,
5353
"reason": "OK",
5454
SPANDATA.HTTP_FRAGMENT: "",
5555
SPANDATA.HTTP_QUERY: "",
@@ -76,7 +76,7 @@ def before_breadcrumb(crumb, hint):
7676
assert crumb["data"] == {
7777
"url": url,
7878
SPANDATA.HTTP_METHOD: "GET",
79-
"status_code": 200,
79+
SPANDATA.HTTP_STATUS_CODE: 200,
8080
"reason": "OK",
8181
"extra": "foo",
8282
SPANDATA.HTTP_FRAGMENT: "",
@@ -134,7 +134,7 @@ def test_httplib_misuse(sentry_init, capture_events, request):
134134
assert crumb["data"] == {
135135
"url": "http://localhost:{}/200".format(PORT),
136136
SPANDATA.HTTP_METHOD: "GET",
137-
"status_code": 200,
137+
SPANDATA.HTTP_STATUS_CODE: 200,
138138
"reason": "OK",
139139
SPANDATA.HTTP_FRAGMENT: "",
140140
SPANDATA.HTTP_QUERY: "",

tests/tracing/test_noop_span.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def test_noop_start_span(sentry_init):
2727
assert isinstance(span, NoOpSpan)
2828
assert sentry_sdk.Hub.current.scope.span is span
2929

30-
span.set_tag("http.status_code", "418")
30+
span.set_tag("http.response.status_code", 418)
3131
span.set_data("http.entity_type", "teapot")
3232

3333

0 commit comments

Comments
 (0)