From 50e6abaa2fd59fbafb125e26427d7ea25062c53d Mon Sep 17 00:00:00 2001 From: wood chen Date: Sun, 1 Dec 2024 01:46:57 +0800 Subject: [PATCH] refactor(monitoring): enhance TopReferers handling and JSON serialization - Improved LogRequest function to correctly increment counts for referers, ensuring accurate tracking of direct and indirect accesses. - Added MarshalJSON method to SystemMetrics for proper serialization of TopReferers, facilitating better JSON representation of metrics. - Updated comments for clarity and maintainability, enhancing code readability. --- monitoring/metrics.go | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/monitoring/metrics.go b/monitoring/metrics.go index 51c79f4..5e4adc9 100644 --- a/monitoring/metrics.go +++ b/monitoring/metrics.go @@ -1,6 +1,7 @@ package monitoring import ( + "encoding/json" "runtime" "strings" "sync" @@ -22,7 +23,7 @@ type SystemMetrics struct { } `json:"memory_stats"` // 热门引用来源 - TopReferers sync.Map `json:"-"` // 使用 sync.Map 足够了 + TopReferers sync.Map `json:"-"` // 内部使用 sync.Map } type RequestLog struct { @@ -53,9 +54,17 @@ func init() { func LogRequest(log RequestLog) { // 更新引用来源 if log.Referer != "direct" { - metrics.TopReferers.Store(log.Referer, 1) + if val, ok := metrics.TopReferers.Load(log.Referer); ok { + metrics.TopReferers.Store(log.Referer, val.(int64)+1) + } else { + metrics.TopReferers.Store(log.Referer, int64(1)) + } } else { - metrics.TopReferers.Store("直接访问", 1) + if val, ok := metrics.TopReferers.Load("直接访问"); ok { + metrics.TopReferers.Store("直接访问", val.(int64)+1) + } else { + metrics.TopReferers.Store("直接访问", int64(1)) + } } // 更新平均延迟 (只关心 API 请求) @@ -77,3 +86,22 @@ func CollectMetrics() *SystemMetrics { return &metrics } + +// 添加 MarshalJSON 方法来正确序列化 TopReferers +func (m *SystemMetrics) MarshalJSON() ([]byte, error) { + type Alias SystemMetrics + referers := make(map[string]int64) + + m.TopReferers.Range(func(key, value interface{}) bool { + referers[key.(string)] = value.(int64) + return true + }) + + return json.Marshal(&struct { + *Alias + TopReferers map[string]int64 `json:"top_referers"` + }{ + Alias: (*Alias)(m), + TopReferers: referers, + }) +}