|
14 | 14 | limitations under the License.
|
15 | 15 | """
|
16 | 16 | import asyncio
|
| 17 | +import logging |
17 | 18 | import types
|
18 | 19 | from collections import deque
|
19 | 20 |
|
@@ -176,17 +177,24 @@ async def on_request_chunk_sent(
|
176 | 177 | params: aiohttp.TraceRequestChunkSentParams,
|
177 | 178 | ):
|
178 | 179 | if getattr(params, "chunk"):
|
179 |
| - decoded_chunk = params.chunk.decode() |
180 |
| - trace_config_ctx.request_body += decoded_chunk |
| 180 | + trace_config_ctx.request_body += _decode_chunk(params.chunk) |
181 | 181 |
|
182 | 182 | async def on_responde_chunk_received(
|
183 | 183 | unused_session: aiohttp.ClientSession,
|
184 | 184 | trace_config_ctx: types.SimpleNamespace,
|
185 | 185 | params: aiohttp.TraceRequestChunkSentParams,
|
186 | 186 | ):
|
187 | 187 | if getattr(params, "chunk"):
|
188 |
| - decoded_chunk = params.chunk.decode() |
189 |
| - trace_config_ctx.response_body += decoded_chunk |
| 188 | + trace_config_ctx.response_body += _decode_chunk(params.chunk) |
| 189 | + |
| 190 | + def _decode_chunk(chunk): |
| 191 | + try: |
| 192 | + decoded_chunk = chunk.decode() |
| 193 | + except UnicodeDecodeError as e: |
| 194 | + decoded_chunk = str(chunk) |
| 195 | + logging.info(f"Could not decode body chunk: {decoded_chunk}, {e}") |
| 196 | + |
| 197 | + return decoded_chunk |
190 | 198 |
|
191 | 199 | def _trace_config_ctx_factory(**kwargs):
|
192 | 200 | kwargs.setdefault("trace_request_ctx", {})
|
|
0 commit comments