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"
)
// 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,

View File

@ -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),