Skip to content

Commit

Permalink
Adding wildcard support for container inclusion/exclusion. Solves inf…
Browse files Browse the repository at this point in the history
  • Loading branch information
Matteo Cerutti committed May 21, 2017
1 parent 8fdc2ae commit 2d75b5d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 10 deletions.
8 changes: 8 additions & 0 deletions plugins/inputs/docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,22 @@ for the stat structure can be found
## To use TCP, set endpoint = "tcp://[ip]:[port]"
## To use environment variables (ie, docker-machine), set endpoint = "ENV"
endpoint = "unix:///var/run/docker.sock"
## Only collect metrics for these containers, collect all if empty
container_names = []
## Containers to include and exclude. Globs accepted.
## Note that the options are ignored when using the container_names option. An empty array for both will include all containers
container_name_include = []
container_name_exclude = []
## Timeout for docker list, info, and stats commands
timeout = "5s"
## Whether to report for each container per-device blkio (8:0, 8:1...) and
## network (eth0, eth1, ...) stats or not
perdevice = true
## Whether to report for each container total blkio and network stats or not
total = false
Expand Down
67 changes: 57 additions & 10 deletions plugins/inputs/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,33 @@ type DockerLabelFilter struct {
labelExclude filter.Filter
}

type DockerContainerFilter struct {
containerInclude filter.Filter
containerExclude filter.Filter
}

// Docker object
type Docker struct {
Endpoint string
ContainerNames []string

Timeout internal.Duration
PerDevice bool `toml:"perdevice"`
Total bool `toml:"total"`
TagEnvironment []string `toml:"tag_env"`
LabelInclude []string `toml:"docker_label_include"`
LabelExclude []string `toml:"docker_label_exclude"`
LabelFilter DockerLabelFilter

LabelFilter DockerLabelFilter
ContainerInclude []string `toml:"container_name_include"`
ContainerExclude []string `toml:"container_name_exclude"`
ContainerFilter DockerContainerFilter

client *client.Client
engine_host string

testing bool
labelFiltersCreated bool
testing bool
filtersCreated bool
}

// infoWrapper wraps client.Client.List for testing.
Expand Down Expand Up @@ -110,8 +119,15 @@ var sampleConfig = `
## To use TCP, set endpoint = "tcp://[ip]:[port]"
## To use environment variables (ie, docker-machine), set endpoint = "ENV"
endpoint = "unix:///var/run/docker.sock"
## Only collect metrics for these containers, collect all if empty
container_names = []
## Containers to include and exclude. Globs accepted.
## Note that an empty array for both will include all containers
container_name_include = []
container_name_exclude = []
## Timeout for docker list, info, and stats commands
timeout = "5s"
Expand Down Expand Up @@ -161,13 +177,18 @@ func (d *Docker) Gather(acc telegraf.Accumulator) error {
}
d.client = c
}

// Create label filters if not already created
if !d.labelFiltersCreated {
if !d.filtersCreated {
err := d.createLabelFilters()
if err != nil {
return err
}
d.labelFiltersCreated = true
err = d.createContainerFilters()
if err != nil {
return err
}
d.filtersCreated = true
}

// Get daemon info
Expand Down Expand Up @@ -310,6 +331,12 @@ func (d *Docker) gatherContainer(
if !sliceContains(cname, d.ContainerNames) {
return nil
}
} else {
if len(d.ContainerInclude) == 0 || !d.ContainerFilter.containerInclude.Match(cname) {
if len(d.ContainerExclude) > 0 && d.ContainerFilter.containerExclude.Match(cname) {
return nil
}
}
}

ctx, cancel := context.WithTimeout(context.Background(), d.Timeout.Duration)
Expand Down Expand Up @@ -656,16 +683,36 @@ func parseSize(sizeStr string) (int64, error) {
return int64(size), nil
}

func (d *Docker) createContainerFilters() error {
if len(d.ContainerInclude) != 0 {
var err error
d.ContainerFilter.containerInclude, err = filter.Compile(d.ContainerInclude)
if err != nil {
return err
}
}

if len(d.ContainerExclude) != 0 {
var err error
d.ContainerFilter.containerExclude, err = filter.Compile(d.ContainerExclude)
if err != nil {
return err
}
}

return nil
}

func (d *Docker) createLabelFilters() error {
if len(d.LabelInclude) != 0 && d.LabelFilter.labelInclude == nil {
if len(d.LabelInclude) != 0 {
var err error
d.LabelFilter.labelInclude, err = filter.Compile(d.LabelInclude)
if err != nil {
return err
}
}

if len(d.LabelExclude) != 0 && d.LabelFilter.labelExclude == nil {
if len(d.LabelExclude) != 0 {
var err error
d.LabelFilter.labelExclude, err = filter.Compile(d.LabelExclude)
if err != nil {
Expand All @@ -679,9 +726,9 @@ func (d *Docker) createLabelFilters() error {
func init() {
inputs.Add("docker", func() telegraf.Input {
return &Docker{
PerDevice: true,
Timeout: internal.Duration{Duration: time.Second * 5},
labelFiltersCreated: false,
PerDevice: true,
Timeout: internal.Duration{Duration: time.Second * 5},
filtersCreated: false,
}
})
}

0 comments on commit 2d75b5d

Please sign in to comment.