Skip to content

Commit

Permalink
feat: More checks to avoid crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreZiviani committed Jul 3, 2023
1 parent 7f348e5 commit 8ebfa1a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
5 changes: 5 additions & 0 deletions exporter/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion exporter/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 8ebfa1a

Please sign in to comment.