|
102 | 102 | } |
103 | 103 |
|
104 | 104 |
|
105 | | -def convert_any_value_to_string(value: Any) -> str: |
| 105 | +def _convert_any_value_to_string(value: Any) -> str: |
106 | 106 | if isinstance(value, bool): |
107 | 107 | return "true" if value else "false" |
108 | 108 | if isinstance(value, bytes): |
109 | 109 | 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)): |
115 | 111 | return str(value) |
116 | | - if isinstance(value, list) or isinstance(value, tuple): |
| 112 | + if isinstance(value, (list, tuple)): |
117 | 113 | return json.dumps(value) |
118 | 114 | logging.warning( |
119 | 115 | "Unknown value %s found, cannot convert to string.", type(value) |
120 | 116 | ) |
121 | 117 | return "" |
122 | 118 |
|
123 | 119 |
|
| 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 | + |
124 | 137 | class CloudLoggingExporter(LogExporter): |
125 | 138 | def __init__( |
126 | 139 | self, |
@@ -198,30 +211,14 @@ def export(self, batch: Sequence[LogData]): |
198 | 211 | log_record.severity_number.value # type: ignore[index] |
199 | 212 | ] |
200 | 213 | log_entry.labels = { |
201 | | - k: convert_any_value_to_string(v) |
| 214 | + k: _convert_any_value_to_string(v) |
202 | 215 | for k, v in attributes.items() |
203 | 216 | } |
204 | | - self._set_payload_in_log_entry(log_entry, log_record.body) |
| 217 | + _set_payload_in_log_entry(log_entry, log_record.body) |
205 | 218 | log_entries.append(log_entry) |
206 | 219 |
|
207 | 220 | self._write_log_entries(log_entries) |
208 | 221 |
|
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 | | - |
225 | 222 | def _write_log_entries(self, log_entries: list[LogEntry]): |
226 | 223 | batch: list[LogEntry] = [] |
227 | 224 | batch_byte_size = 0 |
|
0 commit comments