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

Exporting of docker runtime variables as tags #2532

Closed
wants to merge 4 commits into from
Closed
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
27 changes: 25 additions & 2 deletions plugins/inputs/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ type Docker struct {
Endpoint string
ContainerNames []string
Timeout internal.Duration
PerDevice bool `toml:"perdevice"`
Total bool `toml:"total"`
PerDevice bool `toml:"perdevice"`
Total bool `toml:"total"`
EnvToTag []string `toml:"env_to_tag"`

client *client.Client
engine_host string
Expand Down Expand Up @@ -98,6 +99,10 @@ var sampleConfig = `
perdevice = true
## Whether to report for each container total blkio and network stats or not
total = false
## List of varibles from container runtime, e.g.: "docker run -e APPLICATION_NAME=shop -e APP_ID=ver1.2 shop"
## Names of varibles will be transformed to tags, e.g.: container_env_APPLICATION_NAME container_env_APP_ID
## Values of variables will be transformed to values of tags, e.g.: container_env_APPLICATION_NAME=shop, container_env_APP_ID=ver1.2
env_to_tag = [ "APPLICATION_NAME", "APP_ID" ]

`

Expand Down Expand Up @@ -251,6 +256,11 @@ func (d *Docker) gatherContainer(
// Not sure what to do with other names, just take the first.
cname = strings.TrimPrefix(container.Names[0], "/")
}
// getting container configuration ( container.Config )
conf, err := d.client.ContainerInspect(context.TODO(), cname)
if err != nil {
return err
}

// the image name sometimes has a version part, or a private repo
// ie, rabbitmq:3-management or docker.someco.net:4443/rabbitmq:3-management
Expand All @@ -270,6 +280,19 @@ func (d *Docker) gatherContainer(
"container_image": imageName,
"container_version": imageVersion,
}

for _, config_env := range conf.Config.Env {
confNameValue := strings.Split(config_env, "=")
confName := confNameValue[0]
confValue := confNameValue[1]
for _, name := range d.EnvToTag {
if confName == name {
tag := "container_env_" + confName
tags[tag] = confValue
}
}
}

if len(d.ContainerNames) > 0 {
if !sliceContains(cname, d.ContainerNames) {
return nil
Expand Down