Skip to content

Commit 9fffba4

Browse files
authored
Update LogRecord.to_json to handle bytes field in the body (#4614)
* fix console log exporter * Fix console exporter * Add changelog * Remove BytesEncoder import
1 parent 347bfae commit 9fffba4

File tree

3 files changed

+15
-29
lines changed

3 files changed

+15
-29
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
- Update ConsoleLogExporter.export to handle LogRecord's containing bytes type
11+
in the body ([#4614](https://github.com/open-telemetry/opentelemetry-python/pull/4614/)).
1012
- opentelemetry-sdk: Fix invalid `type: ignore` that causes mypy to ignore the whole file
1113
([#4618](https://github.com/open-telemetry/opentelemetry-python/pull/4618))
1214
- Add `span_exporter` property back to `BatchSpanProcessor` class

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import abc
1717
import atexit
18+
import base64
1819
import concurrent.futures
1920
import json
2021
import logging
@@ -61,6 +62,13 @@
6162
_ENV_VALUE_UNSET = ""
6263

6364

65+
class BytesEncoder(json.JSONEncoder):
66+
def default(self, o):
67+
if isinstance(o, bytes):
68+
return base64.b64encode(o).decode()
69+
return super().default(o)
70+
71+
6472
class LogDroppedAttributesWarning(UserWarning):
6573
"""Custom warning to indicate dropped log attributes due to limits.
6674
@@ -248,6 +256,7 @@ def to_json(self, indent: int | None = 4) -> str:
248256
"resource": json.loads(self.resource.to_json()),
249257
},
250258
indent=indent,
259+
cls=BytesEncoder,
251260
)
252261

253262
@property

opentelemetry-sdk/tests/logs/test_log_record.py

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,34 +28,10 @@
2828

2929
class TestLogRecord(unittest.TestCase):
3030
def test_log_record_to_json(self):
31-
expected = json.dumps(
32-
{
33-
"body": "a log line",
34-
"severity_number": None,
35-
"severity_text": None,
36-
"attributes": {
37-
"mapping": {"key": "value"},
38-
"none": None,
39-
"sequence": [1, 2],
40-
"str": "string",
41-
},
42-
"dropped_attributes": 0,
43-
"timestamp": "1970-01-01T00:00:00.000000Z",
44-
"observed_timestamp": "1970-01-01T00:00:00.000000Z",
45-
"trace_id": "",
46-
"span_id": "",
47-
"trace_flags": None,
48-
"resource": {
49-
"attributes": {"service.name": "foo"},
50-
"schema_url": "",
51-
},
52-
},
53-
indent=4,
54-
)
55-
actual = LogRecord(
31+
log_record = LogRecord(
5632
timestamp=0,
5733
observed_timestamp=0,
58-
body="a log line",
34+
body={"key": "logLine", "bytes": b"123"},
5935
resource=Resource({"service.name": "foo"}),
6036
attributes={
6137
"mapping": {"key": "value"},
@@ -65,10 +41,9 @@ def test_log_record_to_json(self):
6541
},
6642
)
6743

68-
self.assertEqual(expected, actual.to_json(indent=4))
6944
self.assertEqual(
70-
actual.to_json(indent=None),
71-
'{"body": "a log line", "severity_number": null, "severity_text": null, "attributes": {"mapping": {"key": "value"}, "none": null, "sequence": [1, 2], "str": "string"}, "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": ""}}',
45+
log_record.to_json(indent=None),
46+
'{"body": {"key": "logLine", "bytes": "MTIz"}, "severity_number": null, "severity_text": null, "attributes": {"mapping": {"key": "value"}, "none": null, "sequence": [1, 2], "str": "string"}, "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": ""}}',
7247
)
7348

7449
def test_log_record_to_json_serializes_severity_number_as_int(self):

0 commit comments

Comments
 (0)