/
submitter.go
468 lines (391 loc) · 15.5 KB
/
submitter.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// 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 runner
import (
"fmt"
"hash/fnv"
"net/http"
"os"
"strconv"
"sync"
"time"
model "github.com/DataDog/agent-payload/v5/process"
"github.com/DataDog/datadog-agent/comp/core/config"
"github.com/DataDog/datadog-agent/comp/core/log"
"github.com/DataDog/datadog-agent/comp/forwarder/defaultforwarder"
//nolint:revive // TODO(PROC) Fix revive linter
forwarder "github.com/DataDog/datadog-agent/comp/forwarder/defaultforwarder"
"github.com/DataDog/datadog-agent/comp/forwarder/defaultforwarder/transaction"
"github.com/DataDog/datadog-agent/comp/process/forwarders"
"github.com/DataDog/datadog-agent/comp/process/types"
ddconfig "github.com/DataDog/datadog-agent/pkg/config"
"github.com/DataDog/datadog-agent/pkg/process/checks"
"github.com/DataDog/datadog-agent/pkg/process/runner/endpoint"
"github.com/DataDog/datadog-agent/pkg/process/statsd"
"github.com/DataDog/datadog-agent/pkg/process/status"
"github.com/DataDog/datadog-agent/pkg/process/util/api"
apicfg "github.com/DataDog/datadog-agent/pkg/process/util/api/config"
"github.com/DataDog/datadog-agent/pkg/process/util/api/headers"
"github.com/DataDog/datadog-agent/pkg/version"
)
//nolint:revive // TODO(PROC) Fix revive linter
type Submitter interface {
Submit(start time.Time, name string, messages *types.Payload)
}
var _ Submitter = &CheckSubmitter{}
//nolint:revive // TODO(PROC) Fix revive linter
type CheckSubmitter struct {
log log.Component
// Per-check Weighted Queues
processResults *api.WeightedQueue
rtProcessResults *api.WeightedQueue
eventResults *api.WeightedQueue
connectionsResults *api.WeightedQueue
// Forwarders
processForwarder defaultforwarder.Component
rtProcessForwarder defaultforwarder.Component
connectionsForwarder defaultforwarder.Component
eventForwarder defaultforwarder.Component
// Endpoints for logging purposes
processAPIEndpoints []apicfg.Endpoint
processEventsAPIEndpoints []apicfg.Endpoint
hostname string
exit chan struct{}
wg *sync.WaitGroup
// Used to cache the hash result of the host name and the pid of the process agent. Being used as part of
// getRequestID method. Must use pointer, to distinguish between uninitialized value and the theoretical but yet
// possible 0 value for the hash result.
requestIDCachedHash *uint64
dropCheckPayloads []string
forwarderRetryMaxQueueBytes int
// Channel for notifying the submitter to enable/disable realtime mode
rtNotifierChan chan types.RTResponse
agentStartTime int64
}
//nolint:revive // TODO(PROC) Fix revive linter
func NewSubmitter(config config.Component, log log.Component, forwarders forwarders.Component, hostname string) (*CheckSubmitter, error) {
queueBytes := config.GetInt("process_config.process_queue_bytes")
if queueBytes <= 0 {
log.Warnf("Invalid queue bytes size: %d. Using default value: %d", queueBytes, ddconfig.DefaultProcessQueueBytes)
queueBytes = ddconfig.DefaultProcessQueueBytes
}
queueSize := config.GetInt("process_config.queue_size")
if queueSize <= 0 {
log.Warnf("Invalid check queue size: %d. Using default value: %d", queueSize, ddconfig.DefaultProcessQueueSize)
queueSize = ddconfig.DefaultProcessQueueSize
}
processResults := api.NewWeightedQueue(queueSize, int64(queueBytes))
log.Debugf("Creating process check queue with max_size=%d and max_weight=%d", processResults.MaxSize(), processResults.MaxWeight())
rtQueueSize := config.GetInt("process_config.rt_queue_size")
if rtQueueSize <= 0 {
log.Warnf("Invalid rt check queue size: %d. Using default value: %d", rtQueueSize, ddconfig.DefaultProcessRTQueueSize)
rtQueueSize = ddconfig.DefaultProcessRTQueueSize
}
// reuse main queue's ProcessQueueBytes because it's unlikely that it'll reach to that size in bytes, so we don't need a separate config for it
rtProcessResults := api.NewWeightedQueue(rtQueueSize, int64(queueBytes))
log.Debugf("Creating rt process check queue with max_size=%d and max_weight=%d", rtProcessResults.MaxSize(), rtProcessResults.MaxWeight())
connectionsResults := api.NewWeightedQueue(queueSize, int64(queueBytes))
log.Debugf("Creating connections queue with max_size=%d and max_weight=%d", connectionsResults.MaxSize(), connectionsResults.MaxWeight())
eventResults := api.NewWeightedQueue(queueSize, int64(queueBytes))
log.Debugf("Creating event check queue with max_size=%d and max_weight=%d", eventResults.MaxSize(), eventResults.MaxWeight())
dropCheckPayloads := config.GetStringSlice("process_config.drop_check_payloads")
if len(dropCheckPayloads) > 0 {
log.Debugf("Dropping payloads from checks: %v", dropCheckPayloads)
}
status.UpdateDropCheckPayloads(dropCheckPayloads)
// Forwarder initialization
processAPIEndpoints, err := endpoint.GetAPIEndpoints(config)
if err != nil {
return nil, err
}
processEventsAPIEndpoints, err := endpoint.GetEventsAPIEndpoints(config)
if err != nil {
return nil, err
}
return &CheckSubmitter{
log: log,
processResults: processResults,
rtProcessResults: rtProcessResults,
eventResults: eventResults,
connectionsResults: connectionsResults,
processForwarder: forwarders.GetProcessForwarder(),
rtProcessForwarder: forwarders.GetRTProcessForwarder(),
connectionsForwarder: forwarders.GetConnectionsForwarder(),
eventForwarder: forwarders.GetEventForwarder(),
processAPIEndpoints: processAPIEndpoints,
processEventsAPIEndpoints: processEventsAPIEndpoints,
hostname: hostname,
dropCheckPayloads: dropCheckPayloads,
forwarderRetryMaxQueueBytes: queueBytes,
rtNotifierChan: make(chan types.RTResponse, 1), // Buffer the channel so we don't block submissions
wg: &sync.WaitGroup{},
exit: make(chan struct{}),
agentStartTime: time.Now().Unix(),
}, nil
}
func printStartMessage(log log.Component, hostname string, processAPIEndpoints []apicfg.Endpoint, processEventsAPIEndpoints []apicfg.Endpoint) {
eps := make([]string, 0, len(processAPIEndpoints))
for _, e := range processAPIEndpoints {
eps = append(eps, e.Endpoint.String())
}
eventsEps := make([]string, 0, len(processEventsAPIEndpoints))
for _, e := range processEventsAPIEndpoints {
eventsEps = append(eventsEps, e.Endpoint.String())
}
log.Infof("Starting CheckSubmitter for host=%s, endpoints=%s, events endpoints=%s", hostname, eps, eventsEps)
}
//nolint:revive // TODO(PROC) Fix revive linter
func (s *CheckSubmitter) Submit(start time.Time, name string, messages *types.Payload) {
results := s.resultsQueueForCheck(name)
s.messagesToResultsQueue(start, name, messages.Message, results)
}
//nolint:revive // TODO(PROC) Fix revive linter
func (s *CheckSubmitter) Start() error {
printStartMessage(s.log, s.hostname, s.processAPIEndpoints, s.processEventsAPIEndpoints)
s.wg.Add(1)
go func() {
defer s.wg.Done()
s.consumePayloads(s.processResults, s.processForwarder)
}()
s.wg.Add(1)
go func() {
defer s.wg.Done()
s.consumePayloads(s.rtProcessResults, s.rtProcessForwarder)
}()
s.wg.Add(1)
go func() {
defer s.wg.Done()
s.consumePayloads(s.connectionsResults, s.connectionsForwarder)
}()
s.wg.Add(1)
go func() {
defer s.wg.Done()
s.consumePayloads(s.eventResults, s.eventForwarder)
}()
s.wg.Add(1)
go func() {
defer s.wg.Done()
heartbeat := time.NewTicker(15 * time.Second)
defer heartbeat.Stop()
queueSizeTicker := time.NewTicker(10 * time.Second)
defer queueSizeTicker.Stop()
queueLogTicker := time.NewTicker(time.Minute)
defer queueLogTicker.Stop()
agentVersion, _ := version.Agent()
tags := []string{
fmt.Sprintf("version:%s", agentVersion.GetNumberAndPre()),
fmt.Sprintf("revision:%s", agentVersion.Commit),
}
for {
select {
case <-heartbeat.C:
statsd.Client.Gauge("datadog.process.agent", 1, tags, 1) //nolint:errcheck
case <-queueSizeTicker.C:
status.UpdateQueueStats(&status.QueueStats{
ProcessQueueSize: s.processResults.Len(),
RtProcessQueueSize: s.rtProcessResults.Len(),
ConnectionsQueueSize: s.connectionsResults.Len(),
EventQueueSize: s.eventResults.Len(),
ProcessQueueBytes: s.processResults.Weight(),
RtProcessQueueBytes: s.rtProcessResults.Weight(),
ConnectionsQueueBytes: s.connectionsResults.Weight(),
EventQueueBytes: s.eventResults.Weight(),
})
case <-queueLogTicker.C:
s.logQueuesSize()
case <-s.exit:
return
}
}
}()
return nil
}
//nolint:revive // TODO(PROC) Fix revive linter
func (s *CheckSubmitter) Stop() {
close(s.exit)
s.processResults.Stop()
s.rtProcessResults.Stop()
s.connectionsResults.Stop()
s.eventResults.Stop()
s.wg.Wait()
close(s.rtNotifierChan)
}
//nolint:revive // TODO(PROC) Fix revive linter
func (s *CheckSubmitter) GetRTNotifierChan() <-chan types.RTResponse {
return s.rtNotifierChan
}
func (s *CheckSubmitter) consumePayloads(results *api.WeightedQueue, fwd forwarder.Forwarder) {
for {
// results.Poll() will return ok=false when stopped
item, ok := results.Poll()
if !ok {
return
}
result := item.(*checkResult)
for _, payload := range result.payloads {
var (
forwarderPayload = transaction.NewBytesPayloadsWithoutMetaData([]*[]byte{&payload.body})
responses chan forwarder.Response
err error
updateRTStatus bool
)
if s.shouldDropPayload(result.name) {
continue
}
switch result.name {
case checks.ProcessCheckName:
updateRTStatus = true
responses, err = fwd.SubmitProcessChecks(forwarderPayload, payload.headers)
case checks.RTProcessCheckName:
updateRTStatus = true
responses, err = fwd.SubmitRTProcessChecks(forwarderPayload, payload.headers)
case checks.ContainerCheckName:
updateRTStatus = true
responses, err = fwd.SubmitContainerChecks(forwarderPayload, payload.headers)
case checks.RTContainerCheckName:
updateRTStatus = true
responses, err = fwd.SubmitRTContainerChecks(forwarderPayload, payload.headers)
case checks.ConnectionsCheckName:
responses, err = fwd.SubmitConnectionChecks(forwarderPayload, payload.headers)
case checks.DiscoveryCheckName:
// A Process Discovery check does not change the RT mode
responses, err = fwd.SubmitProcessDiscoveryChecks(forwarderPayload, payload.headers)
case checks.ProcessEventsCheckName:
responses, err = fwd.SubmitProcessEventChecks(forwarderPayload, payload.headers)
default:
err = fmt.Errorf("unsupported payload type: %s", result.name)
}
if err != nil {
s.log.Errorf("Unable to submit payload: %s", err)
continue
}
if statuses := readResponseStatuses(result.name, responses); len(statuses) > 0 {
if updateRTStatus {
notifyRTStatusChange(s.rtNotifierChan, statuses)
}
}
}
}
}
func (s *CheckSubmitter) resultsQueueForCheck(name string) *api.WeightedQueue {
switch name {
case checks.RTProcessCheckName, checks.RTContainerCheckName:
return s.rtProcessResults
case checks.ConnectionsCheckName:
return s.connectionsResults
case checks.ProcessEventsCheckName:
return s.eventResults
}
return s.processResults
}
func (s *CheckSubmitter) logQueuesSize() {
var (
processSize = s.processResults.Len()
rtProcessSize = s.rtProcessResults.Len()
connectionsSize = s.connectionsResults.Len()
eventsSize = s.eventResults.Len()
)
if processSize == 0 &&
rtProcessSize == 0 &&
connectionsSize == 0 &&
eventsSize == 0 {
return
}
s.log.Infof(
"Delivery queues: process[size=%d, weight=%d], rtprocess[size=%d, weight=%d], connections[size=%d, weight=%d], event[size=%d, weight=%d]",
processSize, s.processResults.Weight(),
rtProcessSize, s.rtProcessResults.Weight(),
connectionsSize, s.connectionsResults.Weight(),
eventsSize, s.eventResults.Weight(),
)
}
func (s *CheckSubmitter) messagesToResultsQueue(start time.Time, name string, messages []model.MessageBody, queue *api.WeightedQueue) {
result := s.messagesToCheckResult(start, name, messages)
if result == nil {
return
}
queue.Add(result)
// update proc and container count for info
status.UpdateProcContainerCount(messages)
}
func (s *CheckSubmitter) messagesToCheckResult(start time.Time, name string, messages []model.MessageBody) *checkResult {
if len(messages) == 0 {
return nil
}
payloads := make([]checkPayload, 0, len(messages))
sizeInBytes := 0
for messageIndex, m := range messages {
body, err := api.EncodePayload(m)
if err != nil {
s.log.Errorf("Unable to encode message: %s", err)
continue
}
agentVersion, _ := version.Agent()
extraHeaders := make(http.Header)
extraHeaders.Set(headers.TimestampHeader, strconv.Itoa(int(start.Unix())))
extraHeaders.Set(headers.HostHeader, s.hostname)
extraHeaders.Set(headers.ProcessVersionHeader, agentVersion.GetNumber())
extraHeaders.Set(headers.ContainerCountHeader, strconv.Itoa(getContainerCount(m)))
extraHeaders.Set(headers.ContentTypeHeader, headers.ProtobufContentType)
extraHeaders.Set(headers.AgentStartTime, strconv.FormatInt(s.agentStartTime, 10))
switch name {
case checks.ProcessEventsCheckName:
extraHeaders.Set(headers.EVPOriginHeader, "process-agent")
extraHeaders.Set(headers.EVPOriginVersionHeader, version.AgentVersion)
case checks.ConnectionsCheckName, checks.ProcessCheckName:
requestID := s.getRequestID(start, messageIndex)
s.log.Debugf("the request id of the current message: %s", requestID)
extraHeaders.Set(headers.RequestIDHeader, requestID)
}
payloads = append(payloads, checkPayload{
body: body,
headers: extraHeaders,
})
sizeInBytes += len(body)
}
return &checkResult{
name: name,
payloads: payloads,
sizeInBytes: int64(sizeInBytes),
}
}
// getRequestID generates a unique identifier (string representation of 64 bits integer) that is composed as follows:
// 1. 22 bits of the seconds in the current month.
// 2. 28 bits of hash of the hostname and process agent pid.
// 3. 14 bits of the current message in the batch being sent to the server.
func (s *CheckSubmitter) getRequestID(start time.Time, chunkIndex int) string {
// The epoch is the beginning of the month of the `start` variable.
epoch := time.Date(start.Year(), start.Month(), 1, 0, 0, 0, 0, start.Location())
// We are taking the seconds in the current month, and representing them under 22 bits.
// In a month we have 60 seconds per minute * 60 minutes per hour * 24 hours per day * maximum 31 days a month
// which is 2678400, and it can be represented with log2(2678400) = 21.35 bits.
seconds := (uint64(start.Sub(epoch).Seconds()) & secondsMask) << (hashNumberOfBits + chunkNumberOfBits)
//// Next, we want 28 bits of hashed hostname & process agent pid.
if s.requestIDCachedHash == nil {
hash := fnv.New32()
hash.Write([]byte(s.hostname))
hash.Write([]byte(strconv.Itoa(os.Getpid())))
hostNamePIDHash := (uint64(hash.Sum32()) & hashMask) << chunkNumberOfBits
s.requestIDCachedHash = &hostNamePIDHash
}
// Next, we take up to 14 bits to represent the message index in the batch.
// It means that we support up to 16384 (2 ^ 14) different messages being sent on the same batch.
chunk := uint64(chunkIndex & chunkMask)
return fmt.Sprintf("%d", seconds+*s.requestIDCachedHash+chunk)
}
func (s *CheckSubmitter) shouldDropPayload(check string) bool {
for _, d := range s.dropCheckPayloads {
if d == check {
return true
}
}
return false
}
func notifyRTStatusChange(rtNotifierChan chan<- types.RTResponse, statuses types.RTResponse) {
select {
case rtNotifierChan <- statuses:
default: // Never block on the rtNotifierChan in case the runner has somehow stopped
}
}