Skip to content

Commit

Permalink
opentelemetry-sdk: serialize logs severity_number as int (#4324)
Browse files Browse the repository at this point in the history
* opentelemetry-sdk: serialize logs severity_number as int

Instead of a string.

* Update changelog
  • Loading branch information
xrmx authored Nov 26, 2024
1 parent a0304a0 commit 6812da2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 15 additions & 2 deletions opentelemetry-sdk/tests/logs/test_log_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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"}

Expand Down

0 comments on commit 6812da2

Please sign in to comment.