Skip to content

Commit

Permalink
Add Docker container environment variables as tags. Only whitelisted #…
Browse files Browse the repository at this point in the history
  • Loading branch information
rsingh2411 committed Apr 3, 2017
1 parent 78c7f4e commit 92ff944
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 7 deletions.
2 changes: 2 additions & 0 deletions plugins/inputs/docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ for the stat structure can be found
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
gather_environment = ["JAVA_HOME", "HEAP_SIZE"]
```

### Measurements & Fields:
Expand Down
46 changes: 41 additions & 5 deletions plugins/inputs/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
PerDevice bool `toml:"perdevice"`
Total bool `toml:"total"`
GatherEnvironment []string `toml:"gather_environment"`

client *client.Client
engine_host string
Expand Down Expand Up @@ -70,6 +71,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
Expand Down Expand Up @@ -98,6 +111,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
##gather_environment = ["JAVA_HOME", "HEAP_SIZE"]
`

Expand Down Expand Up @@ -296,6 +311,27 @@ func (d *Docker) gatherContainer(
tags[k] = label
}

// Add whitelisted environment variables to tags
if len(d.GatherEnvironment) > 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 _, prefix := range d.GatherEnvironment {
if strings.Count(envvar, "=") > 1 {
continue
}
if strings.HasPrefix(envvar, prefix) {
kvs := strings.SplitN(envvar, "=", 2)
if len(kvs) == 2 && len(kvs[1]) > 0 {
tags[kvs[0]] = kvs[1]
}
}
}
}
}

gatherContainerStats(v, acc, tags, container.ID, d.PerDevice, d.Total)

return nil
Expand Down
11 changes: 9 additions & 2 deletions plugins/inputs/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,9 @@ func testStats() *types.StatsJSON {
func TestDockerGatherInfo(t *testing.T) {
var acc testutil.Accumulator
d := Docker{
client: nil,
testing: true,
client: nil,
testing: true,
GatherEnvironment: []string{"ENVVAR1", "ENVVAR2", "ENVVAR3", "ENVVAR4", "ENVVAR5", "ENVVAR6"},
}

err := d.Gather(&acc)
Expand Down Expand Up @@ -294,6 +295,9 @@ func TestDockerGatherInfo(t *testing.T) {
"cpu": "cpu3",
"container_version": "v2.2.2",
"engine_host": "absol",
"ENVVAR1": "loremipsum",
"ENVVAR2": "dolorsitamet",
"ENVVAR6": " ",
},
)
acc.AssertContainsTaggedFields(t,
Expand Down Expand Up @@ -340,6 +344,9 @@ func TestDockerGatherInfo(t *testing.T) {
"container_name": "etcd2",
"container_image": "quay.io:4443/coreos/etcd",
"container_version": "v2.2.2",
"ENVVAR1": "loremipsum",
"ENVVAR2": "dolorsitamet",
"ENVVAR6": " ",
},
)

Expand Down
19 changes: 19 additions & 0 deletions plugins/inputs/docker/fake_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/registry"
)

Expand Down Expand Up @@ -141,3 +142,21 @@ func (d FakeDockerClient) ContainerStats(ctx context.Context, containerID string
stat.Body = ioutil.NopCloser(strings.NewReader(jsonStat))
return stat, nil
}

func (d FakeDockerClient) ContainerInspect(ctx context.Context, containerID string) (types.ContainerJSON, error) {
json := types.ContainerJSON{
Config: &container.Config{
Env: []string{
"ENVVAR1=loremipsum",
"ENVVAR2=dolorsitamet",
"ENVVAR3==ubuntu:10.04",
"ENVVAR4",
"ENVVAR5=",
"ENVVAR6= ",
"PATH=/bin:/sbin",
},
},
}

return json, nil
}

0 comments on commit 92ff944

Please sign in to comment.