From f946b73f1693cd966bc0de3fa3fc682eb116f6cb Mon Sep 17 00:00:00 2001 From: Gabe Kangas Date: Fri, 9 Dec 2022 09:49:17 -0800 Subject: [PATCH] Fix CPU usage collection panic with default zero value. Closes #2423 --- metrics/hardware.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/metrics/hardware.go b/metrics/hardware.go index 595944006..9a642fa73 100644 --- a/metrics/hardware.go +++ b/metrics/hardware.go @@ -24,7 +24,14 @@ func collectCPUUtilization() { return } - metricValue := TimestampedValue{time.Now(), v[0]} + // Default to zero but try to use the cumulative values of all the CPUs + // if values exist. + value := 0.0 + if len(v) > 0 { + value = v[0] + } + + metricValue := TimestampedValue{time.Now(), value} metrics.CPUUtilizations = append(metrics.CPUUtilizations, metricValue) cpuUsage.Set(metricValue.Value) }