Skip to content

Commit 0e55341

Browse files
authored
Handle null statuses in http_status_to_status_code (#823)
1 parent e69030e commit 0e55341

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6464

6565
### Fixed
6666

67+
- `opentelemetry-instrumentation-urllib` Fixed an error on unexpected status values.
68+
([#823](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/823))
69+
6770
- `opentelemetry-exporter-richconsole` Fixed attribute error on parentless spans.
6871
([#782](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/782))
6972

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

+3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ def http_status_to_status_code(
4949
status (int): HTTP status code
5050
"""
5151
# See: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#status
52+
if not isinstance(status, int):
53+
return StatusCode.UNSET
54+
5255
if status < 100:
5356
return StatusCode.ERROR
5457
if status <= 299:

opentelemetry-instrumentation/tests/test_utils.py

+6
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ def test_http_status_to_status_code(self):
5656
actual = http_status_to_status_code(int(status_code))
5757
self.assertEqual(actual, expected, status_code)
5858

59+
def test_http_status_to_status_code_none(self):
60+
for status_code, expected in ((None, StatusCode.UNSET),):
61+
with self.subTest(status_code=status_code):
62+
actual = http_status_to_status_code(status_code)
63+
self.assertEqual(actual, expected, status_code)
64+
5965
def test_http_status_to_status_code_redirect(self):
6066
for status_code, expected in (
6167
(HTTPStatus.MULTIPLE_CHOICES, StatusCode.ERROR),

0 commit comments

Comments
 (0)