proxy-go/internal/models/metrics.go
wood chen ed63121a00 refactor(metrics): Introduce JSON-friendly metrics serialization
- Create PathMetricsJSON struct for clean JSON serialization
- Add ToJSON method to convert PathMetrics to JSON-compatible representation
- Update metrics handlers and collectors to use new JSON-friendly type
- Simplify type conversion in SafePathMetrics utility function
- Improve metrics data transfer and serialization consistency
2025-02-17 08:01:29 +08:00

92 lines
2.3 KiB
Go

package models
import (
"sync/atomic"
)
type PathStats struct {
Requests atomic.Int64
Errors atomic.Int64
Bytes atomic.Int64
LatencySum atomic.Int64
}
// PathMetrics 路径统计信息
type PathMetrics struct {
Path string `json:"path"`
RequestCount atomic.Int64 `json:"request_count"`
ErrorCount atomic.Int64 `json:"error_count"`
TotalLatency atomic.Int64 `json:"-"`
BytesTransferred atomic.Int64 `json:"bytes_transferred"`
AvgLatency string `json:"avg_latency"`
}
// PathMetricsJSON 用于 JSON 序列化的路径统计信息
type PathMetricsJSON struct {
Path string `json:"path"`
RequestCount int64 `json:"request_count"`
ErrorCount int64 `json:"error_count"`
BytesTransferred int64 `json:"bytes_transferred"`
AvgLatency string `json:"avg_latency"`
}
// GetRequestCount 获取请求数
func (p *PathMetrics) GetRequestCount() int64 {
return p.RequestCount.Load()
}
// GetErrorCount 获取错误数
func (p *PathMetrics) GetErrorCount() int64 {
return p.ErrorCount.Load()
}
// GetTotalLatency 获取总延迟
func (p *PathMetrics) GetTotalLatency() int64 {
return p.TotalLatency.Load()
}
// GetBytesTransferred 获取传输字节数
func (p *PathMetrics) GetBytesTransferred() int64 {
return p.BytesTransferred.Load()
}
// AddRequest 增加请求数
func (p *PathMetrics) AddRequest() {
p.RequestCount.Add(1)
}
// AddError 增加错误数
func (p *PathMetrics) AddError() {
p.ErrorCount.Add(1)
}
// AddLatency 增加延迟
func (p *PathMetrics) AddLatency(latency int64) {
p.TotalLatency.Add(latency)
}
// AddBytes 增加传输字节数
func (p *PathMetrics) AddBytes(bytes int64) {
p.BytesTransferred.Add(bytes)
}
// ToJSON 转换为 JSON 友好的结构
func (p *PathMetrics) ToJSON() PathMetricsJSON {
return PathMetricsJSON{
Path: p.Path,
RequestCount: p.RequestCount.Load(),
ErrorCount: p.ErrorCount.Load(),
BytesTransferred: p.BytesTransferred.Load(),
AvgLatency: p.AvgLatency,
}
}
type HistoricalMetrics struct {
Timestamp string `json:"timestamp"`
TotalRequests int64 `json:"total_requests"`
TotalErrors int64 `json:"total_errors"`
TotalBytes int64 `json:"total_bytes"`
ErrorRate float64 `json:"error_rate"`
AvgLatency float64 `json:"avg_latency"`
}