Fix CPU usage collection panic with default zero value. Closes #2423

This commit is contained in:
Gabe Kangas 2022-12-09 09:49:17 -08:00
parent 77d3f59ca6
commit f946b73f16
No known key found for this signature in database
GPG key ID: 4345B2060657F330

View file

@ -24,7 +24,14 @@ func collectCPUUtilization() {
return 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) metrics.CPUUtilizations = append(metrics.CPUUtilizations, metricValue)
cpuUsage.Set(metricValue.Value) cpuUsage.Set(metricValue.Value)
} }