feat: 实现完整可观测性架构与火山v3适配器重构
重构整体架构: 1. 新增telemetry包实现零依赖的Prometheus指标系统 2. 新增metrics包集中管理业务埋点指标 3. 重构火山v3适配器,拆分client/request/response等模块 4. 替换旧的service/stats统计系统为标准指标埋点 5. 新增/metrics观测端点与完整仪表盘支持 功能更新: - 实现基于IP的限流与并发限制,添加指标埋点 - 重构TTS控制器,支持多格式输出与完整错误分类 - 更新.env.example配置示例,新增多项可选参数 - 替换旧的volcano适配器实现,支持完整的v3 API特性 - 清理冗余代码,移除service/stats与旧adapter实现
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
package telemetry
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"sort"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
// Gauge 可增可减的瞬时值。
|
||||
type Gauge struct {
|
||||
metricName string
|
||||
help string
|
||||
labelNames []string
|
||||
|
||||
mu sync.RWMutex
|
||||
values map[string]*gaugeChild
|
||||
}
|
||||
|
||||
type gaugeChild struct {
|
||||
labels Labels
|
||||
bits atomic.Uint64
|
||||
}
|
||||
|
||||
func newGauge(name, help string, labelNames []string) *Gauge {
|
||||
return &Gauge{
|
||||
metricName: name,
|
||||
help: help,
|
||||
labelNames: append([]string(nil), labelNames...),
|
||||
values: make(map[string]*gaugeChild),
|
||||
}
|
||||
}
|
||||
|
||||
// Set 直接设置当前值。
|
||||
func (g *Gauge) Set(v float64, labels Labels) {
|
||||
child := g.getOrCreate(labels)
|
||||
child.bits.Store(float64bits(v))
|
||||
}
|
||||
|
||||
// Inc +1。
|
||||
func (g *Gauge) Inc(labels Labels) { g.Add(1, labels) }
|
||||
|
||||
// Dec -1。
|
||||
func (g *Gauge) Dec(labels Labels) { g.Add(-1, labels) }
|
||||
|
||||
// Add 累加 v(可负)。
|
||||
func (g *Gauge) Add(v float64, labels Labels) {
|
||||
child := g.getOrCreate(labels)
|
||||
for {
|
||||
bits := child.bits.Load()
|
||||
cur := float64frombits(bits)
|
||||
next := float64bits(cur + v)
|
||||
if child.bits.CompareAndSwap(bits, next) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Gauge) getOrCreate(labels Labels) *gaugeChild {
|
||||
key := labelKey(g.labelNames, labels)
|
||||
g.mu.RLock()
|
||||
if c, ok := g.values[key]; ok {
|
||||
g.mu.RUnlock()
|
||||
return c
|
||||
}
|
||||
g.mu.RUnlock()
|
||||
|
||||
g.mu.Lock()
|
||||
defer g.mu.Unlock()
|
||||
if c, ok := g.values[key]; ok {
|
||||
return c
|
||||
}
|
||||
c := &gaugeChild{labels: copyLabels(labels, g.labelNames)}
|
||||
g.values[key] = c
|
||||
return c
|
||||
}
|
||||
|
||||
func (g *Gauge) collect(w io.Writer) {
|
||||
fmt.Fprintf(w, "# HELP %s %s\n", g.metricName, g.help)
|
||||
fmt.Fprintf(w, "# TYPE %s gauge\n", g.metricName)
|
||||
|
||||
g.mu.RLock()
|
||||
keys := make([]string, 0, len(g.values))
|
||||
for k := range g.values {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
defer g.mu.RUnlock()
|
||||
|
||||
for _, k := range keys {
|
||||
child := g.values[k]
|
||||
val := float64frombits(child.bits.Load())
|
||||
writeMetricLine(w, g.metricName, child.labels, val)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user