-
Notifications
You must be signed in to change notification settings - Fork 133
/
aggregator.go
258 lines (216 loc) · 5.87 KB
/
aggregator.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
package statsd
import (
"strings"
"sync"
"sync/atomic"
"time"
)
type (
countsMap map[string]*countMetric
gaugesMap map[string]*gaugeMetric
setsMap map[string]*setMetric
histogramMap map[string]*histogramMetric
distributionMap map[string]*distributionMetric
)
type aggregator struct {
nbContextGauge int32
nbContextCount int32
nbContextSet int32
nbContextHistogram int32
nbContextDistribution int32
countsM sync.RWMutex
gaugesM sync.RWMutex
setsM sync.RWMutex
histogramsM sync.RWMutex
distributionM sync.RWMutex
gauges gaugesMap
counts countsMap
sets setsMap
histograms histogramMap
distributions distributionMap
closed chan struct{}
exited chan struct{}
client *Client
}
type aggregatorMetrics struct {
nbContext int32
nbContextGauge int32
nbContextCount int32
nbContextSet int32
nbContextHistogram int32
nbContextDistribution int32
}
func newAggregator(c *Client) *aggregator {
return &aggregator{
client: c,
counts: countsMap{},
gauges: gaugesMap{},
sets: setsMap{},
histograms: histogramMap{},
distributions: distributionMap{},
closed: make(chan struct{}),
exited: make(chan struct{}),
}
}
func (a *aggregator) start(flushInterval time.Duration) {
ticker := time.NewTicker(flushInterval)
go func() {
for {
select {
case <-ticker.C:
a.sendMetrics()
case <-a.closed:
close(a.exited)
return
}
}
}()
}
func (a *aggregator) sendMetrics() {
for _, m := range a.flushMetrics() {
a.client.send(m)
}
}
func (a *aggregator) stop() {
close(a.closed)
<-a.exited
a.sendMetrics()
}
func (a *aggregator) flushTelemetryMetrics() *aggregatorMetrics {
if a == nil {
return nil
}
am := &aggregatorMetrics{
nbContextGauge: atomic.SwapInt32(&a.nbContextGauge, 0),
nbContextCount: atomic.SwapInt32(&a.nbContextCount, 0),
nbContextSet: atomic.SwapInt32(&a.nbContextSet, 0),
nbContextHistogram: atomic.SwapInt32(&a.nbContextHistogram, 0),
nbContextDistribution: atomic.SwapInt32(&a.nbContextDistribution, 0),
}
am.nbContext = am.nbContextGauge + am.nbContextCount + am.nbContextSet + am.nbContextHistogram + am.nbContextDistribution
return am
}
func (a *aggregator) flushMetrics() []metric {
metrics := []metric{}
// We reset the values to avoid sending 'zero' values for metrics not
// sampled during this flush interval
a.setsM.Lock()
sets := a.sets
a.sets = setsMap{}
a.setsM.Unlock()
for _, s := range sets {
metrics = append(metrics, s.flushUnsafe()...)
}
a.gaugesM.Lock()
gauges := a.gauges
a.gauges = gaugesMap{}
a.gaugesM.Unlock()
for _, g := range gauges {
metrics = append(metrics, g.flushUnsafe())
}
a.countsM.Lock()
counts := a.counts
a.counts = countsMap{}
a.countsM.Unlock()
for _, c := range counts {
metrics = append(metrics, c.flushUnsafe())
}
a.histogramsM.Lock()
histograms := a.histograms
a.histograms = histogramMap{}
a.histogramsM.Unlock()
for _, h := range histograms {
metrics = append(metrics, h.flushUnsafe())
}
a.distributionM.Lock()
distributions := a.distributions
a.distributions = distributionMap{}
a.distributionM.Unlock()
for _, d := range distributions {
metrics = append(metrics, d.flushUnsafe())
}
atomic.AddInt32(&a.nbContextCount, int32(len(counts)))
atomic.AddInt32(&a.nbContextGauge, int32(len(gauges)))
atomic.AddInt32(&a.nbContextSet, int32(len(sets)))
atomic.AddInt32(&a.nbContextHistogram, int32(len(histograms)))
atomic.AddInt32(&a.nbContextDistribution, int32(len(distributions)))
return metrics
}
func getContext(name string, tags []string) string {
return name + ":" + strings.Join(tags, tagSeparatorSymbol)
}
func getContextAndTags(name string, tags []string) (string, string) {
stringTags := strings.Join(tags, tagSeparatorSymbol)
return name + ":" + stringTags, stringTags
}
func (a *aggregator) count(name string, value int64, tags []string) error {
context := getContext(name, tags)
a.countsM.RLock()
if count, found := a.counts[context]; found {
count.sample(value)
a.countsM.RUnlock()
return nil
}
a.countsM.RUnlock()
a.countsM.Lock()
a.counts[context] = newCountMetric(name, value, tags)
a.countsM.Unlock()
return nil
}
func (a *aggregator) gauge(name string, value float64, tags []string) error {
context := getContext(name, tags)
a.gaugesM.RLock()
if gauge, found := a.gauges[context]; found {
gauge.sample(value)
a.gaugesM.RUnlock()
return nil
}
a.gaugesM.RUnlock()
gauge := newGaugeMetric(name, value, tags)
a.gaugesM.Lock()
a.gauges[context] = gauge
a.gaugesM.Unlock()
return nil
}
func (a *aggregator) set(name string, value string, tags []string) error {
context := getContext(name, tags)
a.setsM.RLock()
if set, found := a.sets[context]; found {
set.sample(value)
a.setsM.RUnlock()
return nil
}
a.setsM.RUnlock()
a.setsM.Lock()
a.sets[context] = newSetMetric(name, value, tags)
a.setsM.Unlock()
return nil
}
func (a *aggregator) histogram(name string, value float64, tags []string) error {
context, stringTags := getContextAndTags(name, tags)
a.histogramsM.RLock()
if histogram, found := a.histograms[context]; found {
histogram.sample(value)
a.histogramsM.RUnlock()
return nil
}
a.histogramsM.RUnlock()
a.histogramsM.Lock()
a.histograms[context] = newHistogramMetric(name, value, stringTags)
a.histogramsM.Unlock()
return nil
}
func (a *aggregator) distribution(name string, value float64, tags []string) error {
context, stringTags := getContextAndTags(name, tags)
a.distributionM.RLock()
if distribution, found := a.distributions[context]; found {
distribution.sample(value)
a.distributionM.RUnlock()
return nil
}
a.distributionM.RUnlock()
a.distributionM.Lock()
a.distributions[context] = newDistributionMetric(name, value, stringTags)
a.distributionM.Unlock()
return nil
}