|
15 | 15 |
|
16 | 16 | """Provides an httpx.AsyncTransport that handles retrying requests on failure.""" |
17 | 17 |
|
| 18 | +import time |
18 | 19 | from collections.abc import Callable |
19 | 20 | from logging import getLogger |
20 | 21 | from types import TracebackType |
@@ -55,17 +56,28 @@ def _log_retry_stats(retry_state: RetryCallState): |
55 | 56 | function_name = retry_state.fn.__qualname__ |
56 | 57 | attempt_number = retry_state.attempt_number |
57 | 58 |
|
58 | | - retry_stats = {"function_name": function_name} |
59 | | - |
60 | | - # Additionally get internal statistics from the current retry object |
61 | | - internal_stats = retry_state.retry_object.statistics |
62 | | - retry_stats |= internal_stats |
| 59 | + # Get internal statistics from the current retry object |
| 60 | + stats = retry_state.retry_object.statistics |
| 61 | + stats["function_name"] = function_name |
| 62 | + stats["time_elapsed"] = round(time.monotonic() - stats["start_time"], 3) |
| 63 | + stats["start_time"] = round(stats["start_time"], 3) |
| 64 | + stats["idle_for"] = round(stats["idle_for"], 3) |
| 65 | + |
| 66 | + # Enrich with details from current attempt for debugging |
| 67 | + if outcome := retry_state.outcome: |
| 68 | + result = outcome.result() |
| 69 | + if isinstance(result, httpx.Response): |
| 70 | + stats["response_status_code"] = result.status_code |
| 71 | + stats["response_headers"] = result.headers |
| 72 | + elif isinstance(result, Exception): |
| 73 | + stats["excepion_type"] = type(result) |
| 74 | + stats["excepion_message"] = str(result) |
63 | 75 |
|
64 | 76 | log.info( |
65 | 77 | "Retry attempt number %i for function %s.", |
66 | 78 | attempt_number, |
67 | 79 | function_name, |
68 | | - extra=retry_stats, |
| 80 | + extra=stats, |
69 | 81 | ) |
70 | 82 |
|
71 | 83 |
|
|
0 commit comments