feat: 性能监控基础设施、解密缓存及计费简化

- 新增 PerfRecorder 性能记录工具,支持采样率与慢请求日志
- 在请求管道中埋点:auth、body_read、json_parse、context_build、authorize、handle
- 流处理器增加 parse/conversion 耗时追踪与 perf_metrics 落库
- 解密服务添加 LRU 缓存,降低高频解密 CPU 开销
- 格式转换分层开关设计:全局 OFF 时回退到端点配置,而非一刀切拒绝
- 移除 shadow billing 模块,统一使用新计费引擎
- 新增 Codex 网关请求适配器(store=false、role 映射、include 补齐)
- endpoint 创建接口支持 body_rules 参数
This commit is contained in:
fawney19
2026-02-05 14:22:11 +08:00
parent e72e5370c4
commit ed2ff5c1d7
25 changed files with 836 additions and 963 deletions

View File

@@ -1,112 +0,0 @@
from unittest.mock import MagicMock
import pytest
from src.config.settings import config
from src.services.billing.schema import BillingSnapshot, CostResult
from src.services.billing.shadow import CostBreakdown, ShadowBillingService
class TestShadowBillingServiceModeResolution:
def test_get_engine_mode_exact_and_wildcard(self, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(config, "billing_engine", "legacy", raising=False)
monkeypatch.setattr(
config,
"billing_engine_overrides",
'{"anthropic/*": "shadow", "openai/gpt-4o": "new"}',
raising=False,
)
svc = ShadowBillingService(MagicMock())
assert svc.get_engine_mode("openai", "gpt-4o") == "new"
assert svc.get_engine_mode("anthropic", "claude-3-5-sonnet") == "shadow"
assert svc.get_engine_mode("other", "x") == "legacy"
class TestShadowBillingServiceExecution:
def test_legacy_mode_skips_new_engine(self, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(config, "billing_engine", "legacy", raising=False)
monkeypatch.setattr(config, "billing_engine_overrides", "{}", raising=False)
svc = ShadowBillingService(MagicMock())
# Guard: if new engine calculate gets called, fail.
svc._new_billing = MagicMock()
svc._new_billing.calculate.side_effect = AssertionError(
"new engine should not run in legacy mode"
)
legacy_truth = CostBreakdown(
input_cost=0.1,
output_cost=0.2,
cache_creation_cost=0.0,
cache_read_cost=0.0,
request_cost=0.0,
total_cost=0.3,
)
res = svc.calculate_with_shadow(
provider="openai",
provider_id="p-1",
model="gpt-4o",
task_type="chat",
api_format="openai:chat",
input_tokens=1,
output_tokens=1,
legacy_truth=legacy_truth,
is_failed_request=False,
)
assert res.engine_mode == "legacy"
assert res.truth_engine == "legacy"
assert res.shadow_snapshot is None
assert res.truth_breakdown.total_cost == 0.3
def test_shadow_mode_returns_snapshot_and_keeps_legacy_truth(
self, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.setattr(config, "billing_engine", "shadow", raising=False)
monkeypatch.setattr(config, "billing_engine_overrides", "{}", raising=False)
monkeypatch.setattr(config, "billing_diff_threshold_usd", 0.0001, raising=False)
svc = ShadowBillingService(MagicMock())
# Stub new engine output
snapshot = BillingSnapshot(
resolved_dimensions={"input_tokens": 1},
resolved_variables={"input_price_per_1m": "3.0"},
cost_breakdown={"input_cost": 0.003},
total_cost=0.003,
status="complete",
calculated_at="2026-02-02T00:00:00Z",
)
svc._new_billing = MagicMock()
svc._new_billing.calculate.return_value = CostResult(
cost=0.003, status="complete", snapshot=snapshot
)
legacy_truth = CostBreakdown(
input_cost=0.004,
output_cost=0.0,
cache_creation_cost=0.0,
cache_read_cost=0.0,
request_cost=0.0,
total_cost=0.004,
)
res = svc.calculate_with_shadow(
provider="openai",
provider_id="p-1",
model="gpt-4o",
task_type="chat",
api_format="openai:chat",
input_tokens=1,
output_tokens=0,
legacy_truth=legacy_truth,
is_failed_request=False,
)
assert res.engine_mode == "shadow"
assert res.truth_engine == "legacy"
assert res.shadow_snapshot is not None
assert res.truth_breakdown.total_cost == 0.004
assert "diff_usd" in res.comparison