Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 37de27a

Browse files
committed
LogRecord Resource Serialization
Currently, the `LogRecord.to_json` method serializes the resource object using `repr` of its attributes. This differs from how the serialization process is handled in `ReadableSpan.to_json` and `MetricsData.to_json`, which utilize the `Resource.to_json` functionality directly. Using `repr` does not produce a json-parseable output and doesn't follow the same depth of serialization as the other two signal types. Therefore, this change carries over the serialization process from the spans and metrics signal types to the logs type. Fixes open-telemetry#3345
1 parent adbcf82 commit 37de27a

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
- Add unit to view instrument selection criteria
1818
([#3341](https://github.com/open-telemetry/opentelemetry-python/pull/3341))
1919
- Upgrade opentelemetry-proto to 0.20 and regen
20-
[#3355](https://github.com/open-telemetry/opentelemetry-python/pull/3355))
20+
([#3355](https://github.com/open-telemetry/opentelemetry-python/pull/3355))
21+
- LogRecord now JSON serializes resource objects to match ReadableSpan and MetricsData equivalents
22+
([#3345](https://github.com/open-telemetry/opentelemetry-python/issues/3345))
2123

2224
## Version 1.18.0/0.39b0 (2023-05-04)
2325

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,9 @@ def to_json(self, indent=4) -> str:
212212
if self.span_id is not None
213213
else "",
214214
"trace_flags": self.trace_flags,
215-
"resource": repr(self.resource.attributes)
215+
"resource": json.loads(self.resource.to_json())
216216
if self.resource
217-
else "",
217+
else None,
218218
},
219219
indent=indent,
220220
)

opentelemetry-sdk/tests/logs/test_log_record.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
from opentelemetry.attributes import BoundedAttributes
1919
from opentelemetry.sdk._logs import LogLimits, LogRecord
20+
from opentelemetry.sdk.resources import Resource
2021

2122

2223
class TestLogRecord(unittest.TestCase):
@@ -32,7 +33,7 @@ def test_log_record_to_json(self):
3233
"trace_id": "",
3334
"span_id": "",
3435
"trace_flags": None,
35-
"resource": "",
36+
"resource": None,
3637
},
3738
indent=4,
3839
)
@@ -42,6 +43,30 @@ def test_log_record_to_json(self):
4243
).to_json()
4344
self.assertEqual(expected, actual)
4445

46+
def test_log_record_to_json_with_resource(self):
47+
"""Should JSON serialize/deserialize Resource objects within log records."""
48+
expected = json.dumps(
49+
{
50+
"body": "a log line",
51+
"severity_number": "None",
52+
"severity_text": None,
53+
"attributes": None,
54+
"timestamp": "1970-01-01T00:00:00.000000Z",
55+
"trace_id": "",
56+
"span_id": "",
57+
"trace_flags": None,
58+
"resource": {"attributes": {"foo": "bar"}, "schema_url": ""},
59+
},
60+
indent=4,
61+
)
62+
63+
actual = LogRecord(
64+
timestamp=0,
65+
body="a log line",
66+
resource=Resource(attributes={"foo": "bar"}),
67+
).to_json()
68+
self.assertEqual(expected, actual)
69+
4570
def test_log_record_bounded_attributes(self):
4671
attr = {"key": "value"}
4772

0 commit comments

Comments
 (0)