mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
fix(subscription): log a failed OTEL window update instead of passing silently
SubscriptionTracker._maybe_poll ended its OTEL export in `except Exception: pass`, so a broken or misconfigured exporter left no trace. It now logs the error at debug. Polling is unaffected: the snapshot is kept and last_error stays None. Moved out of the mcp-registry-atomic-writes branch (off theme). Evidence: test_maybe_poll_logs_a_failed_otel_update fails on origin/main (no log record) and passes here. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
a9815bbb5f
commit
08ba2d326d
@@ -457,8 +457,8 @@ class SubscriptionTracker(QuotaTracker):
|
||||
from headroom.observability.metrics import get_otel_metrics
|
||||
|
||||
get_otel_metrics().record_subscription_window(self._state.to_dict())
|
||||
except Exception:
|
||||
pass
|
||||
except Exception as exc:
|
||||
logger.debug("Subscription OTEL metrics update failed: %s", exc)
|
||||
|
||||
def _maybe_reset_contribution(self, snapshot: SubscriptionSnapshot) -> None:
|
||||
"""Reset contribution counters when the 5h window rolls over."""
|
||||
|
||||
@@ -363,6 +363,47 @@ async def test_maybe_poll_runs_transcript_scan_off_event_loop(
|
||||
assert seen["thread_id"] != loop_thread_id
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_maybe_poll_logs_a_failed_otel_update(
|
||||
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
"""A broken OTEL exporter must not break polling, but must leave a trace."""
|
||||
monkeypatch.setattr(SubscriptionTracker, "_load_persisted_state", lambda self: None)
|
||||
tracker = SubscriptionTracker()
|
||||
tracker.notify_active("Bearer live-oauth-token", from_local_operator=True)
|
||||
monkeypatch.setattr("headroom.subscription.client.read_cached_oauth_token", lambda: None)
|
||||
snapshot = _make_snapshot()
|
||||
|
||||
async def fetch_snapshot(token: str | None) -> SubscriptionSnapshot:
|
||||
return snapshot
|
||||
|
||||
def broken_exporter(state: dict) -> None:
|
||||
raise RuntimeError("exporter down")
|
||||
|
||||
tracker._client = SimpleNamespace(fetch=fetch_snapshot)
|
||||
monkeypatch.setattr(
|
||||
tracker_module, "_compute_window_tokens_for_snapshot", lambda snap: WindowTokens(input=7)
|
||||
)
|
||||
monkeypatch.setattr(tracker_module, "_detect_discrepancies", lambda snap, tokens: [])
|
||||
monkeypatch.setattr(tracker, "_persist_state", lambda: None)
|
||||
monkeypatch.setitem(
|
||||
sys.modules,
|
||||
"headroom.observability.metrics",
|
||||
SimpleNamespace(
|
||||
get_otel_metrics=lambda: SimpleNamespace(record_subscription_window=broken_exporter)
|
||||
),
|
||||
)
|
||||
|
||||
with caplog.at_level("DEBUG", logger="headroom.subscription.tracker"):
|
||||
await tracker._maybe_poll()
|
||||
|
||||
assert tracker.latest_snapshot is snapshot
|
||||
assert tracker._state.last_error is None
|
||||
assert "Subscription OTEL metrics update failed: exporter down" in [
|
||||
r.getMessage() for r in caplog.records if r.name == "headroom.subscription.tracker"
|
||||
]
|
||||
|
||||
|
||||
def test_persist_and_load_state_round_trip(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
persist_path = tmp_path / "tracker-state.json"
|
||||
tracker = SubscriptionTracker(persist_path=persist_path)
|
||||
|
||||
Reference in New Issue
Block a user