Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align HTTP status code as span data field http.response.status_code #2113

Merged
merged 18 commits into from Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions sentry_sdk/consts.py
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
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
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
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
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
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
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
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
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