Skip to content

Commit

Permalink
Align HTTP status code as span data field http.response.status_code (
Browse files Browse the repository at this point in the history
…#2113)

* Save http status code everywhere in same format

---------

Co-authored-by: Ivana Kellyerova <ivana.kellyerova@sentry.io>
  • Loading branch information
antonpirker and sentrivana authored Jun 6, 2023
1 parent 4f1f782 commit 692d099
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 14 deletions.
6 changes: 6 additions & 0 deletions sentry_sdk/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 0 additions & 2 deletions sentry_sdk/integrations/httpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
1 change: 0 additions & 1 deletion sentry_sdk/integrations/stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 5 additions & 1 deletion sentry_sdk/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion tests/integrations/httpx/test_httpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
Expand Down
6 changes: 2 additions & 4 deletions tests/integrations/opentelemetry/test_span_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion tests/integrations/requests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
6 changes: 3 additions & 3 deletions tests/integrations/stdlib/test_httplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
Expand All @@ -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: "",
Expand Down Expand Up @@ -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: "",
Expand Down
2 changes: 1 addition & 1 deletion tests/tracing/test_noop_span.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")


Expand Down

0 comments on commit 692d099

Please sign in to comment.