metrics: fix the panic for reading empty cpu stats (#21864)

This commit is contained in:
Daniel Liu 2024-12-13 14:00:12 +08:00
parent bf4b42a551
commit d4f1b8a6dd

View file

@ -32,6 +32,10 @@ func ReadCPUStats(stats *CPUStats) {
log.Error("Could not read cpu stats", "err", err)
return
}
if len(timeStats) == 0 {
log.Error("Empty cpu stats")
return
}
// requesting all cpu times will always return an array with only one time stats entry
timeStat := timeStats[0]
stats.GlobalTime = int64((timeStat.User + timeStat.Nice + timeStat.System) * cpu.ClocksPerSec)