diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f34d495b..234f6ab87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ time before a new metric is included by the plugin. - [#1247](https://github.com/influxdata/telegraf/pull/1247): rollbar input plugin. Thanks @francois2metz and @cduez! - [#1208](https://github.com/influxdata/telegraf/pull/1208): Standardized AWS credentials evaluation & wildcard CloudWatch dimensions. Thanks @johnrengelman! +- [#1263](https://github.com/influxdata/telegraf/issues/1263): Added container_labels to docker plugin ### Bugfixes diff --git a/plugins/inputs/docker/README.md b/plugins/inputs/docker/README.md index 52f9a7adb..bae40aefd 100644 --- a/plugins/inputs/docker/README.md +++ b/plugins/inputs/docker/README.md @@ -22,6 +22,9 @@ for the stat structure can be found endpoint = "unix:///var/run/docker.sock" # Only collect metrics for these containers, collect all if empty container_names = [] + ## Only collect these container labels from docker daemon, collect all if empty but note that + ## this will break prometheus output if containers have inconsistent label sets. + container_labels = [] ``` ### Measurements & Fields: diff --git a/plugins/inputs/docker/docker.go b/plugins/inputs/docker/docker.go index 0af7820e1..4f1643eb3 100644 --- a/plugins/inputs/docker/docker.go +++ b/plugins/inputs/docker/docker.go @@ -22,9 +22,10 @@ import ( // Docker object type Docker struct { - Endpoint string - ContainerNames []string - Timeout internal.Duration + Endpoint string + ContainerNames []string + ContainerLabels []string + Timeout internal.Duration client DockerClient } @@ -56,6 +57,9 @@ var sampleConfig = ` endpoint = "unix:///var/run/docker.sock" ## Only collect metrics for these containers, collect all if empty container_names = [] + ## Only collect these container labels from docker daemon, collect all if empty but note that + ## this will break prometheus output if containers have inconsistent label sets. + container_labels = [] ## Timeout for docker list, info, and stats commands timeout = "5s" ` @@ -232,9 +236,21 @@ func (d *Docker) gatherContainer( return fmt.Errorf("Error decoding: %s", err.Error()) } - // Add labels to tags - for k, label := range container.Labels { - tags[k] = label + // Check if there are container labels specified + if len(d.ContainerLabels) == 0 { + // Add all labels to tags + for k, label := range container.Labels { + tags[k] = label + } + } else { + // Omit labels not in d.ContainerLabels and add placeholders for missing labels + for _, l := range d.ContainerLabels { + if label, ok := container.Labels[l]; ok { + tags[l] = label + } else { + tags[l] = "-" + } + } } gatherContainerStats(v, acc, tags, container.ID)