Skip to content

Commit 5b8d79c

Browse files
committed
fix console log exporter
1 parent 12bcd45 commit 5b8d79c

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import logging
2121
import threading
2222
import traceback
23+
import base64
2324
import warnings
2425
from os import environ
2526
from threading import Lock
@@ -60,6 +61,12 @@
6061
_DEFAULT_OTEL_ATTRIBUTE_COUNT_LIMIT = 128
6162
_ENV_VALUE_UNSET = ""
6263

64+
class BytesEncoder(json.JSONEncoder):
65+
def default(self, o):
66+
if isinstance(o, bytes):
67+
return base64.b64encode(o).decode()
68+
return super().default(o)
69+
6370

6471
class LogDroppedAttributesWarning(UserWarning):
6572
"""Custom warning to indicate dropped log attributes due to limits.
@@ -248,6 +255,7 @@ def to_json(self, indent: int | None = 4) -> str:
248255
"resource": json.loads(self.resource.to_json()),
249256
},
250257
indent=indent,
258+
cls=BytesEncoder,
251259
)
252260

253261
@property

opentelemetry-sdk/tests/logs/test_log_record.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
LogDroppedAttributesWarning,
2323
LogLimits,
2424
LogRecord,
25+
BytesEncoder,
2526
)
2627
from opentelemetry.sdk.resources import Resource
2728

@@ -30,7 +31,7 @@ class TestLogRecord(unittest.TestCase):
3031
def test_log_record_to_json(self):
3132
expected = json.dumps(
3233
{
33-
"body": "a log line",
34+
"body": {'key': 'logLine', 'bytes': b'123'},
3435
"severity_number": None,
3536
"severity_text": None,
3637
"attributes": {
@@ -51,11 +52,12 @@ def test_log_record_to_json(self):
5152
},
5253
},
5354
indent=4,
55+
cls=BytesEncoder,
5456
)
5557
actual = LogRecord(
5658
timestamp=0,
5759
observed_timestamp=0,
58-
body="a log line",
60+
body={'key': 'logLine', 'bytes': b'123'},
5961
resource=Resource({"service.name": "foo"}),
6062
attributes={
6163
"mapping": {"key": "value"},

0 commit comments

Comments
 (0)