forked from tair-opensource/RedisShake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metric.go
256 lines (208 loc) · 5.42 KB
/
metric.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package metric
import (
"encoding/json"
"fmt"
"math"
"strconv"
"sync"
"sync/atomic"
"time"
"pkg/libs/log"
"redis-shake/base"
"redis-shake/configure"
)
const (
updateInterval = 10 // seconds
)
var (
MetricMap = new(sync.Map)
runner base.Runner
)
type Op interface {
Update()
}
type Percent struct {
Dividend uint64
Divisor uint64
}
func (p *Percent) Set(dividend, divisor uint64) {
atomic.AddUint64(&p.Dividend, dividend)
atomic.AddUint64(&p.Divisor, divisor)
}
// input: return string?
func (p *Percent) Get(returnString bool) interface{} {
if divisor := atomic.LoadUint64(&p.Divisor); divisor == 0 {
if returnString {
return "null"
} else {
return math.MaxFloat64
}
} else {
dividend := atomic.LoadUint64(&p.Dividend)
if returnString {
return fmt.Sprintf("%.02f", float64(dividend)/float64(divisor))
} else {
return float64(dividend) / float64(divisor)
}
}
}
func (p *Percent) Update() {
p.Dividend = 0
p.Divisor = 0
}
type Delta struct {
Value uint64 // current value
}
func (d *Delta) Update() {
d.Value = 0
}
type Combine struct {
Total uint64 // total number
Delta // delta
}
func (c *Combine) Set(val uint64) {
atomic.AddUint64(&c.Delta.Value, val)
atomic.AddUint64(&(c.Total), val)
}
// main struct
type Metric struct {
PullCmdCount Combine
BypassCmdCount Combine
PushCmdCount Combine
SuccessCmdCount Combine
FailCmdCount Combine
Delay Percent // ms
AvgDelay Percent // ms
NetworkFlow Combine // +speed
FullSyncProgress uint64
}
func CreateMetric(r base.Runner) {
runner = r
}
func AddMetric(id int) {
if _, ok := MetricMap.Load(id); ok {
return
}
singleMetric := new(Metric)
MetricMap.Store(id, singleMetric)
go singleMetric.run()
}
func GetMetric(id int) *Metric {
metric, _ := MetricMap.Load(id)
return metric.(*Metric)
}
func (m *Metric) resetEverySecond(items []Op) {
for _, item := range items {
item.Update()
}
}
func (m *Metric) run() {
resetItems := []Op{
&m.PullCmdCount.Delta,
&m.BypassCmdCount.Delta,
&m.PushCmdCount.Delta,
&m.SuccessCmdCount.Delta,
&m.FailCmdCount.Delta,
&m.Delay,
&m.NetworkFlow.Delta,
}
go func() {
tick := 0
for range time.NewTicker(1 * time.Second).C {
tick++
if tick%updateInterval == 0 && conf.Options.MetricPrintLog {
stat := NewMetricRest()
if opts, err := json.Marshal(stat); err != nil {
log.Infof("marshal metric stat error[%v]", err)
} else {
log.Info(string(opts))
}
}
m.resetEverySecond(resetItems)
}
}()
}
func (m *Metric) AddPullCmdCount(dbSyncerID int, val uint64) {
m.PullCmdCount.Set(val)
pullCmdCountTotal.WithLabelValues(strconv.Itoa(dbSyncerID)).Add(float64(val))
}
func (m *Metric) GetPullCmdCount() interface{} {
return atomic.LoadUint64(&m.PullCmdCount.Value)
}
func (m *Metric) GetPullCmdCountTotal() interface{} {
return atomic.LoadUint64(&m.PullCmdCount.Total)
}
func (m *Metric) AddBypassCmdCount(dbSyncerID int, val uint64) {
m.BypassCmdCount.Set(val)
bypassCmdCountTotal.WithLabelValues(strconv.Itoa(dbSyncerID)).Add(float64(val))
}
func (m *Metric) GetBypassCmdCount() interface{} {
return atomic.LoadUint64(&m.BypassCmdCount.Value)
}
func (m *Metric) GetBypassCmdCountTotal() interface{} {
return atomic.LoadUint64(&m.BypassCmdCount.Total)
}
func (m *Metric) AddPushCmdCount(dbSyncerID int, val uint64) {
m.PushCmdCount.Set(val)
pushCmdCountTotal.WithLabelValues(strconv.Itoa(dbSyncerID)).Add(float64(val))
}
func (m *Metric) GetPushCmdCount() interface{} {
return atomic.LoadUint64(&m.PushCmdCount.Value)
}
func (m *Metric) GetPushCmdCountTotal() interface{} {
return atomic.LoadUint64(&m.PushCmdCount.Total)
}
func (m *Metric) AddSuccessCmdCount(dbSyncerID int, val uint64) {
m.SuccessCmdCount.Set(val)
successCmdCountTotal.WithLabelValues(strconv.Itoa(dbSyncerID)).Add(float64(val))
}
func (m *Metric) GetSuccessCmdCount() interface{} {
return atomic.LoadUint64(&m.SuccessCmdCount.Value)
}
func (m *Metric) GetSuccessCmdCountTotal() interface{} {
return atomic.LoadUint64(&m.SuccessCmdCount.Total)
}
func (m *Metric) AddFailCmdCount(dbSyncerID int, val uint64) {
m.FailCmdCount.Set(val)
failCmdCountTotal.WithLabelValues(strconv.Itoa(dbSyncerID)).Add(float64(val))
}
func (m *Metric) GetFailCmdCount() interface{} {
return atomic.LoadUint64(&m.FailCmdCount.Value)
}
func (m *Metric) GetFailCmdCountTotal() interface{} {
return atomic.LoadUint64(&m.FailCmdCount.Total)
}
func (m *Metric) AddDelay(val uint64) {
m.Delay.Set(val, 1)
m.AvgDelay.Set(val, 1)
}
func (m *Metric) GetDelay() interface{} {
return m.Delay.Get(true)
}
func (m *Metric) GetAvgDelay() interface{} {
return m.AvgDelay.Get(true)
}
func (m *Metric) GetAvgDelayFloat64() float64 {
if avgDelay, ok := m.AvgDelay.Get(false).(float64); ok {
return avgDelay
}
return math.MaxFloat64
}
func (m *Metric) AddNetworkFlow(dbSyncerID int, val uint64) {
// atomic.AddUint64(&m.NetworkFlow.Value, val)
m.NetworkFlow.Set(val)
networkFlowTotalInBytes.WithLabelValues(strconv.Itoa(dbSyncerID)).Add(float64(val))
}
func (m *Metric) GetNetworkFlow() interface{} {
return atomic.LoadUint64(&m.NetworkFlow.Value)
}
func (m *Metric) GetNetworkFlowTotal() interface{} {
return atomic.LoadUint64(&m.NetworkFlow.Total)
}
func (m *Metric) SetFullSyncProgress(dbSyncerID int, val uint64) {
m.FullSyncProgress = val
fullSyncProcessPercent.WithLabelValues(strconv.Itoa(dbSyncerID)).Set(float64(val))
}
func (m *Metric) GetFullSyncProgress() interface{} {
return m.FullSyncProgress
}