-
Notifications
You must be signed in to change notification settings - Fork 133
/
aggregator.go
298 lines (252 loc) · 7.47 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package statsd
import (
"strings"
"sync"
"sync/atomic"
"time"
)
type (
countsMap map[string]*countMetric
gaugesMap map[string]*gaugeMetric
setsMap map[string]*setMetric
bufferedMetricMap map[string]*bufferedMetric
)
type aggregator struct {
nbContextGauge uint64
nbContextCount uint64
nbContextSet uint64
countsM sync.RWMutex
gaugesM sync.RWMutex
setsM sync.RWMutex
gauges gaugesMap
counts countsMap
sets setsMap
histograms bufferedMetricContexts
distributions bufferedMetricContexts
timings bufferedMetricContexts
closed chan struct{}
client *Client
// aggregator implements channelMode mechanism to receive histograms,
// distributions and timings. Since they need sampling they need to
// lock for random. When using both channelMode and ExtendedAggregation
// we don't want goroutine to fight over the lock.
inputMetrics chan metric
stopChannelMode chan struct{}
wg sync.WaitGroup
}
func newAggregator(c *Client, maxSamplesPerContext int64) *aggregator {
return &aggregator{
client: c,
counts: countsMap{},
gauges: gaugesMap{},
sets: setsMap{},
histograms: newBufferedContexts(newHistogramMetric, maxSamplesPerContext),
distributions: newBufferedContexts(newDistributionMetric, maxSamplesPerContext),
timings: newBufferedContexts(newTimingMetric, maxSamplesPerContext),
closed: make(chan struct{}),
stopChannelMode: make(chan struct{}),
}
}
func (a *aggregator) start(flushInterval time.Duration) {
ticker := time.NewTicker(flushInterval)
go func() {
for {
select {
case <-ticker.C:
a.flush()
case <-a.closed:
ticker.Stop()
return
}
}
}()
}
func (a *aggregator) startReceivingMetric(bufferSize int, nbWorkers int) {
a.inputMetrics = make(chan metric, bufferSize)
for i := 0; i < nbWorkers; i++ {
a.wg.Add(1)
go a.pullMetric()
}
}
func (a *aggregator) stopReceivingMetric() {
close(a.stopChannelMode)
a.wg.Wait()
}
func (a *aggregator) stop() {
a.closed <- struct{}{}
}
func (a *aggregator) pullMetric() {
for {
select {
case m := <-a.inputMetrics:
switch m.metricType {
case histogram:
a.histogram(m.name, m.fvalue, m.tags, m.rate)
case distribution:
a.distribution(m.name, m.fvalue, m.tags, m.rate)
case timing:
a.timing(m.name, m.fvalue, m.tags, m.rate)
}
case <-a.stopChannelMode:
a.wg.Done()
return
}
}
}
func (a *aggregator) flush() {
for _, m := range a.flushMetrics() {
a.client.sendBlocking(m)
}
}
func (a *aggregator) flushTelemetryMetrics(t *Telemetry) {
if a == nil {
// aggregation is disabled
return
}
t.AggregationNbContextGauge = atomic.LoadUint64(&a.nbContextGauge)
t.AggregationNbContextCount = atomic.LoadUint64(&a.nbContextCount)
t.AggregationNbContextSet = atomic.LoadUint64(&a.nbContextSet)
t.AggregationNbContextHistogram = a.histograms.getNbContext()
t.AggregationNbContextDistribution = a.distributions.getNbContext()
t.AggregationNbContextTiming = a.timings.getNbContext()
}
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())
}
metrics = a.histograms.flush(metrics)
metrics = a.distributions.flush(metrics)
metrics = a.timings.flush(metrics)
atomic.AddUint64(&a.nbContextCount, uint64(len(counts)))
atomic.AddUint64(&a.nbContextGauge, uint64(len(gauges)))
atomic.AddUint64(&a.nbContextSet, uint64(len(sets)))
return metrics
}
// getContext returns the context for a metric name and tags.
//
// The context is the metric name and tags separated by a separator symbol.
// It is not intended to be used as a metric name but as a unique key to aggregate
func getContext(name string, tags []string) string {
c, _ := getContextAndTags(name, tags)
return c
}
// getContextAndTags returns the context and tags for a metric name and tags.
//
// See getContext for usage for context
// The tags are the tags separated by a separator symbol and can be re-used to pass down to the writer
func getContextAndTags(name string, tags []string) (string, string) {
if len(tags) == 0 {
return name, ""
}
n := len(name) + len(nameSeparatorSymbol) + len(tagSeparatorSymbol)*(len(tags)-1)
for _, s := range tags {
n += len(s)
}
var sb strings.Builder
sb.Grow(n)
sb.WriteString(name)
sb.WriteString(nameSeparatorSymbol)
sb.WriteString(tags[0])
for _, s := range tags[1:] {
sb.WriteString(tagSeparatorSymbol)
sb.WriteString(s)
}
s := sb.String()
return s, s[len(name)+len(nameSeparatorSymbol):]
}
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()
// Check if another goroutines hasn't created the value betwen the RUnlock and 'Lock'
if count, found := a.counts[context]; found {
count.sample(value)
a.countsM.Unlock()
return nil
}
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()
// Check if another goroutines hasn't created the value betwen the 'RUnlock' and 'Lock'
if gauge, found := a.gauges[context]; found {
gauge.sample(value)
a.gaugesM.Unlock()
return nil
}
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()
// Check if another goroutines hasn't created the value betwen the 'RUnlock' and 'Lock'
if set, found := a.sets[context]; found {
set.sample(value)
a.setsM.Unlock()
return nil
}
a.sets[context] = newSetMetric(name, value, tags)
a.setsM.Unlock()
return nil
}
// Only histograms, distributions and timings are sampled with a rate since we
// only pack them in on message instead of aggregating them. Discarding the
// sample rate will have impacts on the CPU and memory usage of the Agent.
// type alias for Client.sendToAggregator
type bufferedMetricSampleFunc func(name string, value float64, tags []string, rate float64) error
func (a *aggregator) histogram(name string, value float64, tags []string, rate float64) error {
return a.histograms.sample(name, value, tags, rate)
}
func (a *aggregator) distribution(name string, value float64, tags []string, rate float64) error {
return a.distributions.sample(name, value, tags, rate)
}
func (a *aggregator) timing(name string, value float64, tags []string, rate float64) error {
return a.timings.sample(name, value, tags, rate)
}