Files
Sakurasan 0e10f0bbde feat: 余额预扣 reserve/capture、冷却兜底重试、Prometheus /metrics
- 预扣:每请求按 body 估算成本上界计入冻结额(1e-8 USD 原子计数),402 判定
  改为「余额 - 在途冻结 ≤ 0」,并发的长流式请求不再互相透支;OT_PROXY_RESERVE_MAX_TOKENS
  控制估算基数(默认 4096,0=关闭),handler defer 释放,失败/中断/panic 均覆盖
- 调度:全部候选都在运行期冷却时,剩余冷却 ≤ OT_PROXY_COOLDOWN_GRACE 的最早恢复渠道
  兜底重试一次而不是 503(429 Retry-After 语义);DB 层不可用的渠道不参与兜底
- 指标:openteam_proxy_requests_total / request_duration / channel_failures /
  usage_queue_depth / frozen_balance,/metrics 默认关闭(OT_METRICS_ENABLED 开启)
2026-11-09 01:04:40 +08:00

63 lines
2.5 KiB
Go

// Package metrics Prometheus 指标:进程内 registry + 文本端点(api 层按开关挂载 /metrics)。
//
// label 基数约束:channel/model 来自受管目录、status/kind 为有限枚举,可安全做 label;
// 请求级随机值(trace id、用户 id、错误详情)禁止入 label,否则基数爆炸拖垮采集端。
package metrics
import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var reg = prometheus.NewRegistry()
var (
// RequestsTotal 代理请求终结计数。channel 为 "none" 表示未打到任何渠道
// (无候选/满载等),model 为请求的模型名。
RequestsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "openteam_proxy_requests_total",
Help: "Proxied requests terminated, by channel/model/status.",
}, []string{"channel", "model", "status"})
// RequestDuration 请求从进入 handler 到终结的耗时(按渠道)。
RequestDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "openteam_proxy_request_duration_seconds",
Help: "Proxy request duration, by channel.",
Buckets: []float64{.05, .1, .25, .5, 1, 2.5, 5, 10, 30, 60, 120, 300},
}, []string{"channel"})
// ChannelFailures 上游失败计数(按渠道与失败类型),与运行期冷却同源。
ChannelFailures = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "openteam_channel_failures_total",
Help: "Upstream failures, by channel/fail kind.",
}, []string{"channel", "kind"})
// RecorderQueueDepth 记账队列中待落库的记录数。持续增长说明 DB 写入跟不上。
RecorderQueueDepth = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "openteam_usage_queue_depth",
Help: "Usage records buffered, waiting to be flushed.",
})
// FrozenBalance 在途请求预扣(冻结)的金额合计(USD)。见 proxy 的 reserve/capture。
FrozenBalance = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "openteam_frozen_balance",
Help: "Balance reserved by in-flight requests (USD).",
})
)
func init() {
reg.MustRegister(
RequestsTotal, RequestDuration, ChannelFailures, RecorderQueueDepth, FrozenBalance,
collectors.NewGoCollector(),
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
)
}
// Handler /metrics 的 http.Handler(独立 registry,避免全局默认注册器的耦合)。
func Handler() http.Handler {
return promhttp.HandlerFor(reg, promhttp.HandlerOpts{})
}