improvement(go.d/docker): add option to filter containers (#19337)
Ilya Mashchenko committed
Jan 7, 2025 at 20:46 UTC
93156bd454ab2ccd2e5c1dbac5c10e1d5d2097ba
6 files changed
+31
-2
src/go/plugin/go.d/collector/docker/collect.go
+6
-2
@@ -111,7 +111,7 @@ func (c *Collector) collectContainers(mx map[string]int64) error {
111
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout.Duration())
112
defer cancel()
113
114
- v, err := c.client.ContainerList(ctx, typesContainer.ListOptions{
114
+ containers, err := c.client.ContainerList(ctx, typesContainer.ListOptions{
115
All: true,
116
Filters: filters.NewArgs(filters.KeyValuePair{Key: "health", Value: status}),
117
Size: c.CollectContainerSize,
@@ -119,7 +119,7 @@ func (c *Collector) collectContainers(mx map[string]int64) error {
119
if err != nil {
120
return err
121
}
122
- containerSet[status] = v
122
+ containerSet[status] = containers
123
return nil
124
125
}(); err != nil {
@@ -154,6 +154,10 @@ func (c *Collector) collectContainers(mx map[string]int64) error {
154
155
name := strings.TrimPrefix(cntr.Names[0], "/")
156
157
+ if c.cntrSr != nil && !c.cntrSr.MatchString(name) {
158
+ continue
159
+ }
160
+
161
seen[name] = true
162
163
if !c.containers[name] {
src/go/plugin/go.d/collector/docker/collector.go
+13
@@ -8,6 +8,7 @@ import (
8
"errors"
9
"time"
10
11
+ "github.com/netdata/netdata/go/plugins/pkg/matcher"
12
"github.com/netdata/netdata/go/plugins/plugin/go.d/agent/module"
13
"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/confopt"
14
"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/dockerhost"
@@ -35,6 +36,7 @@ func New() *Collector {
36
Config: Config{
37
Address: docker.DefaultDockerHost,
38
Timeout: confopt.Duration(time.Second * 2),
39
+ ContainerSelector: "*",
40
CollectContainerSize: false,
41
},
42
@@ -42,6 +44,7 @@ func New() *Collector {
44
newClient: func(cfg Config) (dockerClient, error) {
45
return docker.NewClientWithOpts(docker.WithHost(cfg.Address))
46
},
47
+ cntrSr: matcher.TRUE(),
48
containers: make(map[string]bool),
49
}
50
}
@@ -51,6 +54,7 @@ type Config struct {
54
UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"`
55
Address string `yaml:"address" json:"address"`
56
Timeout confopt.Duration `yaml:"timeout,omitempty" json:"timeout"`
57
+ ContainerSelector string `yaml:"container_selector,omitempty" json:"container_selector"`
58
CollectContainerSize bool `yaml:"collect_container_size" json:"collect_container_size"`
59
}
60
@@ -66,6 +70,7 @@ type (
70
71
verNegotiated bool
72
containers map[string]bool
73
+ cntrSr matcher.Matcher
74
}
75
dockerClient interface {
76
NegotiateAPIVersion(context.Context)
@@ -85,6 +90,14 @@ func (c *Collector) Init(context.Context) error {
90
c.Infof("using docker host from environment: %s ", addr)
91
c.Address = addr
92
}
93
+ if c.ContainerSelector != "" {
94
+ sr, err := matcher.NewSimplePatternsMatcher(c.ContainerSelector)
95
+ if err != nil {
96
+ return err
97
+ }
98
+ c.cntrSr = sr
99
+ }
100
+
101
return nil
102
}
103
src/go/plugin/go.d/collector/docker/config_schema.json
+6
@@ -23,6 +23,12 @@
23
"type": "number",
24
"default": 2
25
},
26
+ "container_selector": {
27
+ "title": "Container Selector",
28
+ "description": "[Pattern](https://github.com/netdata/netdata/tree/master/src/libnetdata/simple_pattern#readme) to specify which containers to monitor. Leave empty to collect metrics for all containers.",
29
+ "type": "string",
30
+ "default": "*"
31
+ },
32
"collect_container_size": {
33
"title": "Collect container size",
34
"description": "Collect container writable layer size.",
src/go/plugin/go.d/collector/docker/metadata.yaml
+4
@@ -73,6 +73,10 @@ modules:
73
description: Request timeout in seconds.
74
default_value: 2
75
required: false
76
+ - name: container_selector
77
+ description: [Pattern](https://github.com/netdata/netdata/tree/master/src/libnetdata/simple_pattern#readme) to specify which containers to monitor.
78
+ default_value: "*"
79
+ required: false
80
- name: collect_container_size
81
description: Whether to collect container writable layer size.
82
default_value: "no"
src/go/plugin/go.d/collector/docker/testdata/config.json
+1
@@ -3,5 +3,6 @@
3
"update_every": 123,
4
"address": "ok",
5
"timeout": 123.123,
6
+ "container_selector": "ok",
7
"collect_container_size": true
8
}
src/go/plugin/go.d/collector/docker/testdata/config.yaml
+1
@@ -2,4 +2,5 @@ vnode: "ok"
2
update_every: 123
3
address: "ok"
4
timeout: 123.123
5
+container_selector: "ok"
6
collect_container_size: yes