Use current time if container read time is zero value (#3437)

(cherry picked from commit cbbdf1043b)
This commit is contained in:
Daniel Nelson 2017-11-07 11:41:53 -08:00 committed by Daniel Nelson
parent 4cedae9d2c
commit 600f9fa067
No known key found for this signature in database
GPG Key ID: CAAD59C9444F6155
1 changed files with 14 additions and 10 deletions

View File

@ -339,7 +339,11 @@ func gatherContainerStats(
total bool, total bool,
daemonOSType string, daemonOSType string,
) { ) {
now := stat.Read tm := stat.Read
if tm.Before(time.Unix(0, 0)) {
tm = time.Now()
}
memfields := map[string]interface{}{ memfields := map[string]interface{}{
"container_id": id, "container_id": id,
@ -399,7 +403,7 @@ func gatherContainerStats(
memfields["private_working_set"] = stat.MemoryStats.PrivateWorkingSet 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{}{ cpufields := map[string]interface{}{
"usage_total": stat.CPUStats.CPUUsage.TotalUsage, "usage_total": stat.CPUStats.CPUUsage.TotalUsage,
@ -424,7 +428,7 @@ func gatherContainerStats(
cputags := copyTags(tags) cputags := copyTags(tags)
cputags["cpu"] = "cpu-total" 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 // If we have OnlineCPUs field, then use it to restrict stats gathering to only Online CPUs
// (https://github.com/moby/moby/commit/115f91d7575d6de6c7781a96a082f144fd17e400) // (https://github.com/moby/moby/commit/115f91d7575d6de6c7781a96a082f144fd17e400)
@ -442,7 +446,7 @@ func gatherContainerStats(
"usage_total": percpu, "usage_total": percpu,
"container_id": id, "container_id": id,
} }
acc.AddFields("docker_container_cpu", fields, percputags, now) acc.AddFields("docker_container_cpu", fields, percputags, tm)
} }
totalNetworkStatMap := make(map[string]interface{}) totalNetworkStatMap := make(map[string]interface{})
@ -462,7 +466,7 @@ func gatherContainerStats(
if perDevice { if perDevice {
nettags := copyTags(tags) nettags := copyTags(tags)
nettags["network"] = network nettags["network"] = network
acc.AddFields("docker_container_net", netfields, nettags, now) acc.AddFields("docker_container_net", netfields, nettags, tm)
} }
if total { if total {
for field, value := range netfields { for field, value := range netfields {
@ -495,17 +499,17 @@ func gatherContainerStats(
nettags := copyTags(tags) nettags := copyTags(tags)
nettags["network"] = "total" nettags["network"] = "total"
totalNetworkStatMap["container_id"] = id 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( func gatherBlockIOMetrics(
stat *types.StatsJSON, stat *types.StatsJSON,
acc telegraf.Accumulator, acc telegraf.Accumulator,
tags map[string]string, tags map[string]string,
now time.Time, tm time.Time,
id string, id string,
perDevice bool, perDevice bool,
total bool, total bool,
@ -576,7 +580,7 @@ func gatherBlockIOMetrics(
if perDevice { if perDevice {
iotags := copyTags(tags) iotags := copyTags(tags)
iotags["device"] = device iotags["device"] = device
acc.AddFields("docker_container_blkio", fields, iotags, now) acc.AddFields("docker_container_blkio", fields, iotags, tm)
} }
if total { if total {
for field, value := range fields { for field, value := range fields {
@ -607,7 +611,7 @@ func gatherBlockIOMetrics(
totalStatMap["container_id"] = id totalStatMap["container_id"] = id
iotags := copyTags(tags) iotags := copyTags(tags)
iotags["device"] = "total" iotags["device"] = "total"
acc.AddFields("docker_container_blkio", totalStatMap, iotags, now) acc.AddFields("docker_container_blkio", totalStatMap, iotags, tm)
} }
} }