use whitelisted docker env variables as metric tags
This commit is contained in:
parent
fd1feff7b4
commit
cf5994a733
|
@ -22,11 +22,12 @@ import (
|
|||
|
||||
// Docker object
|
||||
type Docker struct {
|
||||
Endpoint string
|
||||
ContainerNames []string
|
||||
Timeout internal.Duration
|
||||
PerDevice bool `toml:"perdevice"`
|
||||
Total bool `toml:"total"`
|
||||
Endpoint string
|
||||
ContainerNames []string
|
||||
Timeout internal.Duration
|
||||
TagEnvWhitelist []string
|
||||
PerDevice bool `toml:"perdevice"`
|
||||
Total bool `toml:"total"`
|
||||
|
||||
client DockerClient
|
||||
engine_host string
|
||||
|
@ -37,6 +38,7 @@ type DockerClient interface {
|
|||
Info(ctx context.Context) (types.Info, error)
|
||||
ContainerList(ctx context.Context, options types.ContainerListOptions) ([]types.Container, error)
|
||||
ContainerStats(ctx context.Context, containerID string, stream bool) (io.ReadCloser, error)
|
||||
ContainerInspect(ctx context.Context, containerID string) (types.ContainerJSON, error)
|
||||
}
|
||||
|
||||
// KB, MB, GB, TB, PB...human friendly
|
||||
|
@ -62,6 +64,9 @@ var sampleConfig = `
|
|||
## Timeout for docker list, info, and stats commands
|
||||
timeout = "5s"
|
||||
|
||||
## Use certain environment variables as metric tags
|
||||
tag_env_whitelist = []
|
||||
|
||||
## Whether to report for each container per-device blkio (8:0, 8:1...) and
|
||||
## network (eth0, eth1, ...) stats or not
|
||||
perdevice = true
|
||||
|
@ -265,6 +270,30 @@ func (d *Docker) gatherContainer(
|
|||
tags[k] = label
|
||||
}
|
||||
|
||||
// Add whitelisted environment variables as tags
|
||||
if len(d.TagEnvWhitelist) > 0 {
|
||||
// Get container environments and split them to key value pairs
|
||||
containerJson, _ := d.client.ContainerInspect(ctx, container.ID)
|
||||
containerEnvMap := make(map[string]string, len(containerJson.Config.Env))
|
||||
for _, value := range containerJson.Config.Env {
|
||||
pair := strings.SplitN(value, "=", 2)
|
||||
if len(pair) == 1 {
|
||||
containerEnvMap[pair[0]] = ""
|
||||
} else {
|
||||
containerEnvMap[pair[0]] = pair[1]
|
||||
}
|
||||
}
|
||||
|
||||
// Add matching environment variables
|
||||
for containerEnvKey, value := range containerEnvMap {
|
||||
for _, whitelistedEnv := range d.TagEnvWhitelist {
|
||||
if whitelistedEnv == containerEnvKey {
|
||||
tags[whitelistedEnv] = value
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
gatherContainerStats(v, acc, tags, container.ID, d.PerDevice, d.Total)
|
||||
|
||||
return nil
|
||||
|
|
|
@ -384,6 +384,10 @@ func (d FakeDockerClient) ContainerStats(ctx context.Context, containerID string
|
|||
return stat, nil
|
||||
}
|
||||
|
||||
func (d FakeDockerClient) ContainerInspect(ctx context.Context, containerID string) (types.ContainerJSON, error) {
|
||||
return types.ContainerJSON{}, nil
|
||||
}
|
||||
|
||||
func TestDockerGatherInfo(t *testing.T) {
|
||||
var acc testutil.Accumulator
|
||||
client := FakeDockerClient{}
|
||||
|
|
Loading…
Reference in New Issue