Fix incorrect container name gathered in docker input (#4391)

This commit is contained in:
Greg
2018-07-12 19:41:49 -06:00
committed by Daniel Nelson
parent 8ff63a4b79
commit 0da94a1b3c
3 changed files with 107 additions and 127 deletions

View File

@@ -365,10 +365,18 @@ func (d *Docker) gatherContainer(
) error {
var v *types.StatsJSON
// Parse container name
cname := "unknown"
if len(container.Names) > 0 {
// Not sure what to do with other names, just take the first.
cname = strings.TrimPrefix(container.Names[0], "/")
var cname string
for _, name := range container.Names {
trimmedName := strings.TrimPrefix(name, "/")
match := d.containerFilter.Match(trimmedName)
if match {
cname = trimmedName
break
}
}
if cname == "" {
return nil
}
// the image name sometimes has a version part, or a private repo
@@ -391,10 +399,6 @@ func (d *Docker) gatherContainer(
"container_version": imageVersion,
}
if !d.containerFilter.Match(cname) {
return nil
}
ctx, cancel := context.WithTimeout(context.Background(), d.Timeout.Duration)
defer cancel()
r, err := d.client.ContainerStats(ctx, container.ID, false)
@@ -411,6 +415,9 @@ func (d *Docker) gatherContainer(
}
daemonOSType := r.OSType
// use common (printed at `docker ps`) name for container
tags["container_name"] = strings.TrimPrefix(v.Name, "/")
// Add labels to tags
for k, label := range container.Labels {
if d.labelFilter.Match(k) {
@@ -461,12 +468,12 @@ func (d *Docker) gatherContainer(
acc.AddFields("docker_container_health", healthfields, tags, time.Now())
}
gatherContainerStats(v, acc, tags, container.ID, d.PerDevice, d.Total, daemonOSType)
parseContainerStats(v, acc, tags, container.ID, d.PerDevice, d.Total, daemonOSType)
return nil
}
func gatherContainerStats(
func parseContainerStats(
stat *types.StatsJSON,
acc telegraf.Accumulator,
tags map[string]string,