-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
server_worker.go
70 lines (61 loc) · 2.18 KB
/
server_worker.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
// 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 server
import (
"github.com/DataDog/datadog-agent/comp/core/workloadmeta"
"github.com/DataDog/datadog-agent/comp/dogstatsd/packets"
"github.com/DataDog/datadog-agent/pkg/aggregator"
"github.com/DataDog/datadog-agent/pkg/metrics"
"github.com/DataDog/datadog-agent/pkg/util/optional"
)
var (
// defaultSampleSize is the default allocation size used to store
// samples when a message contains multiple values. This will
// automatically be extended if needed when we append to it.
defaultSampleSize = 1024
)
type worker struct {
server *server
// the batcher will be responsible of batching a few samples / events / service
// checks and it will automatically forward them to the aggregator, meaning that
// the flushing logic to the aggregator is actually in the batcher.
batcher *batcher
parser *parser
// we allocate it once per worker instead of once per packet. This will
// be used to store the samples out a of packets. Allocating it every
// time is very costly, especially on the GC.
samples metrics.MetricSampleBatch
}
func newWorker(s *server, workerNum int, wmeta optional.Option[workloadmeta.Component]) *worker {
var batcher *batcher
if s.ServerlessMode {
batcher = newServerlessBatcher(s.demultiplexer)
} else {
batcher = newBatcher(s.demultiplexer.(aggregator.DemultiplexerWithAggregator))
}
return &worker{
server: s,
batcher: batcher,
parser: newParser(s.config, s.sharedFloat64List, workerNum, wmeta),
samples: make(metrics.MetricSampleBatch, 0, defaultSampleSize),
}
}
func (w *worker) run() {
for {
select {
case <-w.server.stopChan:
return
case <-w.server.health.C:
case <-w.server.serverlessFlushChan:
w.batcher.flush()
case ps := <-w.server.packetsIn:
packets.TelemetryUntrackPackets(ps)
w.samples = w.samples[0:0]
// we return the samples in case the slice was extended
// when parsing the packets
w.samples = w.server.parsePackets(w.batcher, w.parser, ps, w.samples)
}
}
}