-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
container.go
162 lines (134 loc) · 5.12 KB
/
container.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
// 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 checks
import (
"context"
"sync"
"time"
model "github.com/DataDog/agent-payload/v5/process"
"github.com/DataDog/datadog-agent/comp/core/workloadmeta"
ddconfig "github.com/DataDog/datadog-agent/pkg/config"
"github.com/DataDog/datadog-agent/pkg/process/statsd"
proccontainers "github.com/DataDog/datadog-agent/pkg/process/util/containers"
"github.com/DataDog/datadog-agent/pkg/util/cloudproviders"
"github.com/DataDog/datadog-agent/pkg/util/flavor"
"github.com/DataDog/datadog-agent/pkg/util/log"
)
const (
cacheValidityNoRT = 2 * time.Second
)
// NewContainerCheck returns an instance of the ContainerCheck.
func NewContainerCheck(config ddconfig.Reader, wmeta workloadmeta.Component) *ContainerCheck {
return &ContainerCheck{
config: config,
wmeta: wmeta,
}
}
// ContainerCheck is a check that returns container metadata and stats.
type ContainerCheck struct {
sync.Mutex
config ddconfig.Reader
hostInfo *HostInfo
containerProvider proccontainers.ContainerProvider
lastRates map[string]*proccontainers.ContainerRateMetrics
networkID string
containerFailedLogLimit *log.Limit
maxBatchSize int
wmeta workloadmeta.Component
}
// Init initializes a ContainerCheck instance.
func (c *ContainerCheck) Init(_ *SysProbeConfig, info *HostInfo, _ bool) error {
c.containerProvider = proccontainers.GetSharedContainerProvider(c.wmeta)
c.hostInfo = info
networkID, err := cloudproviders.GetNetworkID(context.TODO())
if err != nil {
log.Infof("no network ID detected: %s", err)
}
c.networkID = networkID
c.containerFailedLogLimit = log.NewLogLimit(10, time.Minute*10)
c.maxBatchSize = getMaxBatchSize(c.config)
return nil
}
// IsEnabled returns true if the check is enabled by configuration
// Keep in mind that ContainerRTCheck.IsEnabled should only be enabled if the `ContainerCheck` is enabled
func (c *ContainerCheck) IsEnabled() bool {
if c.config.GetBool("process_config.run_in_core_agent.enabled") && flavor.GetFlavor() == flavor.ProcessAgent {
return false
}
return canEnableContainerChecks(c.config, true)
}
// SupportsRunOptions returns true if the check supports RunOptions
func (c *ContainerCheck) SupportsRunOptions() bool {
return false
}
// Name returns the name of the ProcessCheck.
func (c *ContainerCheck) Name() string { return ContainerCheckName }
// Realtime indicates if this check only runs in real-time mode.
func (c *ContainerCheck) Realtime() bool { return false }
// ShouldSaveLastRun indicates if the output from the last run should be saved for use in flares
func (c *ContainerCheck) ShouldSaveLastRun() bool { return true }
// Run runs the ContainerCheck to collect a list of running ctrList and the
// stats for each container.
//
//nolint:revive // TODO(PROC) Fix revive linter
func (c *ContainerCheck) Run(nextGroupID func() int32, options *RunOptions) (RunResult, error) {
c.Lock()
defer c.Unlock()
startTime := time.Now()
var err error
var containers []*model.Container
var lastRates map[string]*proccontainers.ContainerRateMetrics
containers, lastRates, _, err = c.containerProvider.GetContainers(cacheValidityNoRT, c.lastRates)
if err == nil {
c.lastRates = lastRates
} else {
log.Debugf("Unable to gather stats for containers, err: %v", err)
}
if len(containers) == 0 {
log.Trace("No containers found")
return nil, nil
}
groupSize := len(containers) / c.maxBatchSize
if len(containers)%c.maxBatchSize != 0 {
groupSize++
}
chunked := chunkContainers(containers, groupSize)
messages := make([]model.MessageBody, 0, groupSize)
groupID := nextGroupID()
for i := 0; i < groupSize; i++ {
messages = append(messages, &model.CollectorContainer{
HostName: c.hostInfo.HostName,
NetworkId: c.networkID,
Info: c.hostInfo.SystemInfo,
Containers: chunked[i],
GroupId: groupID,
GroupSize: int32(groupSize),
ContainerHostType: c.hostInfo.ContainerHostType,
})
}
numContainers := float64(len(containers))
statsd.Client.Gauge("datadog.process.containers.host_count", numContainers, []string{}, 1) //nolint:errcheck
log.Debugf("collected %d containers in %s", int(numContainers), time.Since(startTime))
return StandardRunResult(messages), nil
}
// Cleanup frees any resource held by the ContainerCheck before the agent exits
func (c *ContainerCheck) Cleanup() {}
// chunkContainers formats and chunks the ctrList into a slice of chunks using a specific number of chunks.
func chunkContainers(containers []*model.Container, chunks int) [][]*model.Container {
perChunk := (len(containers) / chunks) + 1
chunked := make([][]*model.Container, 0, chunks)
chunk := make([]*model.Container, 0, perChunk)
for _, ctr := range containers {
chunk = append(chunk, ctr)
if len(chunk) == perChunk {
chunked = append(chunked, chunk)
chunk = make([]*model.Container, 0, perChunk)
}
}
if len(chunk) > 0 {
chunked = append(chunked, chunk)
}
return chunked
}