diff --git a/CHANGELOG.md b/CHANGELOG.md index b8fb2bd3f4..ea5f2483ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#4270](https://github.com/open-telemetry/opentelemetry-python/pull/4270)) - api: fix logging of duplicate EventLogger setup warning ([#4299](https://github.com/open-telemetry/opentelemetry-python/pull/4299)) +- sdk: fix serialization of logs severity_number field to int + ([#4324](https://github.com/open-telemetry/opentelemetry-python/pull/4324)) ## Version 1.28.0/0.49b0 (2024-11-05) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py index 110bb89c99..c2db81687a 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py @@ -221,7 +221,9 @@ def to_json(self, indent=4) -> str: return json.dumps( { "body": self.body, - "severity_number": repr(self.severity_number), + "severity_number": self.severity_number.value + if self.severity_number is not None + else None, "severity_text": self.severity_text, "attributes": ( dict(self.attributes) if bool(self.attributes) else None diff --git a/opentelemetry-sdk/tests/logs/test_log_record.py b/opentelemetry-sdk/tests/logs/test_log_record.py index 9c3746989b..f42d3a26ea 100644 --- a/opentelemetry-sdk/tests/logs/test_log_record.py +++ b/opentelemetry-sdk/tests/logs/test_log_record.py @@ -16,6 +16,7 @@ import unittest import warnings +from opentelemetry._logs.severity import SeverityNumber from opentelemetry.attributes import BoundedAttributes from opentelemetry.sdk._logs import ( LogDroppedAttributesWarning, @@ -30,7 +31,7 @@ def test_log_record_to_json(self): expected = json.dumps( { "body": "a log line", - "severity_number": "None", + "severity_number": None, "severity_text": None, "attributes": None, "dropped_attributes": 0, @@ -56,9 +57,21 @@ def test_log_record_to_json(self): self.assertEqual(expected, actual.to_json(indent=4)) self.assertEqual( actual.to_json(indent=None), - '{"body": "a log line", "severity_number": "None", "severity_text": null, "attributes": null, "dropped_attributes": 0, "timestamp": "1970-01-01T00:00:00.000000Z", "observed_timestamp": "1970-01-01T00:00:00.000000Z", "trace_id": "", "span_id": "", "trace_flags": null, "resource": {"attributes": {"service.name": "foo"}, "schema_url": ""}}', + '{"body": "a log line", "severity_number": null, "severity_text": null, "attributes": null, "dropped_attributes": 0, "timestamp": "1970-01-01T00:00:00.000000Z", "observed_timestamp": "1970-01-01T00:00:00.000000Z", "trace_id": "", "span_id": "", "trace_flags": null, "resource": {"attributes": {"service.name": "foo"}, "schema_url": ""}}', ) + def test_log_record_to_json_serializes_severity_number_as_int(self): + actual = LogRecord( + timestamp=0, + severity_number=SeverityNumber.WARN, + observed_timestamp=0, + body="a log line", + resource=Resource({"service.name": "foo"}), + ) + + decoded = json.loads(actual.to_json()) + self.assertEqual(SeverityNumber.WARN.value, decoded["severity_number"]) + def test_log_record_bounded_attributes(self): attr = {"key": "value"}