Add Docker container environment variables as tags. Only whitelisted #2580 (#2581)

This commit is contained in:
rsingh2411
2017-05-19 05:28:34 +05:30
committed by Daniel Nelson
parent 02d40565c6
commit f28f166069
5 changed files with 66 additions and 0 deletions

View File

@@ -31,6 +31,7 @@ type Docker struct {
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"`
@@ -79,6 +80,18 @@ func statsWrapper(
return fc.ContainerStats(ctx, containerID, stream)
}
func inspectWrapper(
c *client.Client,
ctx context.Context,
containerID string,
) (types.ContainerJSON, error) {
if c != nil {
return c.ContainerInspect(ctx, containerID)
}
fc := FakeDockerClient{}
return fc.ContainerInspect(ctx, containerID)
}
// KB, MB, GB, TB, PB...human friendly
const (
KB = 1000
@@ -107,6 +120,8 @@ var sampleConfig = `
perdevice = true
## Whether to report for each container total blkio and network stats or not
total = false
## Which environment variables should we use as a tag
##tag_env = ["JAVA_HOME", "HEAP_SIZE"]
## docker labels to include and exclude as tags. Globs accepted.
## Note that an empty array for both will include all labels as tags
@@ -321,6 +336,23 @@ func (d *Docker) gatherContainer(
}
}
// Add whitelisted environment variables to tags
if len(d.TagEnvironment) > 0 {
info, err := inspectWrapper(d.client, ctx, container.ID)
if err != nil {
return fmt.Errorf("Error inspecting docker container: %s", err.Error())
}
for _, envvar := range info.Config.Env {
for _, configvar := range d.TagEnvironment {
dock_env := strings.SplitN(envvar, "=", 2)
//check for presence of tag in whitelist
if len(dock_env) == 2 && len(strings.TrimSpace(dock_env[1])) != 0 && configvar == dock_env[0] {
tags[dock_env[0]] = dock_env[1]
}
}
}
}
gatherContainerStats(v, acc, tags, container.ID, d.PerDevice, d.Total)
return nil