From f6d15759286b589204fba737a02a63eaad387b5c Mon Sep 17 00:00:00 2001 From: ASAKURA Kazuki <32762324+Arthur1@users.noreply.github.com> Date: Wed, 23 Aug 2023 00:25:26 +0900 Subject: [PATCH] calcurate moderate values when cpu counters are reset --- mackerel-plugin-docker/lib/docker.go | 23 +++++++++++++++++------ mackerel-plugin-docker/lib/docker_test.go | 13 +++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/mackerel-plugin-docker/lib/docker.go b/mackerel-plugin-docker/lib/docker.go index 43e8573ad..4bf9d3899 100644 --- a/mackerel-plugin-docker/lib/docker.go +++ b/mackerel-plugin-docker/lib/docker.go @@ -270,19 +270,30 @@ func addCPUPercentageStats(stats *map[string]interface{}, lastStat map[string]in currentUserUsage, ok1 := (*stats)[internalCPUStatPrefix+name+".user"] prevUserUsage, ok2 := lastStat[internalCPUStatPrefix+name+".user"] if ok1 && ok2 { - userUsage := float64(currentUserUsage.(uint64) - uint64(prevUserUsage.(float64))) - if userUsage >= 0 { - (*stats)["docker.cpuacct_percentage."+name+".user"] = userUsage / hostUsage * 100.0 * float64(cpuNumsInt) + currentUserUsageUInt := currentUserUsage.(uint64) + prevUserUsageUInt := uint64(prevUserUsage.(float64)) + var userUsage float64 + if currentUserUsageUInt >= prevUserUsageUInt { + userUsage = float64(currentUserUsage.(uint64) - uint64(prevUserUsage.(float64))) + } else { + // counter has been reset + userUsage = float64(currentUserUsageUInt) } + (*stats)["docker.cpuacct_percentage."+name+".user"] = userUsage / hostUsage * 100.0 * float64(cpuNumsInt) } currentSystemUsage, ok1 := (*stats)[internalCPUStatPrefix+name+".system"] prevSystemUsage, ok2 := lastStat[internalCPUStatPrefix+name+".system"] if ok1 && ok2 { - systemUsage := float64(currentSystemUsage.(uint64) - uint64(prevSystemUsage.(float64))) - if systemUsage >= 0 { - (*stats)["docker.cpuacct_percentage."+name+".system"] = systemUsage / hostUsage * 100.0 * float64(cpuNumsInt) + currentSystemUsageUInt := currentSystemUsage.(uint64) + prevSystemUsageUInt := uint64(prevSystemUsage.(float64)) + var systemUsage float64 + if currentSystemUsageUInt >= prevSystemUsageUInt { + systemUsage = float64(currentSystemUsageUInt - prevSystemUsageUInt) + } else { + systemUsage = float64(currentSystemUsageUInt) } + (*stats)["docker.cpuacct_percentage."+name+".system"] = systemUsage / hostUsage * 100.0 * float64(cpuNumsInt) } } } diff --git a/mackerel-plugin-docker/lib/docker_test.go b/mackerel-plugin-docker/lib/docker_test.go index 89c19de92..e55fdc655 100644 --- a/mackerel-plugin-docker/lib/docker_test.go +++ b/mackerel-plugin-docker/lib/docker_test.go @@ -99,6 +99,10 @@ func TestAddCPUPercentageStats(t *testing.T) { "docker._internal.cpuacct.containerD.host": uint64(100000), "docker._internal.cpuacct.containerD.user": uint64(3000), "docker._internal.cpuacct.containerD.system": uint64(2000), + "docker._internal.cpuacct.containerF.user": uint64(3000), // it has been reset + "docker._internal.cpuacct.containerF.system": uint64(1000), // it has been reset + "docker._internal.cpuacct.containerF.host": uint64(100000100000), + "docker._internal.cpuacct.containerF.onlineCPUs": int(2), } oldStats := map[string]interface{}{ "docker._internal.cpuacct.containerA.host": float64(90000), @@ -111,6 +115,9 @@ func TestAddCPUPercentageStats(t *testing.T) { "docker._internal.cpuacct.containerE.host": float64(100000), "docker._internal.cpuacct.containerE.user": float64(3000), "docker._internal.cpuacct.containerE.system": float64(2000), + "docker._internal.cpuacct.containerF.user": float64(40000000000), + "docker._internal.cpuacct.containerF.system": float64(20000000000), + "docker._internal.cpuacct.containerF.host": float64(100000000000), } addCPUPercentageStats(&stats, oldStats) @@ -135,4 +142,10 @@ func TestAddCPUPercentageStats(t *testing.T) { if _, ok := stats["docker.cpuacct_percentage.containerE.user"]; ok { t.Errorf("docker.cpuacct_percentage.containerE.user should not be calculated") } + + if stat, ok := stats["docker.cpuacct_percentage.containerF.user"]; !ok { + t.Errorf("docker.cpuacct_percentage.containerF.user should be calculated") + } else if stat != float64(6.0) { + t.Errorf("docker.cpuacct_percentage.containerF.user should be %f, but %f", float64(6.0), stat) + } }