Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added container_labels parameter #1270

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions plugins/inputs/docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ 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
container_labels = []
```

### Measurements & Fields:
Expand Down
27 changes: 21 additions & 6 deletions plugins/inputs/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -56,6 +57,8 @@ 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
container_labels = []
## Timeout for docker list, info, and stats commands
timeout = "5s"
`
Expand Down Expand Up @@ -232,9 +235,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)
Expand Down