-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathbatch.go
More file actions
287 lines (237 loc) · 8.99 KB
/
Copy pathbatch.go
File metadata and controls
287 lines (237 loc) · 8.99 KB
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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
// Package dogstatsd implements DogStatsD.
//
//nolint:revive // TODO(AML) Fix revive linter
package serverimpl
import (
"strconv"
"time"
"github.com/DataDog/datadog-agent/comp/core/telemetry/def"
"github.com/DataDog/datadog-agent/pkg/aggregator"
"github.com/DataDog/datadog-agent/pkg/aggregator/ckey"
"github.com/DataDog/datadog-agent/pkg/metrics"
"github.com/DataDog/datadog-agent/pkg/metrics/event"
"github.com/DataDog/datadog-agent/pkg/metrics/servicecheck"
"github.com/DataDog/datadog-agent/pkg/tagset"
"github.com/DataDog/datadog-agent/pkg/util/log"
)
// interface requiring all functions expected by the dogstatsd server
type dogstatsdBatcher interface {
appendSample(sample metrics.MetricSample)
appendEvent(event *event.Event)
appendServiceCheck(serviceCheck *servicecheck.ServiceCheck)
appendLateSample(sample metrics.MetricSample)
flush()
}
// batcher batches multiple metrics before submission
// this struct is not safe for concurrent use
type batcher struct {
// slice of MetricSampleBatch (one entry per running sampling pipeline)
samples []metrics.MetricSampleBatch
// offset while writing into samples entries (i.e. samples currently stored per pipeline)
samplesCount []int
// MetricSampleBatch used for metrics with timestamp
// metrics with timestamp are not context-sharded since we don't aggregate them.
// When the no-aggregation pipeline is enabled, these batches are sent to a shared
// queue consumed by the no-aggregation workers.
samplesWithTs metrics.MetricSampleBatch
// offset while writing into the sample with timestampe slice (i.e. count of samples
// with timestamp currently stored)
samplesWithTsCount int
events []*event.Event
serviceChecks []*servicecheck.ServiceCheck
// output channels
choutEvents chan<- []*event.Event
choutServiceChecks chan<- []*servicecheck.ServiceCheck
metricSamplePool *metrics.MetricSamplePool
demux aggregator.Demultiplexer
// buffer slice allocated once per contextResolver to combine and sort
// tags, origin detection tags and k8s tags.
shardGenerator shardKeyGenerator
pipelineCount int
// the batcher has to know if the no-aggregation pipeline is enabled or not:
// in the case of the no agg pipeline disabled, it would send them as usual to
// the demux which only choice would be to send them on an arbitrary sampler
// (i.e. the first one). Being aware that the no agg pipeline is disabled,
// the batcher can decide to properly distribute these samples on the available
// pipelines.
noAggPipelineEnabled bool
// telemetry
tlmChannel telemetry.Histogram
}
type shardKeyGenerator struct {
keyGenerator *ckey.KeyGenerator
tagsBuffer *tagset.HashingTagsAccumulator
}
func (s *shardKeyGenerator) Generate(sample metrics.MetricSample, shards int) uint32 {
// TODO(remy): re-using this tagsBuffer later in the pipeline (by sharing
// it in the sample?) would reduce CPU usage, avoiding to recompute
// the tags hashes while generating the context key.
s.tagsBuffer.AppendInterned(sample.ITags...)
s.tagsBuffer.Append(sample.Tags...)
h := s.keyGenerator.Generate(sample.Name, sample.Host, s.tagsBuffer)
s.tagsBuffer.Reset()
return fastrange(h, shards)
}
// Use fastrange instead of a modulo for better performance.
// See http://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/.
//
// Note that we shift the context key because it is an actual 64 bits, and
// the fast range has to operate on 32 bits values, so, we shift it in order
// to "reduce" its size to 32 bits (i.e. the `key>>32`), we don't mind using
// only half of the context key for the shard key, it will be unique enough
// for such purpose.
func fastrange(key ckey.ContextKey, pipelineCount int) uint32 {
// return uint32(uint64(key) % uint64(pipelineCount))
return uint32((uint64(key>>32) * uint64(pipelineCount)) >> 32)
}
func newBatcher(demux aggregator.DemultiplexerWithAggregator, tlmChannel telemetry.Histogram) *batcher {
_, pipelineCount := aggregator.GetDogStatsDWorkerAndPipelineCount()
var e chan []*event.Event
var sc chan []*servicecheck.ServiceCheck
// the Serverless Agent doesn't have to support service checks nor events so
// it doesn't run an Aggregator.
e, sc = demux.GetEventsAndServiceChecksChannels()
// prepare on-time samples buffers
samples := make([]metrics.MetricSampleBatch, pipelineCount)
samplesCount := make([]int, pipelineCount)
for i := range samples {
samples[i] = demux.GetMetricSamplePool().GetBatch()
samplesCount[i] = 0
}
// prepare the late samples buffer
samplesWithTs := demux.GetMetricSamplePool().GetBatch()
samplesWithTsCount := 0
return &batcher{
samples: samples,
samplesCount: samplesCount,
samplesWithTs: samplesWithTs,
samplesWithTsCount: samplesWithTsCount,
metricSamplePool: demux.GetMetricSamplePool(),
choutEvents: e,
choutServiceChecks: sc,
demux: demux,
pipelineCount: pipelineCount,
shardGenerator: newShardGenerator(),
noAggPipelineEnabled: demux.Options().NoAggregationPipelineWorkersCount > 0,
tlmChannel: tlmChannel,
}
}
func newShardGenerator() shardKeyGenerator {
return shardKeyGenerator{
keyGenerator: ckey.NewKeyGenerator(),
tagsBuffer: tagset.NewHashingTagsAccumulator(),
}
}
func newServerlessBatcher(demux aggregator.Demultiplexer, tlmChannel telemetry.Histogram) *batcher {
_, pipelineCount := aggregator.GetDogStatsDWorkerAndPipelineCount()
samples := make([]metrics.MetricSampleBatch, pipelineCount)
samplesCount := make([]int, pipelineCount)
samplesWithTs := demux.GetMetricSamplePool().GetBatch()
samplesWithTsCount := 0
for i := range samples {
samples[i] = demux.GetMetricSamplePool().GetBatch()
samplesCount[i] = 0
}
return &batcher{
samples: samples,
samplesCount: samplesCount,
samplesWithTs: samplesWithTs,
samplesWithTsCount: samplesWithTsCount,
metricSamplePool: demux.GetMetricSamplePool(),
demux: demux,
pipelineCount: pipelineCount,
shardGenerator: newShardGenerator(),
tlmChannel: tlmChannel,
}
}
// Batching data
// -------------
func (b *batcher) appendSample(sample metrics.MetricSample) {
var shardKey uint32
if b.pipelineCount > 1 {
shardKey = b.shardGenerator.Generate(sample, b.pipelineCount)
}
if b.samplesCount[shardKey] >= len(b.samples[shardKey]) {
b.flushSamples(shardKey)
}
b.samples[shardKey][b.samplesCount[shardKey]] = sample
b.samplesCount[shardKey]++
}
func (b *batcher) appendEvent(event *event.Event) {
b.events = append(b.events, event)
}
func (b *batcher) appendServiceCheck(serviceCheck *servicecheck.ServiceCheck) {
b.serviceChecks = append(b.serviceChecks, serviceCheck)
}
func (b *batcher) appendLateSample(sample metrics.MetricSample) {
// if the no aggregation pipeline is not enabled, we fallback on the
// main pipeline eventually distributing the samples on multiple samplers.
if !b.noAggPipelineEnabled {
b.appendSample(sample)
return
}
if b.samplesWithTsCount == len(b.samplesWithTs) {
b.flushSamplesWithTs()
}
b.samplesWithTs[b.samplesWithTsCount] = sample
b.samplesWithTsCount++
}
// Flushing
// --------
func (b *batcher) flushSamples(shard uint32) {
if b.samplesCount[shard] > 0 {
t1 := time.Now()
b.demux.AggregateSamples(aggregator.TimeSamplerID(shard), b.samples[shard][:b.samplesCount[shard]])
t2 := time.Now()
b.tlmChannel.Observe(float64(t2.Sub(t1).Nanoseconds()), strconv.Itoa(int(shard)), "metrics")
b.samplesCount[shard] = 0
b.samples[shard] = b.metricSamplePool.GetBatch()
}
}
func (b *batcher) flushSamplesWithTs() {
if b.samplesWithTsCount > 0 {
t1 := time.Now()
b.demux.SendSamplesWithoutAggregation(b.samplesWithTs[:b.samplesWithTsCount])
t2 := time.Now()
b.tlmChannel.Observe(float64(t2.Sub(t1).Nanoseconds()), "", "late_metrics")
b.samplesWithTsCount = 0
b.samplesWithTs = b.metricSamplePool.GetBatch()
}
}
// flush pushes all batched metrics to the aggregator.
func (b *batcher) flush() {
// flush all on-time samples on their respective time sampler
for i := 0; i < b.pipelineCount; i++ {
b.flushSamples(uint32(i))
}
// flush all samples with timestamp to the serializer
b.flushSamplesWithTs()
// flush events
if len(b.events) > 0 {
if b.choutEvents != nil {
t1 := time.Now()
b.choutEvents <- b.events
t2 := time.Now()
b.tlmChannel.Observe(float64(t2.Sub(t1).Nanoseconds()), "", "events")
} else {
log.Debugf("Skipping event flush due to nil channel")
}
b.events = []*event.Event{}
}
// flush service checks
if len(b.serviceChecks) > 0 {
if b.choutServiceChecks != nil {
t1 := time.Now()
b.choutServiceChecks <- b.serviceChecks
t2 := time.Now()
b.tlmChannel.Observe(float64(t2.Sub(t1).Nanoseconds()), "", "service_checks")
} else {
log.Debugf("Skipping service check flush due to nil channel")
}
b.serviceChecks = []*servicecheck.ServiceCheck{}
}
}