From b9ff57c59e3705eb963d39001192ab3a0ecd2d1e Mon Sep 17 00:00:00 2001 From: gary rong Date: Thu, 19 Nov 2020 04:50:11 +0800 Subject: [PATCH] metrics: fix the panic for reading empty cpu stats (#21864) --- metrics/cpu_enabled.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/metrics/cpu_enabled.go b/metrics/cpu_enabled.go index 52a3c2e96..02192928b 100644 --- a/metrics/cpu_enabled.go +++ b/metrics/cpu_enabled.go @@ -31,6 +31,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)