Use container name from list if no name in container stats (#4854)

This commit is contained in:
Daniel Nelson 2018-10-12 14:36:55 -07:00 committed by GitHub
parent 37fd99abb9
commit 27bd51b9ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 78 additions and 7 deletions

View File

@ -416,7 +416,9 @@ func (d *Docker) gatherContainer(
daemonOSType := r.OSType daemonOSType := r.OSType
// use common (printed at `docker ps`) name for container // use common (printed at `docker ps`) name for container
if v.Name != "" {
tags["container_name"] = strings.TrimPrefix(v.Name, "/") tags["container_name"] = strings.TrimPrefix(v.Name, "/")
}
// Add labels to tags // Add labels to tags
for k, label := range container.Labels { for k, label := range container.Labels {
@ -442,6 +444,7 @@ func (d *Docker) gatherContainer(
} }
} }
} }
if info.State != nil { if info.State != nil {
tags["container_status"] = info.State.Status tags["container_status"] = info.State.Status
statefields := map[string]interface{}{ statefields := map[string]interface{}{
@ -458,7 +461,6 @@ func (d *Docker) gatherContainer(
statefields["finished_at"] = container_time.UnixNano() statefields["finished_at"] = container_time.UnixNano()
} }
acc.AddFields("docker_container_status", statefields, tags, time.Now()) acc.AddFields("docker_container_status", statefields, tags, time.Now())
}
if info.State.Health != nil { if info.State.Health != nil {
healthfields := map[string]interface{}{ healthfields := map[string]interface{}{
@ -467,6 +469,7 @@ func (d *Docker) gatherContainer(
} }
acc.AddFields("docker_container_health", healthfields, tags, time.Now()) acc.AddFields("docker_container_health", healthfields, tags, time.Now())
} }
}
parseContainerStats(v, acc, tags, container.ID, d.PerDevice, d.Total, daemonOSType) parseContainerStats(v, acc, tags, container.ID, d.PerDevice, d.Total, daemonOSType)

View File

@ -3,7 +3,9 @@ package docker
import ( import (
"context" "context"
"crypto/tls" "crypto/tls"
"io/ioutil"
"sort" "sort"
"strings"
"testing" "testing"
"github.com/influxdata/telegraf/testutil" "github.com/influxdata/telegraf/testutil"
@ -747,3 +749,69 @@ func TestContainerStateFilter(t *testing.T) {
}) })
} }
} }
func TestContainerName(t *testing.T) {
tests := []struct {
name string
clientFunc func(host string, tlsConfig *tls.Config) (Client, error)
expected string
}{
{
name: "container stats name is preferred",
clientFunc: func(host string, tlsConfig *tls.Config) (Client, error) {
client := baseClient
client.ContainerListF = func(context.Context, types.ContainerListOptions) ([]types.Container, error) {
var containers []types.Container
containers = append(containers, types.Container{
Names: []string{"/logspout/foo"},
})
return containers, nil
}
client.ContainerStatsF = func(ctx context.Context, containerID string, stream bool) (types.ContainerStats, error) {
return types.ContainerStats{
Body: ioutil.NopCloser(strings.NewReader(`{"name": "logspout"}`)),
}, nil
}
return &client, nil
},
expected: "logspout",
},
{
name: "container stats without name uses container list name",
clientFunc: func(host string, tlsConfig *tls.Config) (Client, error) {
client := baseClient
client.ContainerListF = func(context.Context, types.ContainerListOptions) ([]types.Container, error) {
var containers []types.Container
containers = append(containers, types.Container{
Names: []string{"/logspout"},
})
return containers, nil
}
client.ContainerStatsF = func(ctx context.Context, containerID string, stream bool) (types.ContainerStats, error) {
return types.ContainerStats{
Body: ioutil.NopCloser(strings.NewReader(`{}`)),
}, nil
}
return &client, nil
},
expected: "logspout",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := Docker{
newClient: tt.clientFunc,
}
var acc testutil.Accumulator
err := d.Gather(&acc)
require.NoError(t, err)
for _, metric := range acc.Metrics {
// This tag is set on all container measurements
if metric.Measurement == "docker_container_mem" {
require.Equal(t, tt.expected, metric.Tags["container_name"])
}
}
})
}
}