forked from Ehco1996/ehco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetric_man.go
180 lines (154 loc) · 4.3 KB
/
metric_man.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package cmgr
import (
"context"
"sync"
"time"
"github.com/Ehco1996/ehco/internal/conn"
"github.com/Ehco1996/ehco/internal/constant"
myhttp "github.com/Ehco1996/ehco/pkg/http"
"github.com/Ehco1996/ehco/pkg/metric_reader"
"go.uber.org/zap"
)
type StatsPerRule struct {
RelayLabel string `json:"relay_label"`
Up int64 `json:"up_bytes"`
Down int64 `json:"down_bytes"`
ConnectionCnt int `json:"connection_count"`
HandShakeLatency int64 `json:"latency_in_ms"`
}
type VersionInfo struct {
Version string `json:"version"`
ShortCommit string `json:"short_commit"`
}
type syncReq struct {
Version VersionInfo `json:"version"`
Node metric_reader.NodeMetrics `json:"node"`
Stats []StatsPerRule `json:"stats"`
}
type MetricsStore struct {
mutex sync.RWMutex
metrics []metric_reader.NodeMetrics
bufSize int
clearDuration time.Duration
}
func NewMetricsStore(bufSize int, clearDuration time.Duration) *MetricsStore {
return &MetricsStore{
metrics: make([]metric_reader.NodeMetrics, bufSize),
clearDuration: clearDuration,
bufSize: bufSize,
}
}
func (ms *MetricsStore) Add(m *metric_reader.NodeMetrics) {
ms.mutex.Lock()
defer ms.mutex.Unlock()
// 直接添加新的 metric,假设它是最新的
ms.metrics = append(ms.metrics, *m)
// 清理旧数据
cutoffTime := time.Now().Add(-ms.clearDuration)
for i, metric := range ms.metrics {
if metric.SyncTime.After(cutoffTime) {
ms.metrics = ms.metrics[i:]
break
}
}
}
func (ms *MetricsStore) Query(startTime, endTime time.Time) []metric_reader.NodeMetrics {
ms.mutex.RLock()
defer ms.mutex.RUnlock()
var result []metric_reader.NodeMetrics
for i := len(ms.metrics) - 1; i >= 0; i-- {
if ms.metrics[i].SyncTime.Before(startTime) {
break
}
if !ms.metrics[i].SyncTime.After(endTime) {
result = append(result, ms.metrics[i])
}
}
// 反转结果,使其按时间升序排列
for i := 0; i < len(result)/2; i++ {
j := len(result) - 1 - i
result[i], result[j] = result[j], result[i]
}
return result
}
type QueryNodeMetricsReq struct {
TimeRange string `json:"time_range"` // 15min/30min/1h/6h/12h/24h
Latest bool `json:"latest"` // whether to refresh the cache and get the latest data
}
func (cm *cmgrImpl) syncOnce(ctx context.Context) error {
cm.l.Infof("sync once total closed connections: %d", cm.countClosedConnection())
// todo: opt lock
cm.lock.Lock()
shortCommit := constant.GitRevision
if len(constant.GitRevision) > 7 {
shortCommit = constant.GitRevision[:7]
}
req := syncReq{
Stats: []StatsPerRule{},
Version: VersionInfo{Version: constant.Version, ShortCommit: shortCommit},
}
if cm.cfg.NeedMetrics() {
metrics, err := cm.mr.ReadOnce(ctx)
if err != nil {
cm.l.Errorf("read metrics failed: %v", err)
} else {
req.Node = *metrics
cm.ms.Add(metrics)
}
}
for label, conns := range cm.closedConnectionsMap {
s := StatsPerRule{
RelayLabel: label,
}
var totalLatency int64
for _, c := range conns {
s.ConnectionCnt++
s.Up += c.GetStats().Up
s.Down += c.GetStats().Down
totalLatency += c.GetStats().HandShakeLatency.Milliseconds()
}
if s.ConnectionCnt > 0 {
s.HandShakeLatency = totalLatency / int64(s.ConnectionCnt)
}
req.Stats = append(req.Stats, s)
}
cm.closedConnectionsMap = make(map[string][]conn.RelayConn)
cm.lock.Unlock()
if cm.cfg.NeedSync() {
cm.l.Debug("syncing data to server", zap.Any("data", req))
return myhttp.PostJSONWithRetry(cm.cfg.SyncURL, &req)
} else {
cm.l.Debugf("remove %d closed connections", len(req.Stats))
}
return nil
}
func getTimeRangeDuration(timeRange string) time.Duration {
switch timeRange {
case "15min":
return 15 * time.Minute
case "30min":
return 30 * time.Minute
case "1h":
return 1 * time.Hour
case "6h":
return 6 * time.Hour
case "12h":
return 12 * time.Hour
case "24h":
return 24 * time.Hour
default:
return 15 * time.Minute
}
}
func (cm *cmgrImpl) QueryNodeMetrics(ctx context.Context, req *QueryNodeMetricsReq) ([]metric_reader.NodeMetrics, error) {
if req.Latest {
m, err := cm.mr.ReadOnce(ctx)
if err != nil {
return nil, err
}
cm.ms.Add(m)
return []metric_reader.NodeMetrics{*m}, nil
}
startTime := time.Now().Add(-getTimeRangeDuration(req.TimeRange))
return cm.ms.Query(startTime, time.Now()), nil
}