Handle null statuses in http_status_to_status_code (#823)

This commit is contained in:
Greg Buehler
2022-02-02 10:53:43 -08:00
committed by GitHub
parent e69030e94b
commit 0e553419c5
3 changed files with 12 additions and 0 deletions

View File

@ -64,6 +64,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed ### Fixed
- `opentelemetry-instrumentation-urllib` Fixed an error on unexpected status values.
([#823](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/823))
- `opentelemetry-exporter-richconsole` Fixed attribute error on parentless spans. - `opentelemetry-exporter-richconsole` Fixed attribute error on parentless spans.
([#782](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/782)) ([#782](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/782))

View File

@ -49,6 +49,9 @@ def http_status_to_status_code(
status (int): HTTP status code status (int): HTTP status code
""" """
# See: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#status # See: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#status
if not isinstance(status, int):
return StatusCode.UNSET
if status < 100: if status < 100:
return StatusCode.ERROR return StatusCode.ERROR
if status <= 299: if status <= 299:

View File

@ -56,6 +56,12 @@ class TestUtils(TestBase):
actual = http_status_to_status_code(int(status_code)) actual = http_status_to_status_code(int(status_code))
self.assertEqual(actual, expected, status_code) self.assertEqual(actual, expected, status_code)
def test_http_status_to_status_code_none(self):
for status_code, expected in ((None, StatusCode.UNSET),):
with self.subTest(status_code=status_code):
actual = http_status_to_status_code(status_code)
self.assertEqual(actual, expected, status_code)
def test_http_status_to_status_code_redirect(self): def test_http_status_to_status_code_redirect(self):
for status_code, expected in ( for status_code, expected in (
(HTTPStatus.MULTIPLE_CHOICES, StatusCode.ERROR), (HTTPStatus.MULTIPLE_CHOICES, StatusCode.ERROR),