Skip to content

Commit

Permalink
improvement(go.d/docker): add option to filter containers (netdata#19337
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ilyam8 authored Jan 7, 2025
1 parent 9ef8dad commit 93156bd
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/go/plugin/go.d/collector/docker/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@ func (c *Collector) collectContainers(mx map[string]int64) error {
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout.Duration())
defer cancel()

v, err := c.client.ContainerList(ctx, typesContainer.ListOptions{
containers, err := c.client.ContainerList(ctx, typesContainer.ListOptions{
All: true,
Filters: filters.NewArgs(filters.KeyValuePair{Key: "health", Value: status}),
Size: c.CollectContainerSize,
})
if err != nil {
return err
}
containerSet[status] = v
containerSet[status] = containers
return nil

}(); err != nil {
Expand Down Expand Up @@ -154,6 +154,10 @@ func (c *Collector) collectContainers(mx map[string]int64) error {

name := strings.TrimPrefix(cntr.Names[0], "/")

if c.cntrSr != nil && !c.cntrSr.MatchString(name) {
continue
}

seen[name] = true

if !c.containers[name] {
Expand Down
13 changes: 13 additions & 0 deletions src/go/plugin/go.d/collector/docker/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"time"

"github.com/netdata/netdata/go/plugins/pkg/matcher"
"github.com/netdata/netdata/go/plugins/plugin/go.d/agent/module"
"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/confopt"
"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/dockerhost"
Expand Down Expand Up @@ -35,13 +36,15 @@ func New() *Collector {
Config: Config{
Address: docker.DefaultDockerHost,
Timeout: confopt.Duration(time.Second * 2),
ContainerSelector: "*",
CollectContainerSize: false,
},

charts: summaryCharts.Copy(),
newClient: func(cfg Config) (dockerClient, error) {
return docker.NewClientWithOpts(docker.WithHost(cfg.Address))
},
cntrSr: matcher.TRUE(),
containers: make(map[string]bool),
}
}
Expand All @@ -51,6 +54,7 @@ type Config struct {
UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"`
Address string `yaml:"address" json:"address"`
Timeout confopt.Duration `yaml:"timeout,omitempty" json:"timeout"`
ContainerSelector string `yaml:"container_selector,omitempty" json:"container_selector"`
CollectContainerSize bool `yaml:"collect_container_size" json:"collect_container_size"`
}

Expand All @@ -66,6 +70,7 @@ type (

verNegotiated bool
containers map[string]bool
cntrSr matcher.Matcher
}
dockerClient interface {
NegotiateAPIVersion(context.Context)
Expand All @@ -85,6 +90,14 @@ func (c *Collector) Init(context.Context) error {
c.Infof("using docker host from environment: %s ", addr)
c.Address = addr
}
if c.ContainerSelector != "" {
sr, err := matcher.NewSimplePatternsMatcher(c.ContainerSelector)
if err != nil {
return err
}
c.cntrSr = sr
}

return nil
}

Expand Down
6 changes: 6 additions & 0 deletions src/go/plugin/go.d/collector/docker/config_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
"type": "number",
"default": 2
},
"container_selector": {
"title": "Container Selector",
"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.",
"type": "string",
"default": "*"
},
"collect_container_size": {
"title": "Collect container size",
"description": "Collect container writable layer size.",
Expand Down
4 changes: 4 additions & 0 deletions src/go/plugin/go.d/collector/docker/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ modules:
description: Request timeout in seconds.
default_value: 2
required: false
- name: container_selector
description: [Pattern](https://github.com/netdata/netdata/tree/master/src/libnetdata/simple_pattern#readme) to specify which containers to monitor.
default_value: "*"
required: false
- name: collect_container_size
description: Whether to collect container writable layer size.
default_value: "no"
Expand Down
1 change: 1 addition & 0 deletions src/go/plugin/go.d/collector/docker/testdata/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"update_every": 123,
"address": "ok",
"timeout": 123.123,
"container_selector": "ok",
"collect_container_size": true
}
1 change: 1 addition & 0 deletions src/go/plugin/go.d/collector/docker/testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ vnode: "ok"
update_every: 123
address: "ok"
timeout: 123.123
container_selector: "ok"
collect_container_size: yes

0 comments on commit 93156bd

Please sign in to comment.