From 403a15ab3e18e455022010ffcea20a66afa10474 Mon Sep 17 00:00:00 2001 From: kaiyuechen Date: Tue, 13 Dec 2022 21:47:49 +0800 Subject: [PATCH] fix crash when memory metric can't be collected --- pkg/ensurance/collector/cadvisor/cadvisor_linux.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/ensurance/collector/cadvisor/cadvisor_linux.go b/pkg/ensurance/collector/cadvisor/cadvisor_linux.go index fef292b6c..7bfce01c4 100644 --- a/pkg/ensurance/collector/cadvisor/cadvisor_linux.go +++ b/pkg/ensurance/collector/cadvisor/cadvisor_linux.go @@ -164,8 +164,10 @@ func (c *CadvisorCollector) Collect() (map[string][]common.TimeSeries, error) { hasExtRes := hasExtCpuRes || hasExtMemRes var containerLabels = GetContainerLabels(pod, containerId, containerName, hasExtRes) - addSampleToStateMap(types.MetricNameContainerMemTotalUsage, composeSample(containerLabels, float64(v.Stats[0].Memory.WorkingSet), now), stateMap) - klog.V(6).Infof("Pod: %s, containerName: %s, key %s, container_mem_total_usage %#v", klog.KObj(pod), containerName, key, float64(v.Stats[0].Memory.WorkingSet)) + if v.Stats[0].Memory != nil { + addSampleToStateMap(types.MetricNameContainerMemTotalUsage, composeSample(containerLabels, float64(v.Stats[0].Memory.WorkingSet), now), stateMap) + klog.V(6).Infof("Pod: %s, containerName: %s, key %s, container_mem_total_usage %#v", klog.KObj(pod), containerName, key, float64(v.Stats[0].Memory.WorkingSet)) + } if state, ok := c.latestContainersStates[key]; ok { klog.V(6).Infof("For key %s, LatestContainersStates exist", key)