重构整体架构: 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实现
91 lines
1.8 KiB
Go
91 lines
1.8 KiB
Go
package telemetry
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"sort"
|
|
"sync"
|
|
"sync/atomic"
|
|
)
|
|
|
|
// Counter 单调递增的累计指标(整数语义,内部用 float64 位以 atomic 操作)。
|
|
type Counter struct {
|
|
metricName string
|
|
help string
|
|
labelNames []string
|
|
|
|
mu sync.RWMutex
|
|
values map[string]*counterChild // key = labelKey(...)
|
|
}
|
|
|
|
type counterChild struct {
|
|
labels Labels
|
|
bits atomic.Uint64 // float64
|
|
}
|
|
|
|
func newCounter(name, help string, labelNames []string) *Counter {
|
|
return &Counter{
|
|
metricName: name,
|
|
help: help,
|
|
labelNames: append([]string(nil), labelNames...),
|
|
values: make(map[string]*counterChild),
|
|
}
|
|
}
|
|
|
|
// Inc 计数 +1。
|
|
func (c *Counter) Inc(labels Labels) { c.Add(1, labels) }
|
|
|
|
// Add 累加 v(v 必须 >= 0)。
|
|
func (c *Counter) Add(v float64, labels Labels) {
|
|
if v < 0 {
|
|
return
|
|
}
|
|
child := c.getOrCreate(labels)
|
|
for {
|
|
bits := child.bits.Load()
|
|
cur := float64frombits(bits)
|
|
next := float64bits(cur + v)
|
|
if child.bits.CompareAndSwap(bits, next) {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func (c *Counter) getOrCreate(labels Labels) *counterChild {
|
|
key := labelKey(c.labelNames, labels)
|
|
c.mu.RLock()
|
|
if child, ok := c.values[key]; ok {
|
|
c.mu.RUnlock()
|
|
return child
|
|
}
|
|
c.mu.RUnlock()
|
|
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
if child, ok := c.values[key]; ok {
|
|
return child
|
|
}
|
|
child := &counterChild{labels: copyLabels(labels, c.labelNames)}
|
|
c.values[key] = child
|
|
return child
|
|
}
|
|
|
|
func (c *Counter) collect(w io.Writer) {
|
|
fmt.Fprintf(w, "# HELP %s %s\n", c.metricName, c.help)
|
|
fmt.Fprintf(w, "# TYPE %s counter\n", c.metricName)
|
|
|
|
c.mu.RLock()
|
|
keys := make([]string, 0, len(c.values))
|
|
for k := range c.values {
|
|
keys = append(keys, k)
|
|
}
|
|
sort.Strings(keys)
|
|
defer c.mu.RUnlock()
|
|
|
|
for _, k := range keys {
|
|
child := c.values[k]
|
|
val := float64frombits(child.bits.Load())
|
|
writeMetricLine(w, c.metricName, child.labels, val)
|
|
}
|
|
}
|