Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(subscription): log a failed OTEL window update instead of passing silently (#4131)

Tejas Chopra7d9dd55e7c4b
diff --git a/headroom/subscription/tracker.py b/headroom/subscription/tracker.py
index 14bdf5b26..80d5aa661 100644
--- a/headroom/subscription/tracker.py
+++ b/headroom/subscription/tracker.py
@@ -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."""
diff --git a/tests/test_subscription_tracker.py b/tests/test_subscription_tracker.py
index e95834a19..ece9c6966 100644
--- a/tests/test_subscription_tracker.py
+++ b/tests/test_subscription_tracker.py
@@ -363,6 +363,47 @@ async def test_maybe_poll_runs_transcript_scan_off_event_loop(
assert seen["thread_id"] != loop_thread_id
+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)