Skip to content

Commit

Permalink
fix(go.d/k8sstate): use static list of warning/terminated reasons (ne…
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyam8 authored Oct 28, 2024
1 parent 5204ee2 commit e465363
Show file tree
Hide file tree
Showing 6 changed files with 454 additions and 322 deletions.
90 changes: 36 additions & 54 deletions src/go/plugin/go.d/modules/k8s_state/charts.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,24 +681,42 @@ var (
{ID: "pod_%s_container_%s_state_terminated", Name: "terminated"},
},
}
containersStateWaitingChartTmpl = module.Chart{
IDSep: true,
ID: "pod_%s_container_%s.state_waiting_reason",
Title: "Container waiting state reason",
Units: "state",
Fam: "container waiting reason",
Ctx: "k8s_state.pod_container_waiting_state_reason",
Priority: prioPodContainerWaitingStateReason,
}
containersStateTerminatedChartTmpl = module.Chart{
IDSep: true,
ID: "pod_%s_container_%s.state_terminated_reason",
Title: "Container terminated state reason",
Units: "state",
Fam: "container terminated reason",
Ctx: "k8s_state.pod_container_terminated_state_reason",
Priority: prioPodContainerTerminatedStateReason,
}
containersStateWaitingChartTmpl = func() module.Chart {
chart := module.Chart{
IDSep: true,
ID: "pod_%s_container_%s.state_waiting_reason",
Title: "Container waiting state reason",
Units: "state",
Fam: "container waiting reason",
Ctx: "k8s_state.pod_container_waiting_state_reason",
Priority: prioPodContainerWaitingStateReason,
}
for _, v := range containerWaitingStateReasons {
chart.Dims = append(chart.Dims, &module.Dim{
ID: "pod_%s_container_%s_state_waiting_reason_" + v,
Name: v,
})
}
return chart
}()
containersStateTerminatedChartTmpl = func() module.Chart {
chart := module.Chart{
IDSep: true,
ID: "pod_%s_container_%s.state_terminated_reason",
Title: "Container terminated state reason",
Units: "state",
Fam: "container terminated reason",
Ctx: "k8s_state.pod_container_terminated_state_reason",
Priority: prioPodContainerTerminatedStateReason,
}
for _, v := range containerTerminatedStateReasons {
chart.Dims = append(chart.Dims, &module.Dim{
ID: "pod_%s_container_%s_state_terminated_reason_" + v,
Name: v,
})
}
return chart
}()
)

func (ks *KubeState) newContainerCharts(ps *podState, cs *containerState) *module.Charts {
Expand Down Expand Up @@ -728,42 +746,6 @@ func (ks *KubeState) addContainerCharts(ps *podState, cs *containerState) {
}
}

func (ks *KubeState) addContainerWaitingStateReasonToChart(ps *podState, cs *containerState, reason string) {
id := fmt.Sprintf(containersStateWaitingChartTmpl.ID, replaceDots(ps.id()), cs.name)
c := ks.Charts().Get(id)
if c == nil {
ks.Warningf("chart '%s' does not exist", id)
return
}
dim := &module.Dim{
ID: fmt.Sprintf("pod_%s_container_%s_state_waiting_reason_%s", ps.id(), cs.name, reason),
Name: reason,
}
if err := c.AddDim(dim); err != nil {
ks.Warning(err)
return
}
c.MarkNotCreated()
}

func (ks *KubeState) addContainerTerminatedStateReasonToChart(ps *podState, cs *containerState, reason string) {
id := fmt.Sprintf(containersStateTerminatedChartTmpl.ID, replaceDots(ps.id()), cs.name)
c := ks.Charts().Get(id)
if c == nil {
ks.Warningf("chart '%s' does not exist", id)
return
}
dim := &module.Dim{
ID: fmt.Sprintf("pod_%s_container_%s_state_terminated_reason_%s", ps.id(), cs.name, reason),
Name: reason,
}
if err := c.AddDim(dim); err != nil {
ks.Warning(err)
return
}
c.MarkNotCreated()
}

var discoveryStatusChart = module.Chart{
ID: "discovery_discoverers_state",
Title: "Running discoverers state",
Expand Down
50 changes: 40 additions & 10 deletions src/go/plugin/go.d/modules/k8s_state/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package k8s_state
import (
"errors"
"fmt"
"slices"
"strings"
"time"

Expand All @@ -15,6 +16,29 @@ import (

const precision = 1000

var (
containerWaitingStateReasons = []string{
"PodInitializing",
"ContainerCreating",
"CrashLoopBackOff",
"CreateContainerConfigError",
"ErrImagePull",
"ImagePullBackOff",
"CreateContainerError",
"InvalidImageName",
"Other",
}
containerTerminatedStateReasons = []string{
"OOMKilled",
"Completed",
"Error",
"ContainerCannotRun",
"DeadlineExceeded",
"Evicted",
"Other",
}
)

func (ks *KubeState) collect() (map[string]int64, error) {
if ks.discoverer == nil {
return nil, errors.New("nil discoverer")
Expand Down Expand Up @@ -165,19 +189,25 @@ func (ks *KubeState) collectPodsState(mx map[string]int64) {
mx[ppx+"state_terminated"] = boolToInt(cs.stateTerminated)
mx[ppx+"readiness"] = boolToInt(cs.ready)
mx[ppx+"restarts"] = cs.restarts
for _, r := range cs.stateWaitingReasons {
if r.new {
r.new = false
ks.addContainerWaitingStateReasonToChart(ps, cs, r.reason)

for _, v := range containerWaitingStateReasons {
mx[ppx+"state_waiting_reason_"+v] = 0
}
if v := cs.waitingReason; v != "" {
if !slices.Contains(containerWaitingStateReasons, cs.waitingReason) {
v = "Other"
}
mx[ppx+"state_waiting_reason_"+r.reason] = boolToInt(r.active)
mx[ppx+"state_waiting_reason_"+v] = 1
}

for _, v := range containerTerminatedStateReasons {
mx[ppx+"state_terminated_reason_"+v] = 0
}
for _, r := range cs.stateTerminatedReasons {
if r.new {
r.new = false
ks.addContainerTerminatedStateReasonToChart(ps, cs, r.reason)
if v := cs.terminatedReason; v != "" {
if !slices.Contains(containerTerminatedStateReasons, cs.terminatedReason) {
v = "Other"
}
mx[ppx+"state_terminated_reason_"+r.reason] = boolToInt(r.active)
mx[ppx+"state_terminated_reason_"+v] = 1
}
}
}
Expand Down
Loading

0 comments on commit e465363

Please sign in to comment.