fix(host): clear previous usage metrics when any container stopped of pod (#23706)

This commit is contained in:
Zexi Li
2025-11-06 20:56:30 +08:00
committed by GitHub
parent 982b0eec78
commit 77db769c8b
2 changed files with 21 additions and 0 deletions
@@ -697,6 +697,23 @@ func (m *SGuestMonitor) getCadvisorDiskIoMetrics(cur stats.DiskIoStats, prev map
return ret
}
func isPodContainerStopped(prevUsage *GuestMetrics, stat *stats.PodStats) bool {
hasPrevUsage := prevUsage != nil && prevUsage.PodMetrics != nil
if !hasPrevUsage {
return false
}
curTime := stat.CPU.Time.Time
podCpu := &PodCpuMetric{
PodMetricMeta: NewPodMetricMeta(curTime),
CpuUsageSecondsTotal: float64(*stat.CPU.UsageCoreNanoSeconds) / float64(time.Second),
}
pmPodCpu := prevUsage.PodMetrics.PodCpu
if podCpu.CpuUsageSecondsTotal < pmPodCpu.CpuUsageSecondsTotal {
return true
}
return false
}
func (m *SGuestMonitor) PodMetrics(prevUsage *GuestMetrics) *PodMetrics {
stat := m.podStat
curTime := stat.CPU.Time.Time
+4
View File
@@ -506,6 +506,10 @@ func (s *SGuestMonitorCollector) collectGmReport(
if !gm.HasPodMetrics() {
return s.collectGuestMetrics(gm, prevUsage)
} else {
if isPodContainerStopped(prevUsage, gm.podStat) {
log.Infof("pod %s(%s) has container(s) stopped, clear previous usage", gm.Name, gm.Id)
prevUsage = new(GuestMetrics)
}
return s.collectPodMetrics(gm, prevUsage)
}
}