diff --git a/exporter/kubernetes.go b/exporter/kubernetes.go index eb3d1d3..d890f89 100644 --- a/exporter/kubernetes.go +++ b/exporter/kubernetes.go @@ -288,6 +288,11 @@ func (m *Metrics) GetUsageCost() { name := pod.GetName() namespace := pod.GetNamespace() + if _, ok := m.Pods[namespace+"/"+name]; !ok { + log.Debugf("could not find pod %s/%s when iterating through metrics, ignoring", namespace, name) + continue + } + me := m.Pods[namespace+"/"+name] me.Usage.Cpu.Reset() me.Usage.Memory.Reset() diff --git a/exporter/metrics.go b/exporter/metrics.go index e5f9deb..a0b7a57 100644 --- a/exporter/metrics.go +++ b/exporter/metrics.go @@ -66,6 +66,9 @@ func (m *Metrics) Describe(ch chan<- *prometheus.Desc) { func (m *Metrics) Collect(ch chan<- prometheus.Metric) { m.podsMtx.Lock() + m.nodesMtx.Lock() + defer m.podsMtx.Unlock() + defer m.nodesMtx.Unlock() m.GetUsageCost() podLabels := []string{"pod", "namespace", "node", "type", "lifecycle"} @@ -141,7 +144,6 @@ func (m *Metrics) Collect(ch chan<- prometheus.Metric) { podLabelValues..., ) } - m.podsMtx.Unlock() nodeLabels := []string{"node", "region", "az", "type", "lifecycle"} if len(m.addNodeLabels) > 0 {