Skip to content

Commit 77db769

Browse files
authored
fix(host): clear previous usage metrics when any container stopped of pod (#23706)
1 parent 982b0ee commit 77db769

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

pkg/hostman/hostmetrics/container_metrics.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,23 @@ func (m *SGuestMonitor) getCadvisorDiskIoMetrics(cur stats.DiskIoStats, prev map
697697
return ret
698698
}
699699

700+
func isPodContainerStopped(prevUsage *GuestMetrics, stat *stats.PodStats) bool {
701+
hasPrevUsage := prevUsage != nil && prevUsage.PodMetrics != nil
702+
if !hasPrevUsage {
703+
return false
704+
}
705+
curTime := stat.CPU.Time.Time
706+
podCpu := &PodCpuMetric{
707+
PodMetricMeta: NewPodMetricMeta(curTime),
708+
CpuUsageSecondsTotal: float64(*stat.CPU.UsageCoreNanoSeconds) / float64(time.Second),
709+
}
710+
pmPodCpu := prevUsage.PodMetrics.PodCpu
711+
if podCpu.CpuUsageSecondsTotal < pmPodCpu.CpuUsageSecondsTotal {
712+
return true
713+
}
714+
return false
715+
}
716+
700717
func (m *SGuestMonitor) PodMetrics(prevUsage *GuestMetrics) *PodMetrics {
701718
stat := m.podStat
702719
curTime := stat.CPU.Time.Time

pkg/hostman/hostmetrics/hostmetrics.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,10 @@ func (s *SGuestMonitorCollector) collectGmReport(
506506
if !gm.HasPodMetrics() {
507507
return s.collectGuestMetrics(gm, prevUsage)
508508
} else {
509+
if isPodContainerStopped(prevUsage, gm.podStat) {
510+
log.Infof("pod %s(%s) has container(s) stopped, clear previous usage", gm.Name, gm.Id)
511+
prevUsage = new(GuestMetrics)
512+
}
509513
return s.collectPodMetrics(gm, prevUsage)
510514
}
511515
}

0 commit comments

Comments
 (0)