-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
runner.go
538 lines (454 loc) · 13.9 KB
/
runner.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
// 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"
"math/rand"
"net/http"
"sync"
"time"
"go.uber.org/atomic"
model "github.com/DataDog/agent-payload/v5/process"
sysconfig "github.com/DataDog/datadog-agent/cmd/system-probe/config"
sysconfigtypes "github.com/DataDog/datadog-agent/cmd/system-probe/config/types"
"github.com/DataDog/datadog-agent/comp/forwarder/defaultforwarder"
"github.com/DataDog/datadog-agent/comp/process/types"
ddconfig "github.com/DataDog/datadog-agent/pkg/config"
oconfig "github.com/DataDog/datadog-agent/pkg/orchestrator/config"
"github.com/DataDog/datadog-agent/pkg/process/checks"
"github.com/DataDog/datadog-agent/pkg/process/status"
"github.com/DataDog/datadog-agent/pkg/process/util/api"
"github.com/DataDog/datadog-agent/pkg/util/log"
)
type checkResult struct {
name string
payloads []checkPayload
sizeInBytes int64
}
func (cr *checkResult) Weight() int64 {
return cr.sizeInBytes
}
func (cr *checkResult) Type() string {
return cr.name
}
var _ api.WeightedItem = &checkResult{}
type checkPayload struct {
body []byte
headers http.Header
}
//nolint:revive // TODO(PROC) Fix revive linter
type Runner interface{}
// CheckRunner will collect metrics from the local system and ship to the backend.
type CheckRunner struct {
config ddconfig.Reader
sysProbeCfg *checks.SysProbeConfig
hostInfo *checks.HostInfo
// required for being able to start and stop the collector
wg *sync.WaitGroup
stop chan struct{}
// true if real-time is enabled
realTimeEnabled *atomic.Bool
// the next groupID to be issued
groupID *atomic.Int32
rtIntervalCh chan time.Duration
orchestrator *oconfig.OrchestratorConfig
// counters for each type of check
runCounters sync.Map
enabledChecks []checks.Check
// Controls the real-time interval, can change live.
realTimeInterval time.Duration
// Enables running realtime checks
runRealTime bool
// Submits check payloads to datadog
Submitter Submitter
// listens for when to enable and disable realtime mode
rtNotifierChan <-chan types.RTResponse
}
//nolint:revive // TODO(PROC) Fix revive linter
func (l *CheckRunner) RunRealTime() bool {
return l.runRealTime
}
// NewRunner creates a new CheckRunner
func NewRunner(
config ddconfig.Reader,
sysCfg *sysconfigtypes.Config,
hostInfo *checks.HostInfo,
enabledChecks []checks.Check,
rtNotifierChan <-chan types.RTResponse,
) (*CheckRunner, error) {
runRealTime := !config.GetBool("process_config.disable_realtime_checks")
sysProbeCfg := &checks.SysProbeConfig{}
if sysCfg != nil && sysCfg.Enabled {
// If the sysprobe module is enabled, the process check can call out to the sysprobe for privileged stats
_, processModuleEnabled := sysCfg.EnabledModules[sysconfig.ProcessModule]
sysProbeCfg.ProcessModuleEnabled = processModuleEnabled
sysProbeCfg.MaxConnsPerMessage = sysCfg.MaxConnsPerMessage
sysProbeCfg.SystemProbeAddress = sysCfg.SocketAddress
}
return NewRunnerWithChecks(config, sysProbeCfg, hostInfo, enabledChecks, runRealTime, rtNotifierChan)
}
// NewRunnerWithChecks creates a new CheckRunner
func NewRunnerWithChecks(
config ddconfig.Reader,
sysProbeCfg *checks.SysProbeConfig,
hostInfo *checks.HostInfo,
checks []checks.Check,
runRealTime bool,
rtNotifierChan <-chan types.RTResponse,
) (*CheckRunner, error) {
orchestrator := oconfig.NewDefaultOrchestratorConfig()
if err := orchestrator.Load(); err != nil {
return nil, err
}
return &CheckRunner{
hostInfo: hostInfo,
config: config,
sysProbeCfg: sysProbeCfg,
wg: &sync.WaitGroup{},
stop: make(chan struct{}),
rtIntervalCh: make(chan time.Duration),
orchestrator: orchestrator,
groupID: atomic.NewInt32(rand.Int31()),
enabledChecks: checks,
// Defaults for real-time on start
realTimeInterval: 2 * time.Second,
realTimeEnabled: atomic.NewBool(false),
runRealTime: runRealTime,
rtNotifierChan: rtNotifierChan,
}, nil
}
func (l *CheckRunner) runCheck(c checks.Check) {
runCounter := l.nextRunCounter(c.Name())
start := time.Now()
// update the last collected timestamp for info
status.UpdateLastCollectTime(start)
result, err := c.Run(l.nextGroupID, nil)
if err != nil {
log.Errorf("Unable to run check '%s': %s", c.Name(), err)
return
}
if result == nil {
// Check returned nothing
return
}
if c.ShouldSaveLastRun() {
checks.StoreCheckOutput(c.Name(), result.Payloads())
} else {
checks.StoreCheckOutput(c.Name(), nil)
}
msg := &types.Payload{
CheckName: c.Name(),
Message: result.Payloads(),
}
l.Submitter.Submit(start, c.Name(), msg)
if !c.Realtime() {
logCheckDuration(c.Name(), start, runCounter)
}
}
func (l *CheckRunner) runCheckWithRealTime(c checks.Check, options *checks.RunOptions) {
start := time.Now()
// update the last collected timestamp for info
status.UpdateLastCollectTime(start)
result, err := c.Run(l.nextGroupID, options)
if err != nil {
log.Errorf("Unable to run check '%s': %s", c.Name(), err)
return
}
if result == nil {
// Check returned nothing
return
}
msg := &types.Payload{
CheckName: c.Name(),
Message: result.Payloads(),
}
l.Submitter.Submit(start, c.Name(), msg)
if options.RunStandard {
// We are only updating the run counter for the standard check
// since RT checks are too frequent and we only log standard check
// durations
runCounter := l.nextRunCounter(c.Name())
checks.StoreCheckOutput(c.Name(), result.Payloads())
logCheckDuration(c.Name(), start, runCounter)
}
rtName := checks.RTName(c.Name())
msg = &types.Payload{
Message: result.RealtimePayloads(),
}
l.Submitter.Submit(start, rtName, msg)
if options.RunRealtime {
checks.StoreCheckOutput(rtName, msg.Message)
}
}
func (l *CheckRunner) nextRunCounter(name string) int32 {
runCounter := int32(1)
if rc, ok := l.runCounters.Load(name); ok {
runCounter = rc.(int32) + 1
}
l.runCounters.Store(name, runCounter)
return runCounter
}
func logCheckDuration(name string, start time.Time, runCounter int32) {
d := time.Since(start)
switch {
case runCounter < 5:
log.Infof("Finished %s check #%d in %s", name, runCounter, d)
case runCounter == 5:
log.Infof("Finished %s check #%d in %s. First 5 check runs finished, next runs will be logged every 20 runs.", name, runCounter, d)
case runCounter%20 == 0:
log.Infof("Finish %s check #%d in %s", name, runCounter, d)
}
}
func (l *CheckRunner) nextGroupID() int32 {
return l.groupID.Inc()
}
const (
secondsNumberOfBits = 22
hashNumberOfBits = 28
chunkNumberOfBits = 14
secondsMask = 1<<secondsNumberOfBits - 1
hashMask = 1<<hashNumberOfBits - 1
chunkMask = 1<<chunkNumberOfBits - 1
)
//nolint:revive // TODO(PROC) Fix revive linter
func (l *CheckRunner) Run() error {
realTimeAllowed := !l.config.GetBool("process_config.disable_realtime_checks")
checkNamesLength := len(l.enabledChecks)
if realTimeAllowed {
// checkNamesLength is double when realtime checks is enabled as we append the Process real time name
// as well as the original check name
checkNamesLength = checkNamesLength * 2
}
checkNames := make([]string, 0, checkNamesLength)
for _, check := range l.enabledChecks {
checkNames = append(checkNames, check.Name())
// Append `process_rt` if process check is enabled, and rt is enabled, so the customer doesn't get confused if
// process_rt doesn't show up in the enabled checks
if check.Name() == checks.ProcessCheckName && realTimeAllowed {
checkNames = append(checkNames, checks.RTProcessCheckName)
}
}
status.UpdateEnabledChecks(checkNames)
log.Infof("Starting process-agent with enabled checks=%v", checkNames)
if realTimeAllowed && l.rtNotifierChan != nil {
l.listenForRTUpdates()
}
for _, c := range l.enabledChecks {
if err := c.Init(l.sysProbeCfg, l.hostInfo, false); err != nil {
return err
}
runner, err := l.runnerForCheck(c)
if err != nil {
return fmt.Errorf("error starting check %s: %s", c.Name(), err)
}
l.wg.Add(1)
go func() {
defer l.wg.Done()
runner()
}()
}
return nil
}
func (l *CheckRunner) listenForRTUpdates() {
l.wg.Add(1)
go func() {
defer l.wg.Done()
for {
select {
case response, ok := <-l.rtNotifierChan:
if !ok {
return
}
l.UpdateRTStatus(response)
case <-l.stop:
return
}
}
}()
}
func (l *CheckRunner) runnerForCheck(c checks.Check) (func(), error) {
if !l.runRealTime || !c.SupportsRunOptions() {
return l.basicRunner(c), nil
}
rtName := checks.RTName(c.Name())
interval := checks.GetInterval(l.config, c.Name())
rtInterval := checks.GetInterval(l.config, rtName)
if interval < rtInterval || interval%rtInterval != 0 {
// Check interval must be greater or equal to realtime check interval and the intervals must be divisible
// in order to be run on the same goroutine
defaultInterval := checks.GetDefaultInterval(c.Name())
defaultRTInterval := checks.GetDefaultInterval(rtName)
log.Warnf(
"Invalid %s check interval overrides [%s,%s], resetting to defaults [%s,%s]",
c.Name(),
interval,
rtInterval,
defaultInterval,
defaultRTInterval,
)
interval = defaultInterval
rtInterval = defaultRTInterval
}
return checks.NewRunnerWithRealTime(
checks.RunnerConfig{
CheckInterval: interval,
RtInterval: rtInterval,
ExitChan: l.stop,
RtIntervalChan: l.rtIntervalCh,
RtEnabled: func() bool {
return l.realTimeEnabled.Load()
},
RunCheck: func(options checks.RunOptions) {
l.runCheckWithRealTime(c, &options)
},
},
)
}
func (l *CheckRunner) basicRunner(c checks.Check) func() {
return func() {
// Run the check the first time to prime the caches.
if !c.Realtime() {
l.runCheck(c)
}
ticker := time.NewTicker(checks.GetInterval(l.config, c.Name()))
for {
select {
case <-ticker.C:
realTimeEnabled := l.runRealTime && l.realTimeEnabled.Load()
if !c.Realtime() || realTimeEnabled {
l.runCheck(c)
}
case d := <-l.rtIntervalCh:
// Live-update the ticker.
if c.Realtime() {
ticker.Stop()
ticker = time.NewTicker(d)
}
case _, ok := <-l.stop:
if !ok {
return
}
}
}
}
}
//nolint:revive // TODO(PROC) Fix revive linter
func (l *CheckRunner) UpdateRTStatus(statuses []*model.CollectorStatus) {
// If realtime mode is disabled in the config, do not change the real time status.
if !l.runRealTime {
return
}
curEnabled := l.realTimeEnabled.Load()
// If any of the endpoints wants real-time we'll do that.
// We will pick the maximum interval given since generally this is
// only set if we're trying to limit load on the backend.
shouldEnableRT := false
maxInterval := 0 * time.Second
activeClients := int32(0)
for _, s := range statuses {
shouldEnableRT = shouldEnableRT || s.ActiveClients > 0
if s.ActiveClients > 0 {
activeClients += s.ActiveClients
}
interval := time.Duration(s.Interval) * time.Second
if interval > maxInterval {
maxInterval = interval
}
}
if curEnabled && !shouldEnableRT {
log.Info("Detected 0 clients, disabling real-time mode")
l.realTimeEnabled.Store(false)
} else if !curEnabled && shouldEnableRT {
log.Infof("Detected %d active clients, enabling real-time mode", activeClients)
l.realTimeEnabled.Store(true)
}
if maxInterval != l.realTimeInterval {
l.realTimeInterval = maxInterval
if l.realTimeInterval <= 0 {
l.realTimeInterval = 2 * time.Second
}
// Pass along the real-time interval, one per check, so that every
// check routine will see the new interval.
for range l.enabledChecks {
l.rtIntervalCh <- l.realTimeInterval
}
log.Infof("real time interval updated to %s", l.realTimeInterval)
}
}
//nolint:revive // TODO(PROC) Fix revive linter
func (l *CheckRunner) Stop() {
close(l.stop)
l.wg.Wait()
for _, check := range l.enabledChecks {
log.Debugf("Cleaning up %s check", check.Name())
check.Cleanup()
}
}
//nolint:revive // TODO(PROC) Fix revive linter
func (l *CheckRunner) GetChecks() []checks.Check {
return l.enabledChecks
}
//nolint:revive // TODO(PROC) Fix revive linter
func (l *CheckRunner) IsRealTimeEnabled() bool {
return l.realTimeEnabled.Load()
}
// getContainerCount returns the number of containers in the message body
func getContainerCount(mb model.MessageBody) int {
switch v := mb.(type) {
case *model.CollectorProc:
return len(v.GetContainers())
case *model.CollectorRealTime:
return len(v.GetContainerStats())
case *model.CollectorContainer:
return len(v.GetContainers())
case *model.CollectorContainerRealTime:
return len(v.GetStats())
case *model.CollectorConnections:
return 0
}
return 0
}
func readResponseStatuses(checkName string, responses <-chan defaultforwarder.Response) []*model.CollectorStatus {
var statuses []*model.CollectorStatus
for response := range responses {
if response.Err != nil {
log.Errorf("[%s] Error from %s: %s", checkName, response.Domain, response.Err)
continue
}
if response.StatusCode >= 300 {
log.Errorf("[%s] Invalid response from %s: %d -> %s", checkName, response.Domain, response.StatusCode, response.Err)
continue
}
// some checks don't receive a response with a status used to enable RT mode
if ignoreResponseBody(checkName) {
continue
}
r, err := model.DecodeMessage(response.Body)
if err != nil {
log.Errorf("[%s] Could not decode response body: %s", checkName, err)
continue
}
switch r.Header.Type {
case model.TypeResCollector:
rm := r.Body.(*model.ResCollector)
if len(rm.Message) > 0 {
log.Errorf("[%s] Error in response from %s: %s", checkName, response.Domain, rm.Message)
} else {
statuses = append(statuses, rm.Status)
}
default:
log.Errorf("[%s] Unexpected response type from %s: %d", checkName, response.Domain, r.Header.Type)
}
}
return statuses
}
func ignoreResponseBody(checkName string) bool {
switch checkName {
case checks.ProcessEventsCheckName:
return true
default:
return false
}
}