mirror of
https://github.com/woodchen-ink/random-api-go.git
synced 2025-07-18 13:52:02 +08:00
refactor(monitoring, public): enhance system metrics and update display logic
- Streamlined SystemMetrics by removing unnecessary fields and focusing on essential metrics such as AverageLatency and MemoryStats. - Updated LogRequest to improve handling of referers and average latency calculations, ensuring accurate metrics collection. - Enhanced the HTML display to include a dedicated section for system metrics, ensuring graceful handling of undefined values. - Improved the presentation of top referers with a time-based reset mechanism, contributing to a cleaner and more efficient user interface.
This commit is contained in:
parent
8d0d502910
commit
813d87531f
@ -1,11 +1,9 @@
|
|||||||
package monitoring
|
package monitoring
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -17,68 +15,56 @@ type SystemMetrics struct {
|
|||||||
// 系统指标
|
// 系统指标
|
||||||
NumCPU int `json:"num_cpu"`
|
NumCPU int `json:"num_cpu"`
|
||||||
NumGoroutine int `json:"num_goroutine"`
|
NumGoroutine int `json:"num_goroutine"`
|
||||||
|
AverageLatency float64 `json:"average_latency"`
|
||||||
MemoryStats struct {
|
MemoryStats struct {
|
||||||
Alloc uint64 `json:"alloc"`
|
|
||||||
TotalAlloc uint64 `json:"total_alloc"`
|
|
||||||
Sys uint64 `json:"sys"`
|
|
||||||
HeapAlloc uint64 `json:"heap_alloc"`
|
HeapAlloc uint64 `json:"heap_alloc"`
|
||||||
HeapSys uint64 `json:"heap_sys"`
|
HeapSys uint64 `json:"heap_sys"`
|
||||||
} `json:"memory_stats"`
|
} `json:"memory_stats"`
|
||||||
|
|
||||||
// 性能指标
|
|
||||||
RequestCount atomic.Int64 `json:"request_count"`
|
|
||||||
AverageLatency float64 `json:"average_latency"`
|
|
||||||
|
|
||||||
// 流量统计
|
|
||||||
TotalBytesIn int64 `json:"total_bytes_in"`
|
|
||||||
TotalBytesOut int64 `json:"total_bytes_out"`
|
|
||||||
|
|
||||||
// 状态码统计
|
|
||||||
StatusCodes map[int]int64 `json:"status_codes"`
|
|
||||||
|
|
||||||
// 路径延迟统计
|
|
||||||
PathLatencies map[string]float64 `json:"path_latencies"`
|
|
||||||
|
|
||||||
// 热门引用来源
|
// 热门引用来源
|
||||||
TopReferers map[string]int64 `json:"top_referers"`
|
TopReferers sync.Map `json:"-"` // 使用 sync.Map 足够了
|
||||||
|
|
||||||
// 添加性能监控指标
|
|
||||||
GCStats struct {
|
|
||||||
NumGC uint32 `json:"num_gc"`
|
|
||||||
PauseTotal float64 `json:"pause_total"`
|
|
||||||
PauseAvg float64 `json:"pause_avg"`
|
|
||||||
} `json:"gc_stats"`
|
|
||||||
|
|
||||||
CPUUsage float64 `json:"cpu_usage"`
|
|
||||||
ThreadCount int `json:"thread_count"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type RequestLog struct {
|
type RequestLog struct {
|
||||||
Time int64 `json:"time"` // 使用 Unix 时间戳
|
Time int64 `json:"time"`
|
||||||
Path string `json:"path"` // 考虑使用字符串池
|
Path string `json:"path"`
|
||||||
Method string `json:"method"` // 使用常量池
|
Method string `json:"method"`
|
||||||
StatusCode int `json:"status_code"`
|
StatusCode int `json:"status_code"`
|
||||||
Latency float64 `json:"latency"` // 改回 float64,保持一致性
|
Latency float64 `json:"latency"`
|
||||||
IP string `json:"ip"`
|
IP string `json:"ip"`
|
||||||
Referer string `json:"referer"`
|
Referer string `json:"referer"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
metrics SystemMetrics
|
metrics SystemMetrics
|
||||||
mu sync.RWMutex
|
|
||||||
startTime = time.Now()
|
startTime = time.Now()
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
metrics.StatusCodes = make(map[int]int64)
|
// 定期清理引用来源
|
||||||
metrics.PathLatencies = make(map[string]float64)
|
go func() {
|
||||||
metrics.TopReferers = make(map[string]int64)
|
ticker := time.NewTicker(5 * time.Minute)
|
||||||
|
for range ticker.C {
|
||||||
|
metrics.TopReferers = sync.Map{} // 直接重置
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
func LogRequest(log RequestLog) {
|
||||||
|
// 更新引用来源
|
||||||
|
if log.Referer != "direct" {
|
||||||
|
metrics.TopReferers.Store(log.Referer, 1)
|
||||||
|
} else {
|
||||||
|
metrics.TopReferers.Store("直接访问", 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新平均延迟 (只关心 API 请求)
|
||||||
|
if strings.HasPrefix(log.Path, "/pic/") || strings.HasPrefix(log.Path, "/video/") {
|
||||||
|
metrics.AverageLatency = (metrics.AverageLatency + log.Latency) / 2
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func CollectMetrics() *SystemMetrics {
|
func CollectMetrics() *SystemMetrics {
|
||||||
mu.Lock()
|
|
||||||
defer mu.Unlock()
|
|
||||||
|
|
||||||
var m runtime.MemStats
|
var m runtime.MemStats
|
||||||
runtime.ReadMemStats(&m)
|
runtime.ReadMemStats(&m)
|
||||||
|
|
||||||
@ -86,74 +72,8 @@ func CollectMetrics() *SystemMetrics {
|
|||||||
metrics.StartTime = startTime
|
metrics.StartTime = startTime
|
||||||
metrics.NumCPU = runtime.NumCPU()
|
metrics.NumCPU = runtime.NumCPU()
|
||||||
metrics.NumGoroutine = runtime.NumGoroutine()
|
metrics.NumGoroutine = runtime.NumGoroutine()
|
||||||
|
|
||||||
metrics.MemoryStats.Alloc = m.Alloc
|
|
||||||
metrics.MemoryStats.TotalAlloc = m.TotalAlloc
|
|
||||||
metrics.MemoryStats.Sys = m.Sys
|
|
||||||
metrics.MemoryStats.HeapAlloc = m.HeapAlloc
|
metrics.MemoryStats.HeapAlloc = m.HeapAlloc
|
||||||
metrics.MemoryStats.HeapSys = m.HeapSys
|
metrics.MemoryStats.HeapSys = m.HeapSys
|
||||||
|
|
||||||
return &metrics
|
return &metrics
|
||||||
}
|
}
|
||||||
|
|
||||||
func LogRequest(log RequestLog) {
|
|
||||||
metrics.RequestCount.Add(1)
|
|
||||||
|
|
||||||
mu.Lock() // 添加全局锁保护 map 写入
|
|
||||||
metrics.StatusCodes[log.StatusCode]++
|
|
||||||
|
|
||||||
// 直接使用完整的 referer
|
|
||||||
if log.Referer != "direct" {
|
|
||||||
metrics.TopReferers[log.Referer]++
|
|
||||||
} else {
|
|
||||||
metrics.TopReferers["直接访问"]++
|
|
||||||
}
|
|
||||||
mu.Unlock()
|
|
||||||
|
|
||||||
// 只记录 API 请求
|
|
||||||
if strings.HasPrefix(log.Path, "/pic/") || strings.HasPrefix(log.Path, "/video/") {
|
|
||||||
// 更新路径延迟
|
|
||||||
mu.Lock() // 保护 PathLatencies map
|
|
||||||
if existing, ok := metrics.PathLatencies[log.Path]; ok {
|
|
||||||
metrics.PathLatencies[log.Path] = (existing + log.Latency) / 2
|
|
||||||
} else {
|
|
||||||
metrics.PathLatencies[log.Path] = log.Latency
|
|
||||||
}
|
|
||||||
mu.Unlock()
|
|
||||||
|
|
||||||
// 更新平均延迟
|
|
||||||
count := metrics.RequestCount.Load()
|
|
||||||
if count > 1 {
|
|
||||||
metrics.AverageLatency = (metrics.AverageLatency*(float64(count)-1) + log.Latency) / float64(count)
|
|
||||||
} else {
|
|
||||||
metrics.AverageLatency = log.Latency
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 添加分段锁结构
|
|
||||||
type bucket struct {
|
|
||||||
mu sync.Mutex
|
|
||||||
}
|
|
||||||
|
|
||||||
var buckets = make([]bucket, 32)
|
|
||||||
|
|
||||||
func getBucket(path string) *bucket {
|
|
||||||
hash := uint32(0)
|
|
||||||
for i := 0; i < len(path); i++ {
|
|
||||||
hash = hash*31 + uint32(path[i])
|
|
||||||
}
|
|
||||||
return &buckets[hash%32]
|
|
||||||
}
|
|
||||||
|
|
||||||
// 添加 MarshalJSON 方法
|
|
||||||
func (m *SystemMetrics) MarshalJSON() ([]byte, error) {
|
|
||||||
type Alias SystemMetrics
|
|
||||||
return json.Marshal(&struct {
|
|
||||||
RequestCount int64 `json:"request_count"`
|
|
||||||
*Alias
|
|
||||||
}{
|
|
||||||
RequestCount: m.RequestCount.Load(),
|
|
||||||
Alias: (*Alias)(m),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
<body>
|
<body>
|
||||||
<div class="overlay">
|
<div class="overlay">
|
||||||
<main>
|
<main>
|
||||||
|
<div id="system-metrics"></div>
|
||||||
<div id="markdown-content" class="prose prose-dark">
|
<div id="markdown-content" class="prose prose-dark">
|
||||||
</div>
|
</div>
|
||||||
</main>
|
</main>
|
||||||
@ -218,62 +219,31 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function updateMetricsDisplay(metrics) {
|
function updateMetricsDisplay(metrics) {
|
||||||
// 格式化延迟显示
|
|
||||||
function formatLatency(microseconds) {
|
|
||||||
if (microseconds < 1000) {
|
|
||||||
return `${microseconds.toFixed(3)}µs`;
|
|
||||||
}
|
|
||||||
if (microseconds < 1000000) {
|
|
||||||
return `${(microseconds/1000).toFixed(3)}ms`;
|
|
||||||
}
|
|
||||||
return `${(microseconds/1000000).toFixed(3)}s`;
|
|
||||||
}
|
|
||||||
|
|
||||||
const metricsHtml = `
|
const metricsHtml = `
|
||||||
<div class="metrics-container">
|
<div class="metrics-container">
|
||||||
<div class="metrics-section">
|
<div class="metrics-section">
|
||||||
<h3>基础指标</h3>
|
<h3>基础指标</h3>
|
||||||
<div class="metrics-grid">
|
<div class="metrics-grid">
|
||||||
<div class="metric-item">运行时间:${formatDuration(metrics.uptime)}</div>
|
<div class="metric-item">运行时间:${formatDuration(metrics?.uptime || 0)}</div>
|
||||||
<div class="metric-item">启动时间:${new Date(metrics.start_time).toLocaleString()}</div>
|
<div class="metric-item">启动时间:${new Date(metrics?.start_time || Date.now()).toLocaleString()}</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="metrics-section">
|
<div class="metrics-section">
|
||||||
<h3>系统指标</h3>
|
<h3>系统指标</h3>
|
||||||
<div class="metrics-grid">
|
<div class="metrics-grid">
|
||||||
<div class="metric-item">CPU核心数:${metrics.num_cpu}</div>
|
<div class="metric-item">CPU核心数:${metrics?.num_cpu || 0}</div>
|
||||||
<div class="metric-item">Goroutine数:${metrics.num_goroutine}</div>
|
<div class="metric-item">Goroutine数:${metrics?.num_goroutine || 0}</div>
|
||||||
<div class="metric-item">内存使用:${formatBytes(metrics.memory_stats.heap_alloc)}</div>
|
<div class="metric-item">内存使用:${formatBytes(metrics?.memory_stats?.heap_alloc || 0)}</div>
|
||||||
<div class="metric-item">系统内存:${formatBytes(metrics.memory_stats.heap_sys)}</div>
|
<div class="metric-item">系统内存:${formatBytes(metrics?.memory_stats?.heap_sys || 0)}</div>
|
||||||
|
<div class="metric-item">平均延迟:${formatLatency(metrics?.average_latency || 0)}</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="metrics-section">
|
<div class="metrics-section">
|
||||||
<h3>性能指标</h3>
|
<h3>热门引用来源 (5分钟统计)</h3>
|
||||||
<div class="metrics-grid">
|
|
||||||
<div class="metric-item">总请求数:${metrics.request_count}</div>
|
|
||||||
<div class="metric-item">平均延迟:${formatLatency(metrics.average_latency)}</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="metrics-section">
|
|
||||||
<h3>状态码统计</h3>
|
|
||||||
<div class="status-codes">
|
|
||||||
${Object.entries(metrics.status_codes)
|
|
||||||
.map(([code, count]) => `
|
|
||||||
<div class="status-code-item">
|
|
||||||
<span class="code">${code}</span>
|
|
||||||
<span class="count">${count}</span>
|
|
||||||
</div>
|
|
||||||
`).join('')}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="metrics-section">
|
|
||||||
<h3>热门引用来源</h3>
|
|
||||||
<div class="top-referers">
|
<div class="top-referers">
|
||||||
${Object.entries(metrics.top_referers)
|
${metrics?.top_referers ? Object.entries(metrics.top_referers)
|
||||||
.sort(([, a], [, b]) => b - a)
|
.sort(([, a], [, b]) => b - a)
|
||||||
.slice(0, 10)
|
.slice(0, 10)
|
||||||
.map(([referer, count]) => `
|
.map(([referer, count]) => `
|
||||||
@ -281,7 +251,7 @@
|
|||||||
<span class="referer">${referer}</span>
|
<span class="referer">${referer}</span>
|
||||||
<span class="count">${count}</span>
|
<span class="count">${count}</span>
|
||||||
</div>
|
</div>
|
||||||
`).join('')}
|
`).join('') : ''}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user