mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
fix(proxy): keep pricing and extension errors payload-safe at every log level
_warn_pricing_once sent the raw exception to DEBUG (exc_info), so a pricing
backend error quoting a credential reached proxy.log once debug was on. It now
logs the lookup, safe_id(model) and describe_exception(exc) (types and code
locations, never the message) at WARNING and nothing else. All callers (cache
rates, list price, output price, estimate_cost) share it. The dedupe moves to
WarnOnce from headroom/log_safety.py. Extension discovery failure uses
describe_exception instead of a DEBUG traceback.
Regression: estimate_cost('gpt-4o', 100, 10) with a pricing error carrying a
credential canary; every WARNING/DEBUG record is formatted and must not contain it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
3202058caf
commit
64ff3f03af
+21
-33
@@ -16,6 +16,7 @@ from datetime import datetime, timedelta
|
||||
from typing import TYPE_CHECKING, Any, NamedTuple
|
||||
|
||||
from headroom.cache_economics import CACHE_ECONOMICS as _CACHE_ECONOMICS
|
||||
from headroom.log_safety import WarnOnce, describe_exception, safe_id
|
||||
from headroom.proxy.budget_basis_policy import (
|
||||
BUDGET_BASIS_BLOCK,
|
||||
BUDGET_BASIS_IGNORE,
|
||||
@@ -57,30 +58,26 @@ logger = logging.getLogger("headroom.proxy")
|
||||
# Pricing-lookup warnings are emitted on the per-request cost path, so an
|
||||
# unresolvable model (a custom / OpenAI-compatible name LiteLLM can't price,
|
||||
# e.g. glm-5.2) floods proxy.log with an identical WARNING every single request
|
||||
# (#2504). Track which models have already been warned so each fires once per
|
||||
# process. Model names come from clients, so the set is capped: when it fills it
|
||||
# is cleared, which at worst repeats a warning instead of growing without bound.
|
||||
_warned_pricing_models: set[str] = set()
|
||||
_WARNED_PRICING_MODELS_MAX = 1024
|
||||
# (#2504). Each (lookup, model) pair warns once; model names come from clients,
|
||||
# so WarnOnce also caps how many distinct models can warn.
|
||||
_pricing_warnings = WarnOnce(1024, "unpriceable models")
|
||||
|
||||
|
||||
def _warn_pricing_once(model: str, message: str, exc: BaseException | None = None) -> None:
|
||||
"""Emit ``message`` at WARNING only the first time ``model`` fails pricing.
|
||||
def _warn_pricing_once(
|
||||
lookup: str, what: str, model: str, exc: BaseException | None = None
|
||||
) -> None:
|
||||
"""Warn once per ``(lookup, model)`` that pricing failed, payload-safe.
|
||||
|
||||
With ``exc``, the WARNING names only the exception type: pricing errors can
|
||||
echo the client's model string or provider internals. The full exception
|
||||
goes to DEBUG.
|
||||
A pricing backend's exception can quote provider responses or credentials,
|
||||
so only :func:`describe_exception` (types and code locations) is logged, at
|
||||
every level.
|
||||
"""
|
||||
if model in _warned_pricing_models or not logger.isEnabledFor(logging.WARNING):
|
||||
if not _pricing_warnings.first((lookup, model), logger):
|
||||
return
|
||||
if len(_warned_pricing_models) >= _WARNED_PRICING_MODELS_MAX:
|
||||
_warned_pricing_models.clear()
|
||||
_warned_pricing_models.add(model)
|
||||
if exc is None:
|
||||
logger.warning(message)
|
||||
return
|
||||
logger.warning("%s (%s)", message, type(exc).__name__)
|
||||
logger.debug("%s", message, exc_info=exc)
|
||||
logger.warning("%s for model %s", what, safe_id(model))
|
||||
else:
|
||||
logger.warning("%s for model %s: %s", what, safe_id(model), describe_exception(exc))
|
||||
|
||||
|
||||
# A route whose responses never carry a usage breakdown hits the estimated-basis
|
||||
@@ -290,9 +287,7 @@ def build_prefix_cache_stats(
|
||||
cache_prices = resolve_rates(model_name)
|
||||
except Exception as e:
|
||||
_warn_pricing_once(
|
||||
f"__cache_rates__:{model_name}",
|
||||
f"Failed to resolve cache rates for model {model_name}",
|
||||
e,
|
||||
"cache_rates", "Failed to resolve cache rates", model_name, e
|
||||
)
|
||||
cache_prices = None
|
||||
best_tokens = tokens_sent
|
||||
@@ -990,8 +985,7 @@ class CostTracker:
|
||||
litellm = _get_litellm_module()
|
||||
if litellm is None:
|
||||
_warn_pricing_once(
|
||||
f"__litellm_unavailable__:{model}",
|
||||
f"LiteLLM not available - cannot calculate costs for model {model}",
|
||||
"litellm_unavailable", "LiteLLM not available - cannot calculate costs", model
|
||||
)
|
||||
return None
|
||||
|
||||
@@ -1014,7 +1008,7 @@ class CostTracker:
|
||||
return float(total_cost) if total_cost > 0 else None
|
||||
|
||||
except Exception as e:
|
||||
_warn_pricing_once(model, f"Failed to get pricing for model {model}", e)
|
||||
_warn_pricing_once("estimate", "Failed to get pricing", model, e)
|
||||
return None
|
||||
|
||||
def _prune_old_costs(self):
|
||||
@@ -1388,9 +1382,7 @@ class CostTracker:
|
||||
cost_per_token = info.get("input_cost_per_token")
|
||||
return cost_per_token * 1_000_000 if cost_per_token else None
|
||||
except Exception as e:
|
||||
_warn_pricing_once(
|
||||
f"__list_price__:{model}", f"Failed to get list price for model {model}", e
|
||||
)
|
||||
_warn_pricing_once("list_price", "Failed to get list price", model, e)
|
||||
return None
|
||||
|
||||
def _get_output_price(self, model: str, *, long_context: bool = False) -> float | None:
|
||||
@@ -1412,9 +1404,7 @@ class CostTracker:
|
||||
return info.get("output_cost_per_token_above_200k_tokens") or base or None
|
||||
return base or None
|
||||
except Exception as e:
|
||||
_warn_pricing_once(
|
||||
f"__output_price__:{model}", f"Failed to get output price for model {model}", e
|
||||
)
|
||||
_warn_pricing_once("output_price", "Failed to get output price", model, e)
|
||||
return None
|
||||
|
||||
def _write_cost_usd(self, model: str, w5m_price: float, w1h_price: float) -> float:
|
||||
@@ -1469,9 +1459,7 @@ class CostTracker:
|
||||
|
||||
rates = resolve_rates(model, long_context=long_context, for_billing=for_billing)
|
||||
except Exception as e:
|
||||
_warn_pricing_once(
|
||||
f"__cache_rates__:{model}", f"Failed to resolve cache rates for model {model}", e
|
||||
)
|
||||
_warn_pricing_once("cache_rates", "Failed to resolve cache rates", model, e)
|
||||
return None
|
||||
if rates is None or not rates.uncached:
|
||||
return None
|
||||
|
||||
@@ -103,6 +103,8 @@ import sys
|
||||
from collections.abc import Callable, Iterable, Iterator
|
||||
from typing import Any
|
||||
|
||||
from headroom.log_safety import describe_exception
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
ENTRY_POINT_GROUP = "headroom.proxy_extension"
|
||||
@@ -125,10 +127,9 @@ def discover() -> Iterator[tuple[str, ProxyExtension]]:
|
||||
except Exception as exc: # noqa: BLE001 — importlib.metadata can raise varied types
|
||||
log.warning(
|
||||
"proxy extensions: entry-point enumeration failed (%s), so no proxy "
|
||||
"extensions will be installed; enable debug logging for the traceback",
|
||||
type(exc).__name__,
|
||||
"extensions will be installed",
|
||||
describe_exception(exc),
|
||||
)
|
||||
log.debug("proxy extensions: entry-point enumeration failure detail", exc_info=True)
|
||||
return
|
||||
for entry in entries:
|
||||
try:
|
||||
|
||||
@@ -14,6 +14,7 @@ from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from headroom.log_safety import WarnOnce
|
||||
from headroom.pricing.counterfactual import resolve_rates
|
||||
from headroom.proxy.cost import CostTracker
|
||||
|
||||
@@ -374,7 +375,7 @@ def test_cache_rate_failure_warns_once_per_model(monkeypatch: pytest.MonkeyPatch
|
||||
"""A raising rate resolver used to read as 'unpriced' ($0) with no trace."""
|
||||
from headroom.proxy import cost
|
||||
|
||||
monkeypatch.setattr(cost, "_warned_pricing_models", set())
|
||||
monkeypatch.setattr(cost, "_pricing_warnings", WarnOnce(1024, "unpriceable models"))
|
||||
_patch_litellm(
|
||||
monkeypatch,
|
||||
{"m": {"input_cost_per_token": 1e-6, "litellm_provider": "anthropic"}},
|
||||
@@ -396,5 +397,42 @@ def test_cache_rate_failure_warns_once_per_model(monkeypatch: pytest.MonkeyPatch
|
||||
|
||||
warnings = [r.getMessage() for r in capture.records if r.levelno == logging.WARNING]
|
||||
assert len(warnings) == 1
|
||||
assert "for model m" in warnings[0] and "RuntimeError" in warnings[0]
|
||||
assert "pricing table corrupt" not in warnings[0] # exception text stays at DEBUG
|
||||
assert "for model 'm'" in warnings[0] and "RuntimeError" in warnings[0]
|
||||
assert "pricing table corrupt" not in warnings[0]
|
||||
|
||||
|
||||
_CREDENTIAL_CANARY = "Authorization: Bearer sk-canary-7f3a9c"
|
||||
|
||||
|
||||
def test_pricing_error_never_logs_its_text_at_any_level(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
"""A pricing backend error quoting a credential must not reach any log record.
|
||||
|
||||
Formats every WARNING and DEBUG record with a real Formatter, which renders
|
||||
exc_info too, so a traceback carrying the text would be caught.
|
||||
"""
|
||||
from headroom.proxy import cost
|
||||
|
||||
class _LeakyLiteLLM:
|
||||
model_cost: dict = {}
|
||||
|
||||
@staticmethod
|
||||
def cost_per_token(**_kwargs):
|
||||
raise RuntimeError(f"upstream rejected request ({_CREDENTIAL_CANARY})")
|
||||
|
||||
monkeypatch.setattr(cost, "_pricing_warnings", WarnOnce(1024, "unpriceable models"))
|
||||
monkeypatch.setattr(cost, "_get_litellm_module", lambda: _LeakyLiteLLM())
|
||||
monkeypatch.delenv("HEADROOM_DEBUG_DUMP", raising=False)
|
||||
capture = _Capture()
|
||||
previous_level = cost.logger.level
|
||||
cost.logger.setLevel(logging.DEBUG)
|
||||
cost.logger.addHandler(capture)
|
||||
try:
|
||||
assert CostTracker().estimate_cost("gpt-4o", 100, 10) is None
|
||||
finally:
|
||||
cost.logger.removeHandler(capture)
|
||||
cost.logger.setLevel(previous_level)
|
||||
|
||||
formatter = logging.Formatter()
|
||||
rendered = [formatter.format(r) for r in capture.records if r.levelno >= logging.DEBUG]
|
||||
assert any("Failed to get pricing for model 'gpt-4o'" in line for line in rendered)
|
||||
assert all("sk-canary" not in line for line in rendered), rendered
|
||||
|
||||
@@ -10,13 +10,15 @@ import logging
|
||||
|
||||
import pytest
|
||||
|
||||
from headroom.log_safety import WarnOnce
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def cost_tracker(monkeypatch: pytest.MonkeyPatch):
|
||||
import headroom.proxy.cost as cost_mod
|
||||
|
||||
# Reset the per-process dedup set so tests are order-independent.
|
||||
cost_mod._warned_pricing_models.clear()
|
||||
# A fresh dedup guard so tests are order-independent.
|
||||
monkeypatch.setattr(cost_mod, "_pricing_warnings", WarnOnce(1024, "unpriceable models"))
|
||||
|
||||
class _FakeLiteLLM:
|
||||
@staticmethod
|
||||
@@ -33,7 +35,7 @@ def test_pricing_failure_warns_once_per_model(cost_tracker, caplog):
|
||||
assert cost_tracker.estimate_cost("glm-5.2", 100, 50) is None
|
||||
|
||||
warnings = [
|
||||
r for r in caplog.records if "Failed to get pricing for model glm-5.2" in r.getMessage()
|
||||
r for r in caplog.records if "Failed to get pricing for model 'glm-5.2'" in r.getMessage()
|
||||
]
|
||||
assert len(warnings) == 1
|
||||
|
||||
@@ -46,14 +48,14 @@ def test_distinct_models_each_warn_once(cost_tracker, caplog):
|
||||
cost_tracker.estimate_cost("mystery-model", 10, 5)
|
||||
|
||||
msgs = [r.getMessage() for r in caplog.records if "Failed to get pricing" in r.getMessage()]
|
||||
assert sum("for model glm-5.2 (" in m for m in msgs) == 1
|
||||
assert sum("for model mystery-model (" in m for m in msgs) == 1
|
||||
assert sum("for model 'glm-5.2':" in m for m in msgs) == 1
|
||||
assert sum("for model 'mystery-model':" in m for m in msgs) == 1
|
||||
|
||||
|
||||
def test_litellm_unavailable_warns_once_per_model(monkeypatch, caplog):
|
||||
import headroom.proxy.cost as cost_mod
|
||||
|
||||
cost_mod._warned_pricing_models.clear()
|
||||
monkeypatch.setattr(cost_mod, "_pricing_warnings", WarnOnce(1024, "unpriceable models"))
|
||||
monkeypatch.setattr(cost_mod, "_get_litellm_module", lambda: None)
|
||||
tracker = cost_mod.CostTracker()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user