Merge remote-tracking branch 'origin/cleanup/log-safety-helpers' into cleanup/proxy-handlers-debuggable-logs

This commit is contained in:
Tejas Chopra
2026-10-10 09:17:54 -07:00
2 changed files with 31 additions and 1 deletions
+7 -1
View File
@@ -14,6 +14,7 @@ Content is logged only when the operator opts in to it explicitly with
from __future__ import annotations
import logging
import math
import os
import threading
import time
@@ -65,8 +66,11 @@ def _describe_one(exc: BaseException) -> str:
# subclasses (ssl.SSLError) and can quote a path or a peer's message.
# An out-of-range errno is not formatted at all: converting a huge int
# to text can itself raise, and a log helper must never raise.
# Negative codes (socket.gaierror's EAI_*) have no os.strerror text.
if 0 <= exc.errno <= _MAX_ERRNO:
text += f" [Errno {exc.errno}] {_errno_text(exc.errno)}"
elif -_MAX_ERRNO - 1 <= exc.errno < 0:
text += f" [Errno {exc.errno}]"
else:
text += " [Errno out of range]"
frames = traceback.extract_tb(exc.__traceback__)[-_MAX_FRAMES:]
@@ -178,13 +182,15 @@ class WarnOnce:
return True
report_overflow = not self._overflowed
self._overflowed = True
started = now if self._window_start is None else self._window_start
remaining = self._window - (now - started)
if report_overflow:
log.warning(
"More than %d distinct %s; further ones are logged at debug only "
"for the next %d minutes",
self._limit,
self._what,
max(1, round(self._window / 60)),
max(1, math.ceil(remaining / 60)),
)
return False
+24
View File
@@ -227,3 +227,27 @@ def test_out_of_range_errno_never_breaks_the_description(digits: int) -> None:
exc.errno = 10 ** (digits - 1) # computed, not a literal, so collection never formats it
assert describe_exception(exc) == "OSError [Errno out of range]"
def test_negative_dns_error_codes_are_kept() -> None:
import socket
assert describe_exception(socket.gaierror(-2, "Name or service not known")) == (
"gaierror [Errno -2]"
)
def test_overflow_notice_states_the_time_left_in_the_window(
caplog: pytest.LogCaptureFixture, monkeypatch: pytest.MonkeyPatch
) -> None:
log = logging.getLogger("headroom.test_log_safety.remaining")
guard = WarnOnce(limit=1, what="failures", window_seconds=3600)
clock = [0.0]
monkeypatch.setattr(log_safety.time, "monotonic", lambda: clock[0])
with caplog.at_level(logging.INFO, logger=log.name):
guard.first("a", log)
clock[0] = 3000.0 # ten minutes left in the window
guard.first("b", log)
assert _warnings(caplog)[-1].endswith("for the next 10 minutes")