-
Notifications
You must be signed in to change notification settings - Fork 104
/
aggregating_meter.go
279 lines (239 loc) · 6.67 KB
/
aggregating_meter.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package gocb
import (
"encoding/json"
"fmt"
"math"
"sync"
"sync/atomic"
"time"
)
type aggregatingMeterGroup struct {
lock sync.Mutex
recorders map[string]*aggregatingValueRecorder
}
func (amg *aggregatingMeterGroup) Recorders() []*aggregatingValueRecorder {
amg.lock.Lock()
if len(amg.recorders) == 0 {
amg.lock.Unlock()
return []*aggregatingValueRecorder{}
}
recorders := make([]*aggregatingValueRecorder, len(amg.recorders))
var i int
for _, r := range amg.recorders {
recorders[i] = r
i++
}
amg.lock.Unlock()
return recorders
}
// AggregatingMeter is a Meter implementation providing a simplified, but useful, view into current SDK state.
type AggregatingMeter struct {
interval time.Duration
valueRecorderGroups map[string]*aggregatingMeterGroup
stopCh chan struct{}
}
type AggregatingMeterOptions struct {
EmitInterval time.Duration
}
func NewAggregatingMeter(opts *AggregatingMeterOptions) *AggregatingMeter {
if opts == nil {
opts = &AggregatingMeterOptions{}
}
interval := opts.EmitInterval
if interval == 0 {
interval = 10 * time.Minute
}
am := &AggregatingMeter{
interval: interval,
valueRecorderGroups: map[string]*aggregatingMeterGroup{
meterValueServiceKV: {
recorders: make(map[string]*aggregatingValueRecorder),
},
meterValueServiceViews: {
recorders: make(map[string]*aggregatingValueRecorder),
},
meterValueServiceQuery: {
recorders: make(map[string]*aggregatingValueRecorder),
},
meterValueServiceSearch: {
recorders: make(map[string]*aggregatingValueRecorder),
},
meterValueServiceAnalytics: {
recorders: make(map[string]*aggregatingValueRecorder),
},
meterValueServiceManagement: {
recorders: make(map[string]*aggregatingValueRecorder),
},
},
stopCh: make(chan struct{}),
}
return am
}
func (am *AggregatingMeter) startLoggerRoutine() {
go am.loggerRoutine()
}
func (am *AggregatingMeter) loggerRoutine() {
for {
select {
case <-am.stopCh:
return
case <-time.After(am.interval):
}
jsonData := am.generateOutput()
if len(jsonData) == 1 {
// Nothing to log so make sure we don't just log empty objects.
return
}
jsonBytes, err := json.Marshal(jsonData)
if err != nil {
logDebugf("Failed to generate threshold logging service JSON: %s", err)
}
logInfof("Aggregate metrics: %s", jsonBytes)
}
}
func (am *AggregatingMeter) generateOutput() map[string]interface{} {
output := make(map[string]interface{})
output["meta"] = map[string]interface{}{
"emit_interval_s": am.interval,
}
for serviceName, group := range am.valueRecorderGroups {
serviceMap := make(map[string]interface{})
recorders := group.Recorders()
if len(recorders) == 0 {
continue
}
for _, recorder := range recorders {
serviceMap[recorder.peerName] = recorder.GetAndResetValues()
}
if len(serviceMap) > 0 {
output[serviceName] = serviceMap
}
}
return output
}
func (am *AggregatingMeter) Counter(_ string, _ map[string]string) (Counter, error) {
return defaultNoopCounter, nil
}
func (am *AggregatingMeter) ValueRecorder(name string, tags map[string]string) (ValueRecorder, error) {
if name != meterNameResponses {
return defaultNoopValueRecorder, nil
}
service, ok := tags[meterAttribServiceKey]
if !ok {
return defaultNoopValueRecorder, nil
}
if _, ok := am.valueRecorderGroups[service]; !ok {
return defaultNoopValueRecorder, nil
}
peerName, ok := tags[meterAttribPeerName]
if !ok {
return defaultNoopValueRecorder, nil
}
// We don't need to lock around accessing recorder groups itself, it must never be modified.
recorderGroup := am.valueRecorderGroups[service]
recorderGroup.lock.Lock()
recorder := recorderGroup.recorders[peerName]
if recorder == nil {
recorder = newAggregatingValueRecorder(peerName)
recorderGroup.recorders[peerName] = recorder
}
recorderGroup.lock.Unlock()
return recorder, nil
}
func (am *AggregatingMeter) close() {
am.stopCh <- struct{}{}
}
type latencyHistogram struct {
bins []uint64
maxValue float64
scaleFactor float64
ratioLog float64
commonRatio float64
startValue float64
}
type cumulativeLatencyHistogram struct {
bins []uint64
commonRatio float64
startValue float64
}
func newLatencyHistogram(maxValue, startValue float64, commonRatio float64) *latencyHistogram {
ratio := math.Log(commonRatio)
// We plus two so that values > maxValue and values <= startValue will have a bin to go into
numBuckets := math.Ceil(math.Log(maxValue/startValue)/ratio) + 2
return &latencyHistogram{
bins: make([]uint64, int(numBuckets)),
maxValue: maxValue,
scaleFactor: startValue,
ratioLog: ratio,
startValue: startValue,
commonRatio: commonRatio,
}
}
func (lh *latencyHistogram) RecordValue(value uint64) {
var bin int
v := float64(value)
if v > lh.maxValue {
bin = len(lh.bins) - 1
} else if v <= lh.scaleFactor {
bin = 0
} else {
bin = int(math.Ceil(math.Log(v/lh.scaleFactor) / lh.ratioLog))
}
atomic.AddUint64(&lh.bins[bin], 1)
}
func (lh *latencyHistogram) AggregateAndReset() *cumulativeLatencyHistogram {
bins := make([]uint64, len(lh.bins))
var countSoFar uint64
for i := 0; i < len(lh.bins); i++ {
thisCount := atomic.SwapUint64(&lh.bins[i], 0)
countSoFar += thisCount
bins[i] = countSoFar
}
return &cumulativeLatencyHistogram{
bins: bins,
commonRatio: lh.commonRatio,
startValue: lh.startValue,
}
}
func (lhs *cumulativeLatencyHistogram) TotalCount() uint64 {
return lhs.bins[len(lhs.bins)-1]
}
func (lhs *cumulativeLatencyHistogram) BinAtPercentile(percentile float64) string {
c := lhs.TotalCount()
count := uint64(math.Ceil((percentile / 100) * float64(c)))
for i, bin := range lhs.bins {
if bin >= count {
if i == len(lhs.bins)-1 {
return fmt.Sprintf("> %.2f", math.Pow(lhs.commonRatio, float64(i-1))*lhs.startValue)
}
return fmt.Sprintf("<= %.2f", math.Pow(lhs.commonRatio, float64(i))*lhs.startValue)
}
}
return "0.0"
}
type aggregatingValueRecorder struct {
peerName string
hist *latencyHistogram
}
func newAggregatingValueRecorder(peerName string) *aggregatingValueRecorder {
return &aggregatingValueRecorder{
peerName: peerName,
hist: newLatencyHistogram(2000000, 1000, 1.5),
}
}
func (bc *aggregatingValueRecorder) RecordValue(val uint64) {
bc.hist.RecordValue(val)
}
func (bc *aggregatingValueRecorder) GetAndResetValues() map[string]interface{} {
hist := bc.hist.AggregateAndReset()
return map[string]interface{}{
"total_count": hist.TotalCount(),
"percentiles_us": map[string]string{
"50.0": hist.BinAtPercentile(50.0),
"90.0": hist.BinAtPercentile(90.0),
"99.0": hist.BinAtPercentile(99.0),
"99.9": hist.BinAtPercentile(99.9),
"100.0": hist.BinAtPercentile(100),
},
}
}