Skip to content

Commit 79b0305

Browse files
committed
More detailed default retry logging
1 parent 0ed9c0d commit 79b0305

File tree

1 file changed

+18
-6
lines changed
  • src/ghga_service_commons/transports

1 file changed

+18
-6
lines changed

src/ghga_service_commons/transports/retry.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
"""Provides an httpx.AsyncTransport that handles retrying requests on failure."""
1717

18+
import time
1819
from collections.abc import Callable
1920
from logging import getLogger
2021
from types import TracebackType
@@ -55,17 +56,28 @@ def _log_retry_stats(retry_state: RetryCallState):
5556
function_name = retry_state.fn.__qualname__
5657
attempt_number = retry_state.attempt_number
5758

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)
6375

6476
log.info(
6577
"Retry attempt number %i for function %s.",
6678
attempt_number,
6779
function_name,
68-
extra=retry_stats,
80+
extra=stats,
6981
)
7082

7183

0 commit comments

Comments
 (0)