Skip to content

Commit 2e80574

Browse files
committed
Fix lint checks.
1 parent ab18b30 commit 2e80574

File tree

1 file changed

+22
-25
lines changed
  • opentelemetry-exporter-gcp-logging/src/opentelemetry/exporter/cloud_logging

1 file changed

+22
-25
lines changed

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

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -102,25 +102,38 @@
102102
}
103103

104104

105-
def convert_any_value_to_string(value: Any) -> str:
105+
def _convert_any_value_to_string(value: Any) -> str:
106106
if isinstance(value, bool):
107107
return "true" if value else "false"
108108
if isinstance(value, bytes):
109109
return base64.b64encode(value).decode()
110-
if (
111-
isinstance(value, int)
112-
or isinstance(value, float)
113-
or isinstance(value, str)
114-
):
110+
if isinstance(value, (int, float, str)):
115111
return str(value)
116-
if isinstance(value, list) or isinstance(value, tuple):
112+
if isinstance(value, (list, tuple)):
117113
return json.dumps(value)
118114
logging.warning(
119115
"Unknown value %s found, cannot convert to string.", type(value)
120116
)
121117
return ""
122118

123119

120+
def _set_payload_in_log_entry(log_entry: LogEntry, body: Any | None):
121+
struct = Struct()
122+
if isinstance(body, Mapping):
123+
struct.update(body)
124+
log_entry.json_payload = struct
125+
elif isinstance(body, bytes):
126+
json_str = body.decode("utf-8", errors="replace")
127+
json_dict = json.loads(json_str)
128+
if isinstance(json_dict, Mapping):
129+
struct.update(json_dict)
130+
log_entry.json_payload = struct
131+
else:
132+
log_entry.text_payload = base64.b64encode(body).decode()
133+
else:
134+
log_entry.text_payload = _convert_any_value_to_string(body)
135+
136+
124137
class CloudLoggingExporter(LogExporter):
125138
def __init__(
126139
self,
@@ -198,30 +211,14 @@ def export(self, batch: Sequence[LogData]):
198211
log_record.severity_number.value # type: ignore[index]
199212
]
200213
log_entry.labels = {
201-
k: convert_any_value_to_string(v)
214+
k: _convert_any_value_to_string(v)
202215
for k, v in attributes.items()
203216
}
204-
self._set_payload_in_log_entry(log_entry, log_record.body)
217+
_set_payload_in_log_entry(log_entry, log_record.body)
205218
log_entries.append(log_entry)
206219

207220
self._write_log_entries(log_entries)
208221

209-
def _set_payload_in_log_entry(self, log_entry: LogEntry, body: Any | None):
210-
struct = Struct()
211-
if isinstance(body, Mapping):
212-
struct.update(body)
213-
log_entry.json_payload = struct
214-
elif isinstance(body, bytes):
215-
json_str = body.decode("utf-8", errors="replace")
216-
json_dict = json.loads(json_str)
217-
if isinstance(json_dict, Mapping):
218-
struct.update(json_dict)
219-
log_entry.json_payload = struct
220-
else:
221-
log_entry.text_payload = base64.b64encode(body).decode()
222-
else:
223-
log_entry.text_payload = convert_any_value_to_string(body)
224-
225222
def _write_log_entries(self, log_entries: list[LogEntry]):
226223
batch: list[LogEntry] = []
227224
batch_byte_size = 0

0 commit comments

Comments
 (0)