Skip to content

Commit 5ddc159

Browse files
committed
Fix byes in LogRecord bug
1 parent a05a651 commit 5ddc159

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

opentelemetry-exporter-gcp-logging/src/opentelemetry/exporter/cloud_logging/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,18 @@ def _convert_any_value_to_string(value: Any) -> str:
117117
return ""
118118

119119

120+
def _convert_bytes_to_b64(body: Mapping) -> Mapping:
121+
for key, value in list(body.items()):
122+
if isinstance(value, bytes):
123+
body[key] = base64.b64encode(value).decode()
124+
elif isinstance(value, Mapping):
125+
body[key] = _convert_bytes_to_b64(value)
126+
return body
127+
120128
def _set_payload_in_log_entry(log_entry: LogEntry, body: Any | None):
121129
struct = Struct()
122130
if isinstance(body, Mapping):
123-
struct.update(body)
131+
struct.update(_convert_bytes_to_b64(body))
124132
log_entry.json_payload = struct
125133
elif isinstance(body, bytes):
126134
json_str = body.decode("utf-8", errors="replace")

opentelemetry-exporter-gcp-logging/tests/__snapshots__/test_cloud_logging/test_convert_otlp_dict_body.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
{
55
"jsonPayload": {
66
"kvlistValue": {
7+
"bytes_field": "Ynl0ZXM=",
78
"values": [
89
{
910
"key": "content",

opentelemetry-exporter-gcp-logging/tests/test_cloud_logging.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ def test_convert_otlp_dict_body(
9292
"stringValue": "You're a helpful assistant."
9393
},
9494
}
95-
]
95+
],
96+
"bytes_field": b'bytes'
9697
}
9798
},
9899
),

0 commit comments

Comments
 (0)