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
This commit is contained in:
wood chen 2025-02-16 20:24:31 +08:00
parent 4e3a17ecd4
commit 41f5b82661
2 changed files with 20 additions and 15 deletions

View File

@ -11,6 +11,7 @@ import (
"time" "time"
) )
// Metrics 定义指标结构
type Metrics struct { type Metrics struct {
// 基础指标 // 基础指标
Uptime string `json:"uptime"` Uptime string `json:"uptime"`
@ -31,12 +32,12 @@ type Metrics struct {
TotalBytes int64 `json:"total_bytes"` TotalBytes int64 `json:"total_bytes"`
BytesPerSecond float64 `json:"bytes_per_second"` BytesPerSecond float64 `json:"bytes_per_second"`
StatusCodeStats map[string]int64 `json:"status_code_stats"` StatusCodeStats map[string]int64 `json:"status_code_stats"`
LatencyPercentiles map[string]float64 `json:"latency_percentiles"`
TopPaths []models.PathMetrics `json:"top_paths"` TopPaths []models.PathMetrics `json:"top_paths"`
RecentRequests []models.RequestLog `json:"recent_requests"` RecentRequests []models.RequestLog `json:"recent_requests"`
TopReferers []models.PathMetrics `json:"top_referers"` TopReferers []models.PathMetrics `json:"top_referers"`
} }
// MetricsHandler 处理指标请求
func (h *ProxyHandler) MetricsHandler(w http.ResponseWriter, r *http.Request) { func (h *ProxyHandler) MetricsHandler(w http.ResponseWriter, r *http.Request) {
uptime := time.Since(h.startTime) uptime := time.Since(h.startTime)
collector := metrics.GetCollector() collector := metrics.GetCollector()
@ -44,7 +45,7 @@ func (h *ProxyHandler) MetricsHandler(w http.ResponseWriter, r *http.Request) {
if stats == nil { if stats == nil {
stats = map[string]interface{}{ stats = map[string]interface{}{
"uptime": uptime.String(), "uptime": metrics.FormatUptime(uptime),
"active_requests": int64(0), "active_requests": int64(0),
"total_requests": int64(0), "total_requests": int64(0),
"total_errors": int64(0), "total_errors": int64(0),
@ -69,7 +70,7 @@ func (h *ProxyHandler) MetricsHandler(w http.ResponseWriter, r *http.Request) {
uptimeSeconds := uptime.Seconds() uptimeSeconds := uptime.Seconds()
metrics := Metrics{ metrics := Metrics{
Uptime: uptime.String(), Uptime: metrics.FormatUptime(uptime),
ActiveRequests: utils.SafeInt64(stats["active_requests"]), ActiveRequests: utils.SafeInt64(stats["active_requests"]),
TotalRequests: totalRequests, TotalRequests: totalRequests,
TotalErrors: totalErrors, TotalErrors: totalErrors,

View File

@ -194,19 +194,23 @@ func (c *Collector) RecordRequest(path string, status int, latency time.Duration
c.recentRequestsMutex.Unlock() c.recentRequestsMutex.Unlock()
} }
// formatUptime 格式化运行时间 // FormatUptime 格式化运行时间
func formatUptime(d time.Duration) string { func FormatUptime(d time.Duration) string {
days := int(d.Hours()) / 24 days := int(d.Hours()) / 24
hours := int(d.Hours()) % 24 hours := int(d.Hours()) % 24
minutes := int(d.Minutes()) % 60 minutes := int(d.Minutes()) % 60
seconds := int(d.Seconds()) % 60
if days > 0 { 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 { 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 获取统计数据 // GetStats 获取统计数据
@ -289,7 +293,7 @@ func (c *Collector) GetStats() map[string]interface{} {
} }
return 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), "active_requests": atomic.LoadInt64(&c.activeRequests),
"total_requests": atomic.LoadInt64(&c.totalRequests), "total_requests": atomic.LoadInt64(&c.totalRequests),
"total_errors": atomic.LoadInt64(&c.totalErrors), "total_errors": atomic.LoadInt64(&c.totalErrors),