mirror of
https://github.com/woodchen-ink/random-api-go.git
synced 2025-07-18 05:42:01 +08:00
- Simplified MetricsMiddleware by removing unnecessary comments and focusing on essential request logging. - Updated RequestLog structure to exclude the Referer field from JSON serialization, enhancing data privacy. - Removed top referers tracking and associated HTML/CSS elements to declutter the user interface and improve performance. - Cleaned up CSS styles related to referers for a more streamlined design.
59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
package monitoring
|
|
|
|
import (
|
|
"runtime"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type SystemMetrics struct {
|
|
// 基础指标
|
|
Uptime time.Duration `json:"uptime"`
|
|
StartTime time.Time `json:"start_time"`
|
|
|
|
// 系统指标
|
|
NumCPU int `json:"num_cpu"`
|
|
NumGoroutine int `json:"num_goroutine"`
|
|
AverageLatency float64 `json:"average_latency"`
|
|
MemoryStats struct {
|
|
HeapAlloc uint64 `json:"heap_alloc"`
|
|
HeapSys uint64 `json:"heap_sys"`
|
|
} `json:"memory_stats"`
|
|
}
|
|
|
|
type RequestLog struct {
|
|
Time int64 `json:"time"`
|
|
Path string `json:"path"`
|
|
Method string `json:"method"`
|
|
StatusCode int `json:"status_code"`
|
|
Latency float64 `json:"latency"`
|
|
IP string `json:"ip"`
|
|
Referer string `json:"-"`
|
|
}
|
|
|
|
var (
|
|
metrics SystemMetrics
|
|
startTime = time.Now()
|
|
)
|
|
|
|
func LogRequest(log RequestLog) {
|
|
// 更新平均延迟 (只关心 API 请求)
|
|
if strings.HasPrefix(log.Path, "/pic/") || strings.HasPrefix(log.Path, "/video/") {
|
|
metrics.AverageLatency = (metrics.AverageLatency + log.Latency) / 2
|
|
}
|
|
}
|
|
|
|
func CollectMetrics() *SystemMetrics {
|
|
var m runtime.MemStats
|
|
runtime.ReadMemStats(&m)
|
|
|
|
metrics.Uptime = time.Since(startTime)
|
|
metrics.StartTime = startTime
|
|
metrics.NumCPU = runtime.NumCPU()
|
|
metrics.NumGoroutine = runtime.NumGoroutine()
|
|
metrics.MemoryStats.HeapAlloc = m.HeapAlloc
|
|
metrics.MemoryStats.HeapSys = m.HeapSys
|
|
|
|
return &metrics
|
|
}
|