File tree Expand file tree Collapse file tree 2 files changed +12
-2
lines changed
src/opentelemetry/sdk/_logs/_internal Expand file tree Collapse file tree 2 files changed +12
-2
lines changed Original file line number Diff line number Diff line change 2020import logging
2121import threading
2222import traceback
23+ import base64
2324import warnings
2425from os import environ
2526from threading import Lock
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
6471class 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
Original file line number Diff line number Diff line change 2222 LogDroppedAttributesWarning ,
2323 LogLimits ,
2424 LogRecord ,
25+ BytesEncoder ,
2526)
2627from 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" },
You can’t perform that action at this time.
0 commit comments