Skip to content

Commit 4bfd7b1

Browse files
committed
Add tests, respond to comments on review.
1 parent 08a01de commit 4bfd7b1

File tree

7 files changed

+47
-15
lines changed

7 files changed

+47
-15
lines changed

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

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import json
1818
import logging
1919
import urllib.parse
20-
from typing import Any, Optional, Sequence
20+
from typing import Any, Mapping, Optional, Sequence
2121

2222
import google.auth
2323
from google.api.monitored_resource_pb2 import MonitoredResource # type: ignore
@@ -28,6 +28,7 @@
2828
LoggingServiceV2GrpcTransport,
2929
)
3030
from google.cloud.logging_v2.types.log_entry import LogEntry
31+
from google.cloud.logging_v2.types.logging import WriteLogEntriesRequest
3132
from google.logging.type.log_severity_pb2 import LogSeverity # type: ignore
3233
from google.protobuf.struct_pb2 import Struct
3334
from google.protobuf.timestamp_pb2 import Timestamp
@@ -98,7 +99,7 @@ def convert_any_value_to_string(value: Any) -> str:
9899
return str(value)
99100
if t is list or t is tuple:
100101
return json.dumps(value)
101-
logging.warning(f"Unknown type {t} found, cannot convert to string.")
102+
logging.warning("Unknown type %s found, cannot convert to string.", t)
102103
return ""
103104

104105

@@ -170,7 +171,10 @@ def export(self, batch: Sequence[LogData]):
170171
log_entry.log_name = log_name
171172
if monitored_resource:
172173
log_entry.resource = monitored_resource
173-
log_entry.trace_sampled = bool(log_record.trace_flags)
174+
log_entry.trace_sampled = (
175+
log_record.trace_flags is not None
176+
and log_record.trace_flags.sampled
177+
)
174178
if log_record.trace_id:
175179
log_entry.trace = f"projects/{project_id}/traces/{format_trace_id(log_record.trace_id)}"
176180
if log_record.span_id:
@@ -186,20 +190,21 @@ def export(self, batch: Sequence[LogData]):
186190
k: convert_any_value_to_string(v)
187191
for k, v in attributes.items()
188192
}
189-
if type(log_record.body) is dict:
193+
if isinstance(log_record.body, Mapping):
190194
s = Struct()
191195
s.update(log_record.body)
192196
log_entry.json_payload = s
193197
elif type(log_record.body) is bytes:
194198
json_str = log_record.body.decode("utf8")
195199
json_dict = json.loads(json_str)
196-
if type(json_dict) is dict:
200+
if isinstance(json_dict, Mapping):
197201
s = Struct()
198202
s.update(json_dict)
199203
log_entry.json_payload = s
200204
else:
201205
logging.warning(
202-
f"LogRecord.body was bytes type and json.loads turned body into type {type(json_dict)}, expected a dictionary."
206+
"LogRecord.body was bytes type and json.loads turned body into type %s, expected a dictionary.",
207+
type(json_dict),
203208
)
204209
else:
205210
log_entry.text_payload = convert_any_value_to_string(
@@ -216,18 +221,38 @@ def _write_log_entries(self, log_entries: list[LogEntry]):
216221
msg_size = LogEntry.pb(entry).ByteSize()
217222
if msg_size > DEFAULT_MAX_ENTRY_SIZE:
218223
logging.warning(
219-
f"Cannot write log that is {msg_size} bytes which exceeds Cloud Logging's maximum limit of {DEFAULT_MAX_ENTRY_SIZE}."
224+
"Cannot write log that is %s bytes which exceeds Cloud Logging's maximum limit of %s bytes.",
225+
msg_size,
226+
DEFAULT_MAX_ENTRY_SIZE,
220227
)
221228
continue
222229
if msg_size + batch_byte_size > DEFAULT_MAX_REQUEST_SIZE:
223-
self.client.write_log_entries(entries=batch)
230+
try:
231+
self.client.write_log_entries(
232+
WriteLogEntriesRequest(
233+
entries=batch, partial_success=True
234+
)
235+
)
236+
# pylint: disable=broad-except
237+
except Exception as ex:
238+
logging.error(
239+
"Error while writing to Cloud Logging", exc_info=ex
240+
)
224241
batch = [entry]
225242
batch_byte_size = msg_size
226243
else:
227244
batch.append(entry)
228245
batch_byte_size += msg_size
229246
if batch:
230-
self.client.write_log_entries(entries=batch)
247+
try:
248+
self.client.write_log_entries(
249+
WriteLogEntriesRequest(entries=batch, partial_success=True)
250+
)
251+
# pylint: disable=broad-except
252+
except Exception as ex:
253+
logging.error(
254+
"Error while writing to Cloud Logging", exc_info=ex
255+
)
231256

232257
def shutdown(self):
233258
pass

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
},
1414
"timestamp": "2025-01-15T21:25:10.997977393Z"
1515
}
16-
]
16+
],
17+
"partialSuccess": true
1718
}
1819
]

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"timestamp": "2025-01-15T21:25:10.997977393Z",
3333
"trace": "projects/fakeproject/traces/00000000000000000000000000000019"
3434
}
35-
]
35+
],
36+
"partialSuccess": true
3637
}
3738
]

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
},
3131
"timestamp": "2025-01-15T21:25:10.997977393Z"
3232
}
33-
]
33+
],
34+
"partialSuccess": true
3435
}
3536
]

opentelemetry-exporter-gcp-logging/tests/__snapshots__/test_cloud_logging/test_convert_various_types_of_bodies[bool].json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"textPayload": "True",
1515
"timestamp": "2025-01-15T21:25:10.997977393Z"
1616
}
17-
]
17+
],
18+
"partialSuccess": true
1819
}
1920
]

opentelemetry-exporter-gcp-logging/tests/__snapshots__/test_cloud_logging/test_convert_various_types_of_bodies[str].json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"textPayload": "A text body",
1515
"timestamp": "2025-01-15T21:25:10.997977393Z"
1616
}
17-
]
17+
],
18+
"partialSuccess": true
1819
}
1920
]

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ def test_too_large_log_raises_warning(caplog) -> None:
6262
]
6363
)
6464
assert len(caplog.records) == 1
65-
assert "exceeds Cloud Logging's maximum limit of 256000.\n" in caplog.text
65+
assert (
66+
"exceeds Cloud Logging's maximum limit of 256000 bytes" in caplog.text
67+
)
6668

6769

6870
def test_convert_otlp_dict_body(

0 commit comments

Comments
 (0)