Skip to content

Commit 76a5cda

Browse files
authored
Don't set STATUS on SpanKind SERVER for 4XX status (#776)
1 parent dbc6a86 commit 76a5cda

File tree

8 files changed

+56
-9
lines changed
  • instrumentation
    • opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi
    • opentelemetry-instrumentation-falcon
    • opentelemetry-instrumentation-tornado/src/opentelemetry/instrumentation/tornado
    • opentelemetry-instrumentation-wsgi/src/opentelemetry/instrumentation/wsgi
  • opentelemetry-instrumentation

8 files changed

+56
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
([#774](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/774))
2020
- `opentelemetry-instrumentation-django` Fixed carrier usage on ASGI requests.
2121
([#767](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/767))
22+
- Don't set Span Status on 4xx http status code for SpanKind.SERVER spans
23+
([#776](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/776))
2224

2325
## [1.6.2-0.25b2](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.6.2-0.25b2) - 2021-10-19
2426

instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,9 @@ def set_status_code(span, status_code):
218218
)
219219
else:
220220
span.set_attribute(SpanAttributes.HTTP_STATUS_CODE, status_code)
221-
span.set_status(Status(http_status_to_status_code(status_code)))
221+
span.set_status(
222+
Status(http_status_to_status_code(status_code, server_span=True))
223+
)
222224

223225

224226
def get_default_span_details(scope: dict) -> Tuple[str, dict]:

instrumentation/opentelemetry-instrumentation-falcon/src/opentelemetry/instrumentation/falcon/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,9 @@ def process_response(
299299
span.set_attribute(SpanAttributes.HTTP_STATUS_CODE, status_code)
300300
span.set_status(
301301
Status(
302-
status_code=http_status_to_status_code(status_code),
302+
status_code=http_status_to_status_code(
303+
status_code, server_span=True
304+
),
303305
description=reason,
304306
)
305307
)

instrumentation/opentelemetry-instrumentation-falcon/tests/test_falcon.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,7 @@ def test_404(self):
114114
self.assertEqual(len(spans), 1)
115115
span = spans[0]
116116
self.assertEqual(span.name, "HTTP GET")
117-
self.assertEqual(span.status.status_code, StatusCode.ERROR)
118-
self.assertEqual(
119-
span.status.description, "NotFound",
120-
)
117+
self.assertEqual(span.status.status_code, StatusCode.UNSET)
121118
self.assertSpanHasAttributes(
122119
span,
123120
{

instrumentation/opentelemetry-instrumentation-tornado/src/opentelemetry/instrumentation/tornado/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,9 @@ def _finish_span(tracer, handler, error=None):
345345

346346
if ctx.span.is_recording():
347347
ctx.span.set_attribute(SpanAttributes.HTTP_STATUS_CODE, status_code)
348-
otel_status_code = http_status_to_status_code(status_code)
348+
otel_status_code = http_status_to_status_code(
349+
status_code, server_span=True
350+
)
349351
otel_status_description = None
350352
if otel_status_code is StatusCode.ERROR:
351353
otel_status_description = reason

instrumentation/opentelemetry-instrumentation-wsgi/src/opentelemetry/instrumentation/wsgi/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,9 @@ def add_response_attributes(
226226
)
227227
else:
228228
span.set_attribute(SpanAttributes.HTTP_STATUS_CODE, status_code)
229-
span.set_status(Status(http_status_to_status_code(status_code)))
229+
span.set_status(
230+
Status(http_status_to_status_code(status_code, server_span=True))
231+
)
230232

231233

232234
def get_default_span_name(environ):

opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def extract_attributes_from_object(
3636

3737

3838
def http_status_to_status_code(
39-
status: int, allow_redirect: bool = True
39+
status: int, allow_redirect: bool = True, server_span: bool = False,
4040
) -> StatusCode:
4141
"""Converts an HTTP status code to an OpenTelemetry canonical status code
4242
@@ -50,6 +50,8 @@ def http_status_to_status_code(
5050
return StatusCode.UNSET
5151
if status <= 399 and allow_redirect:
5252
return StatusCode.UNSET
53+
if status <= 499 and server_span:
54+
return StatusCode.UNSET
5355
return StatusCode.ERROR
5456

5557

opentelemetry-instrumentation/tests/test_utils.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,41 @@ def test_http_status_to_status_code(self):
4343
with self.subTest(status_code=status_code):
4444
actual = http_status_to_status_code(int(status_code))
4545
self.assertEqual(actual, expected, status_code)
46+
47+
def test_http_status_to_status_code_redirect(self):
48+
for status_code, expected in (
49+
(HTTPStatus.MULTIPLE_CHOICES, StatusCode.ERROR),
50+
(HTTPStatus.MOVED_PERMANENTLY, StatusCode.ERROR),
51+
(HTTPStatus.TEMPORARY_REDIRECT, StatusCode.ERROR),
52+
(HTTPStatus.PERMANENT_REDIRECT, StatusCode.ERROR),
53+
):
54+
with self.subTest(status_code=status_code):
55+
actual = http_status_to_status_code(
56+
int(status_code), allow_redirect=False
57+
)
58+
self.assertEqual(actual, expected, status_code)
59+
60+
def test_http_status_to_status_code_server(self):
61+
for status_code, expected in (
62+
(HTTPStatus.OK, StatusCode.UNSET),
63+
(HTTPStatus.ACCEPTED, StatusCode.UNSET),
64+
(HTTPStatus.IM_USED, StatusCode.UNSET),
65+
(HTTPStatus.MULTIPLE_CHOICES, StatusCode.UNSET),
66+
(HTTPStatus.BAD_REQUEST, StatusCode.UNSET),
67+
(HTTPStatus.UNAUTHORIZED, StatusCode.UNSET),
68+
(HTTPStatus.FORBIDDEN, StatusCode.UNSET),
69+
(HTTPStatus.NOT_FOUND, StatusCode.UNSET),
70+
(HTTPStatus.UNPROCESSABLE_ENTITY, StatusCode.UNSET,),
71+
(HTTPStatus.TOO_MANY_REQUESTS, StatusCode.UNSET,),
72+
(HTTPStatus.NOT_IMPLEMENTED, StatusCode.ERROR),
73+
(HTTPStatus.SERVICE_UNAVAILABLE, StatusCode.ERROR),
74+
(HTTPStatus.GATEWAY_TIMEOUT, StatusCode.ERROR,),
75+
(HTTPStatus.HTTP_VERSION_NOT_SUPPORTED, StatusCode.ERROR,),
76+
(600, StatusCode.ERROR),
77+
(99, StatusCode.ERROR),
78+
):
79+
with self.subTest(status_code=status_code):
80+
actual = http_status_to_status_code(
81+
int(status_code), server_span=True
82+
)
83+
self.assertEqual(actual, expected, status_code)

0 commit comments

Comments
 (0)