-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathcheck.go
More file actions
334 lines (282 loc) · 9.83 KB
/
check.go
File metadata and controls
334 lines (282 loc) · 9.83 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
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
// 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.
//go:build linux
// Package kata implements the kata_containers check.
package kata
import (
"bufio"
"fmt"
"io"
"maps"
"net"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"time"
yaml "go.yaml.in/yaml/v2"
"github.com/DataDog/datadog-agent/comp/core/autodiscovery/integration"
tagger "github.com/DataDog/datadog-agent/comp/core/tagger/def"
taggertypes "github.com/DataDog/datadog-agent/comp/core/tagger/types"
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
"github.com/DataDog/datadog-agent/pkg/aggregator/sender"
"github.com/DataDog/datadog-agent/pkg/collector/check"
core "github.com/DataDog/datadog-agent/pkg/collector/corechecks"
"github.com/DataDog/datadog-agent/pkg/metrics/servicecheck"
"github.com/DataDog/datadog-agent/pkg/util/option"
prom "github.com/DataDog/datadog-agent/pkg/util/prometheus"
)
const (
// CheckName is the name of the check
CheckName = "kata_containers"
shimSocket = "shim-monitor.sock"
shimDialTimeout = 5 * time.Second
shimReadTimeout = 10 * time.Second
defaultScrapeInterval = 15 * time.Second
)
var defaultSandboxStoragePaths = []string{"/run/vc/sbs", "/run/kata"}
var defaultRenameLabels = map[string]string{"version": "go_version"}
// KataConfig holds the check instance configuration
type KataConfig struct {
SandboxStoragePaths []string `yaml:"sandbox_storage_paths"`
RenameLabels map[string]string `yaml:"rename_labels"`
ExcludeLabels []string `yaml:"exclude_labels"`
Tags []string `yaml:"tags"`
}
// KataCheck collects metrics from Kata Containers sandboxes
type KataCheck struct {
core.CheckBase
instance *KataConfig
tagger tagger.Component
store workloadmeta.Component
excludeSet map[string]struct{} // built once at Configure time
mu sync.RWMutex
sandboxContainerID map[string]string // sandboxID -> containerID, updated by workloadmeta events
stopOnce sync.Once
stopCh chan struct{}
}
// Factory returns a check factory for kata_containers
func Factory(store workloadmeta.Component, tagger tagger.Component) option.Option[func() check.Check] {
return option.New(func() check.Check {
return core.NewLongRunningCheckWrapper(&KataCheck{
CheckBase: core.NewCheckBase(CheckName),
instance: &KataConfig{},
tagger: tagger,
store: store,
sandboxContainerID: make(map[string]string),
stopCh: make(chan struct{}),
})
})
}
// Parse parses the KataConfig and sets defaults
func (c *KataConfig) Parse(data []byte) error {
c.SandboxStoragePaths = defaultSandboxStoragePaths
c.RenameLabels = maps.Clone(defaultRenameLabels)
if err := yaml.Unmarshal(data, c); err != nil {
return err
}
if len(c.SandboxStoragePaths) == 0 {
c.SandboxStoragePaths = defaultSandboxStoragePaths
}
if c.RenameLabels == nil {
c.RenameLabels = maps.Clone(defaultRenameLabels)
}
return nil
}
// Configure parses the check configuration
func (c *KataCheck) Configure(senderManager sender.SenderManager, _ uint64, config, initConfig integration.Data, source string) error {
if err := c.CommonConfigure(senderManager, initConfig, config, source); err != nil {
return err
}
if err := c.instance.Parse(config); err != nil {
return err
}
c.excludeSet = make(map[string]struct{}, len(c.instance.ExcludeLabels))
for _, l := range c.instance.ExcludeLabels {
c.excludeSet[l] = struct{}{}
}
return nil
}
// Run is the long-running event loop: it subscribes to workloadmeta container
// events to maintain a sandboxID→containerID cache, and periodically scrapes
// all discovered Kata shim sockets.
func (c *KataCheck) Run() error {
filter := workloadmeta.NewFilterBuilder().
AddKind(workloadmeta.KindContainer).
Build()
containerEventsCh := c.store.Subscribe(CheckName, workloadmeta.NormalPriority, filter)
defer c.store.Unsubscribe(containerEventsCh)
ticker := time.NewTicker(defaultScrapeInterval)
defer ticker.Stop()
for {
select {
case eventBundle, ok := <-containerEventsCh:
if !ok {
return nil
}
c.processContainerEvents(eventBundle)
case <-ticker.C:
c.runScrape()
case <-c.stopCh:
return nil
}
}
}
// Stop signals the Run loop to exit.
func (c *KataCheck) Stop() {
c.stopOnce.Do(func() { close(c.stopCh) })
}
// processContainerEvents updates the sandboxContainerID cache from a workloadmeta event bundle.
func (c *KataCheck) processContainerEvents(eventBundle workloadmeta.EventBundle) {
defer eventBundle.Acknowledge()
c.mu.Lock()
defer c.mu.Unlock()
for _, event := range eventBundle.Events {
ctr, ok := event.Entity.(*workloadmeta.Container)
if !ok || ctr.SandboxID == "" {
continue
}
switch event.Type {
case workloadmeta.EventTypeSet:
c.sandboxContainerID[ctr.SandboxID] = ctr.ID
case workloadmeta.EventTypeUnset:
delete(c.sandboxContainerID, ctr.SandboxID)
}
}
}
// runScrape discovers sandboxes and scrapes each one.
func (c *KataCheck) runScrape() {
s, err := c.GetSender()
if err != nil {
_ = c.Warnf("kata_containers: failed to get sender: %v", err)
return
}
defer s.Commit()
sandboxes := c.discoverSandboxes()
s.Gauge("kata.running_shim_count", float64(len(sandboxes)), "", c.instance.Tags)
for sandboxID, socketPath := range sandboxes {
baseTags := c.buildBaseTags(sandboxID)
c.scrapeSandbox(s, sandboxID, socketPath, baseTags)
}
}
// buildBaseTags returns sandbox_id tag plus any orchestrator tags from the tagger.
func (c *KataCheck) buildBaseTags(sandboxID string) []string {
tags := []string{"sandbox_id:" + sandboxID}
c.mu.RLock()
containerID, ok := c.sandboxContainerID[sandboxID]
c.mu.RUnlock()
if ok {
entityID := taggertypes.NewEntityID(taggertypes.ContainerID, containerID)
if taggerTags, err := c.tagger.Tag(entityID, taggertypes.OrchestratorCardinality); err == nil {
tags = append(tags, taggerTags...)
}
}
return tags
}
// discoverSandboxes scans sandbox storage paths and returns a map of sandboxID → socketPath
func (c *KataCheck) discoverSandboxes() map[string]string {
sandboxes := make(map[string]string)
for _, basePath := range c.instance.SandboxStoragePaths {
if _, err := os.Stat(basePath); os.IsNotExist(err) {
continue
}
entries, err := os.ReadDir(basePath)
if err != nil {
_ = c.Warnf("kata_containers: failed to read directory %s: %v", basePath, err)
continue
}
for _, entry := range entries {
if !entry.IsDir() {
continue
}
socketPath := filepath.Join(basePath, entry.Name(), shimSocket)
if _, err := os.Stat(socketPath); err == nil {
sandboxes[entry.Name()] = socketPath
}
}
}
return sandboxes
}
// scrapeSandbox scrapes Prometheus metrics from a single sandbox's shim socket.
// It dials the unix socket directly and issues a raw HTTP/1.1 GET.
// https://github.com/kata-containers/kata-containers/blob/main/docs/design/kata-2-0-metrics.md#metrics-architecture
func (c *KataCheck) scrapeSandbox(s sender.Sender, sandboxID, socketPath string, baseTags []string) {
conn, err := net.DialTimeout("unix", socketPath, shimDialTimeout)
if err != nil {
s.ServiceCheck("kata.openmetrics.health", servicecheck.ServiceCheckCritical, "", baseTags,
fmt.Sprintf("failed to connect to sandbox %s: %v", sandboxID, err))
return
}
defer conn.Close()
conn.SetDeadline(time.Now().Add(shimReadTimeout)) //nolint:errcheck
fmt.Fprintf(conn, "GET /metrics HTTP/1.0\r\nHost: local\r\n\r\n") //nolint:errcheck
resp, err := http.ReadResponse(bufio.NewReader(conn), nil)
if err != nil {
s.ServiceCheck("kata.openmetrics.health", servicecheck.ServiceCheckCritical, "", baseTags,
fmt.Sprintf("failed to read response from sandbox %s: %v", sandboxID, err))
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
s.ServiceCheck("kata.openmetrics.health", servicecheck.ServiceCheckCritical, "", baseTags,
fmt.Sprintf("failed to read body from sandbox %s: %v", sandboxID, err))
return
}
families, err := prom.ParseMetrics(body)
if err != nil {
s.ServiceCheck("kata.openmetrics.health", servicecheck.ServiceCheckCritical, "", baseTags,
fmt.Sprintf("failed to parse metrics from sandbox %s: %v", sandboxID, err))
return
}
for _, family := range families {
for _, sample := range family.Samples {
rawName := sample.Metric["__name__"]
if rawName == "" {
rawName = family.Name
}
metricName := formatMetricName(rawName)
tags := c.buildSampleTags(baseTags, sample.Metric)
switch strings.ToUpper(family.Type) {
case "COUNTER":
s.Rate(metricName, sample.Value, "", tags)
default: // GAUGE, HISTOGRAM, SUMMARY, UNTYPED
s.Gauge(metricName, sample.Value, "", tags)
}
}
}
s.ServiceCheck("kata.openmetrics.health", servicecheck.ServiceCheckOK, "", baseTags, "")
}
// formatMetricName converts a raw Prometheus metric name to a Datadog metric name.
// "kata_hypervisor_fds" -> "kata.hypervisor.fds"
func formatMetricName(rawName string) string {
name := rawName
if after, ok := strings.CutPrefix(name, "kata_"); ok {
name = after
}
name = strings.ReplaceAll(name, "_", ".")
return "kata." + name
}
// buildSampleTags builds the full tag list for a metric sample from pre-resolved baseTags.
func (c *KataCheck) buildSampleTags(baseTags []string, metric prom.Metric) []string {
tags := make([]string, len(baseTags), len(baseTags)+len(metric)+len(c.instance.Tags))
copy(tags, baseTags)
tags = append(tags, c.instance.Tags...)
for k, v := range metric {
if k == "__name__" {
continue
}
if _, excluded := c.excludeSet[k]; excluded {
continue
}
labelName := k
if renamed, ok := c.instance.RenameLabels[k]; ok {
labelName = renamed
}
tags = append(tags, labelName+":"+v)
}
return tags
}