fix(telemetry): 指标写入对空接收者安全,修 nil 解引用 panic

metrics 包的全局指标(UpstreamTotal 等)默认是 nil,只有 main 调过 metrics.Init() 之后才有值。任何不经过 main 的调用路径都会在 controller/tts.go -> adapter/volcano/synthesis.go -> metrics.AdapterRecorder 处 nil 解引用,整个进程崩掉。

生产路径本来没问题(二进制一定先 Init),但这类崩溃只在运行时暴露,且漏掉指标初始化属于很容易踩的装配错误,不该以 panic 收场。

改动: Counter.Add / Gauge.Set / Gauge.Add / Histogram.Observe 增加空接收者判断,nil 时静默忽略(与 noop 语义一致);在包注释里写成显式设计约定,避免以后被当成冗余代码删掉。
This commit is contained in:
sun
2026-10-03 16:13:47 +08:00
parent 21e1fd1fbb
commit 1e2be0cd5b
4 changed files with 23 additions and 2 deletions
+4
View File
@@ -47,7 +47,11 @@ func newHistogram(name, help string, buckets []float64, labelNames []string) *Hi
}
// Observe 记录一个观测值。
// 空接收者(nil *Histogram)安全,见 Counter.Add 的说明。
func (h *Histogram) Observe(v float64, labels Labels) {
if h == nil {
return
}
child := h.getOrCreate(labels)
for {
bits := child.sumBits.Load()