From 600f9fa067b706d82cf20749f69048b22713b472 Mon Sep 17 00:00:00 2001 From: Daniel Nelson Date: Tue, 7 Nov 2017 11:41:53 -0800 Subject: [PATCH] Use current time if container read time is zero value (#3437) (cherry picked from commit cbbdf1043b4005911fea0f2c74362f80c7cf2e77) --- plugins/inputs/docker/docker.go | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/plugins/inputs/docker/docker.go b/plugins/inputs/docker/docker.go index cd32cf115..c59310447 100644 --- a/plugins/inputs/docker/docker.go +++ b/plugins/inputs/docker/docker.go @@ -339,7 +339,11 @@ func gatherContainerStats( total bool, daemonOSType string, ) { - now := stat.Read + tm := stat.Read + + if tm.Before(time.Unix(0, 0)) { + tm = time.Now() + } memfields := map[string]interface{}{ "container_id": id, @@ -399,7 +403,7 @@ func gatherContainerStats( memfields["private_working_set"] = stat.MemoryStats.PrivateWorkingSet } - acc.AddFields("docker_container_mem", memfields, tags, now) + acc.AddFields("docker_container_mem", memfields, tags, tm) cpufields := map[string]interface{}{ "usage_total": stat.CPUStats.CPUUsage.TotalUsage, @@ -424,7 +428,7 @@ func gatherContainerStats( cputags := copyTags(tags) cputags["cpu"] = "cpu-total" - acc.AddFields("docker_container_cpu", cpufields, cputags, now) + acc.AddFields("docker_container_cpu", cpufields, cputags, tm) // If we have OnlineCPUs field, then use it to restrict stats gathering to only Online CPUs // (https://github.com/moby/moby/commit/115f91d7575d6de6c7781a96a082f144fd17e400) @@ -442,7 +446,7 @@ func gatherContainerStats( "usage_total": percpu, "container_id": id, } - acc.AddFields("docker_container_cpu", fields, percputags, now) + acc.AddFields("docker_container_cpu", fields, percputags, tm) } totalNetworkStatMap := make(map[string]interface{}) @@ -462,7 +466,7 @@ func gatherContainerStats( if perDevice { nettags := copyTags(tags) nettags["network"] = network - acc.AddFields("docker_container_net", netfields, nettags, now) + acc.AddFields("docker_container_net", netfields, nettags, tm) } if total { for field, value := range netfields { @@ -495,17 +499,17 @@ func gatherContainerStats( nettags := copyTags(tags) nettags["network"] = "total" totalNetworkStatMap["container_id"] = id - acc.AddFields("docker_container_net", totalNetworkStatMap, nettags, now) + acc.AddFields("docker_container_net", totalNetworkStatMap, nettags, tm) } - gatherBlockIOMetrics(stat, acc, tags, now, id, perDevice, total) + gatherBlockIOMetrics(stat, acc, tags, tm, id, perDevice, total) } func gatherBlockIOMetrics( stat *types.StatsJSON, acc telegraf.Accumulator, tags map[string]string, - now time.Time, + tm time.Time, id string, perDevice bool, total bool, @@ -576,7 +580,7 @@ func gatherBlockIOMetrics( if perDevice { iotags := copyTags(tags) iotags["device"] = device - acc.AddFields("docker_container_blkio", fields, iotags, now) + acc.AddFields("docker_container_blkio", fields, iotags, tm) } if total { for field, value := range fields { @@ -607,7 +611,7 @@ func gatherBlockIOMetrics( totalStatMap["container_id"] = id iotags := copyTags(tags) iotags["device"] = "total" - acc.AddFields("docker_container_blkio", totalStatMap, iotags, now) + acc.AddFields("docker_container_blkio", totalStatMap, iotags, tm) } }