From 41f5b82661db2d1091e7eec0c034c08cd4498074 Mon Sep 17 00:00:00 2001 From: wood chen Date: Sun, 16 Feb 2025 20:24:31 +0800 Subject: [PATCH] feat(metrics): Enhance uptime formatting with more precise time display - Update FormatUptime to include seconds in time representation - Modify metrics handler to use new uptime formatting method - Improve readability of uptime display with more granular time details --- internal/handler/metrics.go | 19 ++++++++++--------- internal/metrics/collector.go | 16 ++++++++++------ 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/internal/handler/metrics.go b/internal/handler/metrics.go index c929437..2804935 100644 --- a/internal/handler/metrics.go +++ b/internal/handler/metrics.go @@ -11,6 +11,7 @@ import ( "time" ) +// Metrics 定义指标结构 type Metrics struct { // 基础指标 Uptime string `json:"uptime"` @@ -28,15 +29,15 @@ type Metrics struct { RequestsPerSecond float64 `json:"requests_per_second"` // 新增字段 - TotalBytes int64 `json:"total_bytes"` - BytesPerSecond float64 `json:"bytes_per_second"` - StatusCodeStats map[string]int64 `json:"status_code_stats"` - LatencyPercentiles map[string]float64 `json:"latency_percentiles"` - TopPaths []models.PathMetrics `json:"top_paths"` - RecentRequests []models.RequestLog `json:"recent_requests"` - TopReferers []models.PathMetrics `json:"top_referers"` + TotalBytes int64 `json:"total_bytes"` + BytesPerSecond float64 `json:"bytes_per_second"` + StatusCodeStats map[string]int64 `json:"status_code_stats"` + TopPaths []models.PathMetrics `json:"top_paths"` + RecentRequests []models.RequestLog `json:"recent_requests"` + TopReferers []models.PathMetrics `json:"top_referers"` } +// MetricsHandler 处理指标请求 func (h *ProxyHandler) MetricsHandler(w http.ResponseWriter, r *http.Request) { uptime := time.Since(h.startTime) collector := metrics.GetCollector() @@ -44,7 +45,7 @@ func (h *ProxyHandler) MetricsHandler(w http.ResponseWriter, r *http.Request) { if stats == nil { stats = map[string]interface{}{ - "uptime": uptime.String(), + "uptime": metrics.FormatUptime(uptime), "active_requests": int64(0), "total_requests": int64(0), "total_errors": int64(0), @@ -69,7 +70,7 @@ func (h *ProxyHandler) MetricsHandler(w http.ResponseWriter, r *http.Request) { uptimeSeconds := uptime.Seconds() metrics := Metrics{ - Uptime: uptime.String(), + Uptime: metrics.FormatUptime(uptime), ActiveRequests: utils.SafeInt64(stats["active_requests"]), TotalRequests: totalRequests, TotalErrors: totalErrors, diff --git a/internal/metrics/collector.go b/internal/metrics/collector.go index db4e166..11723c5 100644 --- a/internal/metrics/collector.go +++ b/internal/metrics/collector.go @@ -194,19 +194,23 @@ func (c *Collector) RecordRequest(path string, status int, latency time.Duration c.recentRequestsMutex.Unlock() } -// formatUptime 格式化运行时间 -func formatUptime(d time.Duration) string { +// FormatUptime 格式化运行时间 +func FormatUptime(d time.Duration) string { days := int(d.Hours()) / 24 hours := int(d.Hours()) % 24 minutes := int(d.Minutes()) % 60 + seconds := int(d.Seconds()) % 60 if days > 0 { - return fmt.Sprintf("%d天%d小时%d分钟", days, hours, minutes) + return fmt.Sprintf("%d天%d小时%d分钟%d秒", days, hours, minutes, seconds) } if hours > 0 { - return fmt.Sprintf("%d小时%d分钟", hours, minutes) + return fmt.Sprintf("%d小时%d分钟%d秒", hours, minutes, seconds) } - return fmt.Sprintf("%d分钟", minutes) + if minutes > 0 { + return fmt.Sprintf("%d分钟%d秒", minutes, seconds) + } + return fmt.Sprintf("%d秒", seconds) } // GetStats 获取统计数据 @@ -289,7 +293,7 @@ func (c *Collector) GetStats() map[string]interface{} { } return map[string]interface{}{ - "uptime": formatUptime(time.Since(c.startTime)), + "uptime": FormatUptime(time.Since(c.startTime)), "active_requests": atomic.LoadInt64(&c.activeRequests), "total_requests": atomic.LoadInt64(&c.totalRequests), "total_errors": atomic.LoadInt64(&c.totalErrors),