Only report cpu usage for online cpus in docker input (#3035)
This commit is contained in:
committed by
Daniel Nelson
parent
4ee74ff54b
commit
681d20083a
@@ -446,7 +446,16 @@ func gatherContainerStats(
|
||||
cputags["cpu"] = "cpu-total"
|
||||
acc.AddFields("docker_container_cpu", cpufields, cputags, now)
|
||||
|
||||
for i, percpu := range stat.CPUStats.CPUUsage.PercpuUsage {
|
||||
// If we have OnlineCPUs field, then use it to restrict stats gathering to only Online CPUs
|
||||
// (https://github.com/moby/moby/commit/115f91d7575d6de6c7781a96a082f144fd17e400)
|
||||
var percpuusage []uint64
|
||||
if stat.CPUStats.OnlineCPUs > 0 {
|
||||
percpuusage = stat.CPUStats.CPUUsage.PercpuUsage[:stat.CPUStats.OnlineCPUs]
|
||||
} else {
|
||||
percpuusage = stat.CPUStats.CPUUsage.PercpuUsage
|
||||
}
|
||||
|
||||
for i, percpu := range percpuusage {
|
||||
percputags := copyTags(tags)
|
||||
percputags["cpu"] = fmt.Sprintf("cpu%d", i)
|
||||
fields := map[string]interface{}{
|
||||
@@ -527,7 +536,11 @@ func calculateCPUPercent(stat *types.StatsJSON) float64 {
|
||||
systemDelta := float64(stat.CPUStats.SystemUsage) - float64(stat.PreCPUStats.SystemUsage)
|
||||
|
||||
if systemDelta > 0.0 && cpuDelta > 0.0 {
|
||||
cpuPercent = (cpuDelta / systemDelta) * float64(len(stat.CPUStats.CPUUsage.PercpuUsage)) * 100.0
|
||||
if stat.CPUStats.OnlineCPUs > 0 {
|
||||
cpuPercent = (cpuDelta / systemDelta) * float64(stat.CPUStats.OnlineCPUs) * 100.0
|
||||
} else {
|
||||
cpuPercent = (cpuDelta / systemDelta) * float64(len(stat.CPUStats.CPUUsage.PercpuUsage)) * 100.0
|
||||
}
|
||||
}
|
||||
return cpuPercent
|
||||
}
|
||||
|
||||
@@ -141,14 +141,29 @@ func TestDockerGatherContainerStats(t *testing.T) {
|
||||
"container_id": "123456789",
|
||||
}
|
||||
acc.AssertContainsTaggedFields(t, "docker_container_cpu", cpu1fields, cputags)
|
||||
|
||||
// Those tagged filed should not be present because of offline CPUs
|
||||
cputags["cpu"] = "cpu2"
|
||||
cpu2fields := map[string]interface{}{
|
||||
"usage_total": uint64(0),
|
||||
"container_id": "123456789",
|
||||
}
|
||||
acc.AssertDoesNotContainsTaggedFields(t, "docker_container_cpu", cpu2fields, cputags)
|
||||
|
||||
cputags["cpu"] = "cpu3"
|
||||
cpu3fields := map[string]interface{}{
|
||||
"usage_total": uint64(0),
|
||||
"container_id": "123456789",
|
||||
}
|
||||
acc.AssertDoesNotContainsTaggedFields(t, "docker_container_cpu", cpu3fields, cputags)
|
||||
}
|
||||
|
||||
func testStats() *types.StatsJSON {
|
||||
stats := &types.StatsJSON{}
|
||||
stats.Read = time.Now()
|
||||
stats.Networks = make(map[string]types.NetworkStats)
|
||||
|
||||
stats.CPUStats.CPUUsage.PercpuUsage = []uint64{1, 1002}
|
||||
stats.CPUStats.OnlineCPUs = 2
|
||||
stats.CPUStats.CPUUsage.PercpuUsage = []uint64{1, 1002, 0, 0}
|
||||
stats.CPUStats.CPUUsage.UsageInUsermode = 100
|
||||
stats.CPUStats.CPUUsage.TotalUsage = 500
|
||||
stats.CPUStats.CPUUsage.UsageInKernelmode = 200
|
||||
|
||||
Reference in New Issue
Block a user