From 1c22e4c02035b10941d38266222ad827b311ef5e Mon Sep 17 00:00:00 2001 From: wood chen Date: Thu, 5 Dec 2024 10:21:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=88=E6=BA=A2=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/metrics/collector.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/internal/metrics/collector.go b/internal/metrics/collector.go index 1ec73be..973ad6d 100644 --- a/internal/metrics/collector.go +++ b/internal/metrics/collector.go @@ -564,18 +564,16 @@ func calculateChangeRate(stats map[string]interface{}) float64 { // CheckDataConsistency 检查数据一致性 func (c *Collector) CheckDataConsistency() error { - stats := c.GetStats() - - // 检查基础指标 - totalReqs := stats["total_requests"].(int64) + // 不再调用 GetStats(),而是直接访问计数器 + totalReqs := c.persistentStats.totalRequests.Load() + atomic.LoadInt64(&c.totalRequests) if totalReqs < 0 { return fmt.Errorf("invalid total_requests: %d", totalReqs) } - // 查状态码统计 - statusStats := stats["status_code_stats"].(map[string]int64) + // 检查状态码统计 var statusTotal int64 - for _, count := range statusStats { + for i := range c.statusStats { + count := c.statusStats[i].Load() if count < 0 { return fmt.Errorf("invalid status code count: %d", count) } @@ -583,14 +581,16 @@ func (c *Collector) CheckDataConsistency() error { } // 检查路径统计 - pathStats := stats["top_paths"].([]models.PathMetrics) var pathTotal int64 - for _, p := range pathStats { - if p.RequestCount < 0 { - return fmt.Errorf("invalid path request count: %d", p.RequestCount) + c.pathStats.Range(func(_, value interface{}) bool { + stats := value.(*models.PathStats) + count := stats.Requests.Load() + if count < 0 { + return false } - pathTotal += p.RequestCount - } + pathTotal += count + return true + }) // 允许一定的误差 if abs(statusTotal-totalReqs) > totalReqs/100 || abs(pathTotal-totalReqs) > totalReqs/100 {