Skip to content

Commit 8618228

Browse files
committed
Address comments
1 parent a65eaf2 commit 8618228

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

dev-constraints.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ setuptools==69.5.1
1717

1818
# pinned for snapshot tests. this should be bumped regularly and snapshots updated by running
1919
# tox -f py311-test -- --snapshot-update
20-
opentelemetry-api==1.20.0
21-
opentelemetry-sdk==1.20.0
20+
opentelemetry-api==1.30.0
21+
opentelemetry-sdk==1.30.0

opentelemetry-exporter-gcp-logging/setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ packages=find_namespace:
2929
install_requires =
3030
google-cloud-logging ~= 3.0
3131
opentelemetry-sdk ~= 1.0
32+
opentelemetry-api ~= 1.2
3233
opentelemetry-resourcedetector-gcp >= 1.5.0dev0, == 1.*
3334

3435
[options.packages.find]

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
from opentelemetry.sdk._logs.export import LogExporter
5151
from opentelemetry.sdk.resources import Resource
5252
from opentelemetry.trace import format_span_id, format_trace_id
53+
from opentelemetry.util.types import AnyValue
5354

5455
DEFAULT_MAX_ENTRY_SIZE = 256000 # 256 KB
5556
DEFAULT_MAX_REQUEST_SIZE = 10000000 # 10 MB
@@ -117,19 +118,28 @@ def _convert_any_value_to_string(value: Any) -> str:
117118
return ""
118119

119120

120-
def _convert_bytes_to_b64(body: Mapping, new_body: dict) -> dict:
121+
# Be careful not to mutate original body. Make copies of anything that needs to change.
122+
def _sanitized_body(
123+
body: Mapping[str, AnyValue]
124+
) -> MutableMapping[str, AnyValue]:
125+
new_body: MutableMapping[str, AnyValue] = {}
121126
for key, value in body.items():
122127
if (
123128
isinstance(value, Sequence)
124129
and len(value) > 0
125130
and isinstance(value[0], bytes)
126131
):
127-
new_body[key] = [base64.b64encode(v).decode() for v in value]
132+
# Should not be possible for a non-bytes value to be present. AnyValue requires Sequence be of one type, and above
133+
# we verified the first value is type bytes.
134+
new_body[key] = [
135+
base64.b64encode(v).decode()
136+
for v in value
137+
if isinstance(v, bytes)
138+
]
128139
elif isinstance(value, bytes):
129140
new_body[key] = base64.b64encode(value).decode()
130-
elif isinstance(value, MutableMapping):
131-
new_subbody = {}
132-
new_body[key] = _convert_bytes_to_b64(value, new_subbody)
141+
elif isinstance(value, Mapping):
142+
new_body[key] = _sanitized_body(value)
133143
else:
134144
new_body[key] = value
135145
return new_body
@@ -138,8 +148,7 @@ def _convert_bytes_to_b64(body: Mapping, new_body: dict) -> dict:
138148
def _set_payload_in_log_entry(log_entry: LogEntry, body: AnyValue):
139149
struct = Struct()
140150
if isinstance(body, Mapping):
141-
new_body = {}
142-
struct.update(_convert_bytes_to_b64(body, new_body))
151+
struct.update(_sanitized_body(body))
143152
log_entry.json_payload = struct
144153
elif isinstance(body, bytes):
145154
json_str = body.decode("utf-8", errors="replace")

tox.ini

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ changedir = {env:PACKAGE_NAME}
7575
commands_pre =
7676
{toxinidir}/get_mock_server.sh {envbindir}
7777

78-
commands = pytest --junitxml={[constants]test_results_dir}/{envname}/junit.xml {posargs}
78+
commands =
79+
pip freeze
80+
pytest --junitxml={[constants]test_results_dir}/{envname}/junit.xml {posargs}
7981

8082
allowlist_externals =
8183
bash

0 commit comments

Comments
 (0)