Skip to content

Commit b6ea53a

Browse files
committed
opentelemetry-sdk: handle none timestamp in LogRecord serialization
1 parent d6c0441 commit b6ea53a

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,9 @@ def to_json(self, indent: int | None = 4) -> str:
306306
dict(self.attributes) if bool(self.attributes) else None
307307
),
308308
"dropped_attributes": self.dropped_attributes,
309-
"timestamp": ns_to_iso_str(self.timestamp),
309+
"timestamp": ns_to_iso_str(self.timestamp)
310+
if self.timestamp is not None
311+
else None,
310312
"observed_timestamp": ns_to_iso_str(self.observed_timestamp),
311313
"trace_id": (
312314
f"0x{format_trace_id(self.trace_id)}"

opentelemetry-sdk/tests/logs/test_log_record.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ def test_log_record_to_json_serializes_severity_number_as_int(self):
6262
decoded = json.loads(actual.to_json())
6363
self.assertEqual(SeverityNumber.WARN.value, decoded["severity_number"])
6464

65+
def test_log_record_to_json_serializes_null_severity_number(self):
66+
actual = LogRecord(
67+
observed_timestamp=0,
68+
body="a log line",
69+
resource=Resource({"service.name": "foo"}),
70+
)
71+
72+
decoded = json.loads(actual.to_json())
73+
self.assertEqual(None, decoded["timestamp"])
74+
6575
def test_log_record_bounded_attributes(self):
6676
attr = {"key": "value"}
6777

0 commit comments

Comments
 (0)