Skip to content

Commit b037b80

Browse files
committed
Respond to comments
1 parent a483967 commit b037b80

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,19 +117,29 @@ def _convert_any_value_to_string(value: Any) -> str:
117117
return ""
118118

119119

120-
def _convert_bytes_to_b64(body: MutableMapping) -> MutableMapping:
120+
def _convert_bytes_to_b64(body: Mapping, new_body: dict) -> dict:
121121
for key, value in list(body.items()):
122-
if isinstance(value, bytes):
123-
body[key] = base64.b64encode(value).decode()
122+
if (
123+
isinstance(value, Sequence)
124+
and len(value) > 0
125+
and isinstance(value[0], bytes)
126+
):
127+
new_body[key] = [base64.b64encode(v).decode() for v in value]
128+
elif isinstance(value, bytes):
129+
new_body[key] = base64.b64encode(value).decode()
124130
elif isinstance(value, MutableMapping):
125-
body[key] = _convert_bytes_to_b64(value)
126-
return body
131+
new_subbody = {}
132+
new_body[key] = _convert_bytes_to_b64(value, new_subbody)
133+
else:
134+
new_body[key] = value
135+
return new_body
127136

128137

129138
def _set_payload_in_log_entry(log_entry: LogEntry, body: Any | None):
130139
struct = Struct()
131-
if isinstance(body, MutableMapping):
132-
struct.update(_convert_bytes_to_b64(body))
140+
if isinstance(body, Mapping):
141+
new_body = {}
142+
struct.update(_convert_bytes_to_b64(body, new_body))
133143
log_entry.json_payload = struct
134144
elif isinstance(body, bytes):
135145
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
"jsonPayload": {
66
"kvlistValue": {
77
"bytes_field": "Ynl0ZXM=",
8+
"repeated_bytes_field": [
9+
"Ynl0ZXM=",
10+
"Ynl0ZXM=",
11+
"Ynl0ZXM="
12+
],
813
"values": [
914
{
1015
"key": "content",

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def test_convert_otlp_dict_body(
9494
}
9595
],
9696
"bytes_field": b"bytes",
97+
"repeated_bytes_field": [b"bytes", b"bytes", b"bytes"],
9798
}
9899
},
99100
),

0 commit comments

Comments
 (0)