mirror of
https://github.com/woodchen-ink/proxy-go.git
synced 2025-07-18 16:41:54 +08:00
fix(metrics): 优化延迟统计和分布数据处理
- 修复延迟统计分布数据获取逻辑,增加更健壮的类型转换 - 确保延迟分布桶始终存在,即使没有数据 - 在处理器中为空分布数据添加默认初始化 - 优化指标收集器中的延迟桶初始化和数据获取方法
This commit is contained in:
parent
b6b77b03ed
commit
2cb88a4f5e
@ -161,15 +161,32 @@ func (h *ProxyHandler) MetricsHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
metrics.LatencyStats.Max = utils.SafeString(latencyStats["max"], "0ms")
|
metrics.LatencyStats.Max = utils.SafeString(latencyStats["max"], "0ms")
|
||||||
|
|
||||||
// 处理分布数据
|
// 处理分布数据
|
||||||
if distribution, ok := latencyStats["distribution"].(map[string]interface{}); ok {
|
if stats["latency_stats"] != nil {
|
||||||
metrics.LatencyStats.Distribution = make(map[string]int64)
|
if distribution, ok := stats["latency_stats"].(map[string]interface{})["distribution"]; ok && distribution != nil {
|
||||||
for k, v := range distribution {
|
if distributionMap, ok := distribution.(map[string]interface{}); ok {
|
||||||
if intValue, ok := v.(float64); ok {
|
metrics.LatencyStats.Distribution = make(map[string]int64)
|
||||||
metrics.LatencyStats.Distribution[k] = int64(intValue)
|
for k, v := range distributionMap {
|
||||||
|
if intValue, ok := v.(float64); ok {
|
||||||
|
metrics.LatencyStats.Distribution[k] = int64(intValue)
|
||||||
|
} else if intValue, ok := v.(int64); ok {
|
||||||
|
metrics.LatencyStats.Distribution[k] = intValue
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 如果分布数据为空,初始化一个空的分布
|
||||||
|
if metrics.LatencyStats.Distribution == nil {
|
||||||
|
metrics.LatencyStats.Distribution = make(map[string]int64)
|
||||||
|
// 添加默认的延迟桶
|
||||||
|
metrics.LatencyStats.Distribution["<10ms"] = 0
|
||||||
|
metrics.LatencyStats.Distribution["10-50ms"] = 0
|
||||||
|
metrics.LatencyStats.Distribution["50-200ms"] = 0
|
||||||
|
metrics.LatencyStats.Distribution["200-1000ms"] = 0
|
||||||
|
metrics.LatencyStats.Distribution[">1s"] = 0
|
||||||
|
}
|
||||||
|
|
||||||
// 填充错误统计数据
|
// 填充错误统计数据
|
||||||
metrics.ErrorStats.ClientErrors = clientErrors
|
metrics.ErrorStats.ClientErrors = clientErrors
|
||||||
metrics.ErrorStats.ServerErrors = serverErrors
|
metrics.ErrorStats.ServerErrors = serverErrors
|
||||||
|
@ -61,11 +61,12 @@ func InitCollector(cfg *config.Config) error {
|
|||||||
instance.bandwidthStats.history = make(map[string]int64)
|
instance.bandwidthStats.history = make(map[string]int64)
|
||||||
|
|
||||||
// 初始化延迟分布桶
|
// 初始化延迟分布桶
|
||||||
instance.latencyBuckets.Store("<10ms", new(int64))
|
buckets := []string{"<10ms", "10-50ms", "50-200ms", "200-1000ms", ">1s"}
|
||||||
instance.latencyBuckets.Store("10-50ms", new(int64))
|
for _, bucket := range buckets {
|
||||||
instance.latencyBuckets.Store("50-200ms", new(int64))
|
counter := new(int64)
|
||||||
instance.latencyBuckets.Store("200-1000ms", new(int64))
|
*counter = 0
|
||||||
instance.latencyBuckets.Store(">1s", new(int64))
|
instance.latencyBuckets.Store(bucket, counter)
|
||||||
|
}
|
||||||
|
|
||||||
// 启动数据一致性检查器
|
// 启动数据一致性检查器
|
||||||
instance.startConsistencyChecker()
|
instance.startConsistencyChecker()
|
||||||
@ -344,14 +345,20 @@ func (c *Collector) GetStats() map[string]interface{} {
|
|||||||
|
|
||||||
// 收集延迟分布
|
// 收集延迟分布
|
||||||
latencyDistribution := make(map[string]int64)
|
latencyDistribution := make(map[string]int64)
|
||||||
c.latencyBuckets.Range(func(key, value interface{}) bool {
|
|
||||||
if counter, ok := value.(*int64); ok {
|
// 确保所有桶都存在,即使计数为0
|
||||||
latencyDistribution[key.(string)] = atomic.LoadInt64(counter)
|
buckets := []string{"<10ms", "10-50ms", "50-200ms", "200-1000ms", ">1s"}
|
||||||
|
for _, bucket := range buckets {
|
||||||
|
if counter, ok := c.latencyBuckets.Load(bucket); ok {
|
||||||
|
if counter != nil {
|
||||||
|
latencyDistribution[bucket] = atomic.LoadInt64(counter.(*int64))
|
||||||
|
} else {
|
||||||
|
latencyDistribution[bucket] = 0
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
latencyDistribution[key.(string)] = value.(int64)
|
latencyDistribution[bucket] = 0
|
||||||
}
|
}
|
||||||
return true
|
}
|
||||||
})
|
|
||||||
|
|
||||||
// 获取最近请求记录(使用读锁)
|
// 获取最近请求记录(使用读锁)
|
||||||
recentRequests := c.recentRequests.GetAll()
|
recentRequests := c.recentRequests.GetAll()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user