Skip to content

Commit

Permalink
minor refactor so units work
Browse files Browse the repository at this point in the history
  • Loading branch information
chadpatel committed Oct 10, 2023
1 parent ba30fbe commit a512dfa
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 87 deletions.
107 changes: 54 additions & 53 deletions internal/aws/containerinsight/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,38 +76,39 @@ const (
FSInodesfree = "filesystem_inodes_free"
FSUtilization = "filesystem_utilization"

StatusConditionReady = "status_condition_ready"
StatusConditionDiskPressure = "status_condition_disk_pressure"
StatusConditionMemoryPressure = "status_condition_memory_pressure"
StatusConditionPIDPressure = "status_condition_pid_pressure"
StatusConditionNetworkUnavailable = "status_condition_network_unavailable"
StatusConditionUnknown = "status_condition_unknown"
StatusCapacityPods = "status_capacity_pods"
StatusAllocatablePods = "status_allocatable_pods"
StatusNumberAvailable = "status_number_available"
StatusNumberUnavailable = "status_number_unavailable"
StatusDesiredNumberScheduled = "status_desired_number_scheduled"
StatusCurrentNumberScheduled = "status_current_number_scheduled"
StatusReplicasAvailable = "status_replicas_available"
StatusReplicasUnavailable = "status_replicas_unavailable"
SpecReplicas = "spec_replicas"
StatusRunning = "status_running"
StatusTerminated = "status_terminated"
StatusWaiting = "status_waiting"
StatusWaitingReasonCrashLoopBackOff = "status_waiting_reason_crash_loop_back_off"
StatusWaitingReasonImagePullError = "status_waiting_reason_image_pull_error"
StatusWaitingReasonStartError = "status_waiting_reason_start_error"
StatusWaitingReasonCreateContainerError = "status_waiting_reason_create_container_error"
StatusWaitingReasonCreateContainerConfigError = "status_waiting_reason_create_container_config_error"
StatusTerminatedReasonOOMKilled = "status_terminated_reason_oom_killed"
StatusPending = "status_pending"
StatusSucceeded = "status_succeeded"
StatusFailed = "status_failed"
StatusUnknown = "status_unknown"
StatusReady = "status_ready"
StatusScheduled = "status_scheduled"
ReplicasDesired = "replicas_desired"
ReplicasReady = "replicas_ready"
StatusConditionReady = "status_condition_ready"
StatusConditionDiskPressure = "status_condition_disk_pressure"
StatusConditionMemoryPressure = "status_condition_memory_pressure"
StatusConditionPIDPressure = "status_condition_pid_pressure"
StatusConditionNetworkUnavailable = "status_condition_network_unavailable"
StatusConditionUnknown = "status_condition_unknown"
StatusCapacityPods = "status_capacity_pods"
StatusAllocatablePods = "status_allocatable_pods"
StatusNumberAvailable = "status_number_available"
StatusNumberUnavailable = "status_number_unavailable"
StatusDesiredNumberScheduled = "status_desired_number_scheduled"
StatusCurrentNumberScheduled = "status_current_number_scheduled"
StatusReplicasAvailable = "status_replicas_available"
StatusReplicasUnavailable = "status_replicas_unavailable"
SpecReplicas = "spec_replicas"
StatusContainerRunning = "container_status_running"
StatusContainerTerminated = "container_status_terminated"
StatusContainerWaiting = "container_status_waiting"
StatusContainerWaitingReasonCrashLoopBackOff = "container_status_waiting_reason_crash_loop_back_off"
StatusContainerWaitingReasonImagePullError = "container_status_waiting_reason_image_pull_error"
StatusContainerWaitingReasonStartError = "container_status_waiting_reason_start_error"
StatusContainerWaitingReasonCreateContainerError = "container_status_waiting_reason_create_container_error"
StatusContainerWaitingReasonCreateContainerConfigError = "container_status_waiting_reason_create_container_config_error"
StatusContainerTerminatedReasonOOMKilled = "container_status_terminated_reason_oom_killed"
StatusRunning = "status_running"
StatusPending = "status_pending"
StatusSucceeded = "status_succeeded"
StatusFailed = "status_failed"
StatusUnknown = "status_unknown"
StatusReady = "status_ready"
StatusScheduled = "status_scheduled"
ReplicasDesired = "replicas_desired"
ReplicasReady = "replicas_ready"

RunningPodCount = "number_of_running_pods"
RunningContainerCount = "number_of_running_containers"
Expand Down Expand Up @@ -163,13 +164,13 @@ const (
)

var WaitingReasonLookup = map[string]string{
"CrashLoopBackOff": StatusWaitingReasonCrashLoopBackOff,
"ErrImagePull": StatusWaitingReasonImagePullError,
"ImagePullBackOff": StatusWaitingReasonImagePullError,
"InvalidImageName": StatusWaitingReasonImagePullError,
"CreateContainerError": StatusWaitingReasonCreateContainerError,
"CreateContainerConfigError": StatusWaitingReasonCreateContainerConfigError,
"StartError": StatusWaitingReasonStartError,
"CrashLoopBackOff": StatusContainerWaitingReasonCrashLoopBackOff,
"ErrImagePull": StatusContainerWaitingReasonImagePullError,
"ImagePullBackOff": StatusContainerWaitingReasonImagePullError,
"InvalidImageName": StatusContainerWaitingReasonImagePullError,
"CreateContainerError": StatusContainerWaitingReasonCreateContainerError,
"CreateContainerConfigError": StatusContainerWaitingReasonCreateContainerConfigError,
"StartError": StatusContainerWaitingReasonStartError,
}

var metricToUnitMap map[string]string
Expand Down Expand Up @@ -261,20 +262,20 @@ func init() {
ReplicasReady: UnitCount,

// kube-state-metrics equivalents
StatusRunning: UnitCount,
StatusTerminated: UnitCount,
StatusWaiting: UnitCount,
StatusWaitingReasonCrashLoopBackOff: UnitCount,
StatusWaitingReasonImagePullError: UnitCount,
StatusWaitingReasonStartError: UnitCount,
StatusWaitingReasonCreateContainerConfigError: UnitCount,
StatusWaitingReasonCreateContainerError: UnitCount,
StatusFailed: UnitCount,
StatusPending: UnitCount,
StatusSucceeded: UnitCount,
StatusUnknown: UnitCount,
StatusReady: UnitCount,
StatusScheduled: UnitCount,
StatusContainerRunning: UnitCount,
StatusContainerTerminated: UnitCount,
StatusContainerWaiting: UnitCount,
StatusContainerWaitingReasonCrashLoopBackOff: UnitCount,
StatusContainerWaitingReasonImagePullError: UnitCount,
StatusContainerWaitingReasonStartError: UnitCount,
StatusContainerWaitingReasonCreateContainerConfigError: UnitCount,
StatusContainerWaitingReasonCreateContainerError: UnitCount,
StatusFailed: UnitCount,
StatusPending: UnitCount,
StatusSucceeded: UnitCount,
StatusUnknown: UnitCount,
StatusReady: UnitCount,
StatusScheduled: UnitCount,

// cluster metrics
NodeCount: UnitCount,
Expand Down
20 changes: 10 additions & 10 deletions receiver/awscontainerinsightreceiver/internal/stores/podstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,16 +595,16 @@ func (p *PodStore) addPodConditionMetrics(metric CIMetric, pod *corev1.Pod) {

func (p *PodStore) addPodContainerStatusMetrics(metric CIMetric, pod *corev1.Pod) {
possibleStatuses := map[string]int{
ci.StatusRunning: 0,
ci.StatusWaiting: 0,
ci.StatusTerminated: 0,
ci.StatusContainerRunning: 0,
ci.StatusContainerWaiting: 0,
ci.StatusContainerTerminated: 0,
}
for _, containerStatus := range pod.Status.ContainerStatuses {
switch {
case containerStatus.State.Running != nil:
possibleStatuses[ci.StatusRunning]++
possibleStatuses[ci.StatusContainerRunning]++
case containerStatus.State.Waiting != nil:
possibleStatuses[ci.StatusWaiting]++
possibleStatuses[ci.StatusContainerWaiting]++
reason := containerStatus.State.Waiting.Reason
if reason != "" {
if val, ok := ci.WaitingReasonLookup[reason]; ok {
Expand All @@ -616,26 +616,26 @@ func (p *PodStore) addPodContainerStatusMetrics(metric CIMetric, pod *corev1.Pod
}
}
case containerStatus.State.Terminated != nil:
possibleStatuses[ci.StatusTerminated]++
possibleStatuses[ci.StatusContainerTerminated]++
if containerStatus.State.Terminated.Reason != "" {
metric.AddTag(ci.ContainerStatusReason, containerStatus.State.Terminated.Reason)
}
}

if containerStatus.LastTerminationState.Terminated != nil && containerStatus.LastTerminationState.Terminated.Reason != "" {
if strings.Contains(containerStatus.LastTerminationState.Terminated.Reason, "OOMKilled") {
if _, foundStatus := possibleStatuses[ci.StatusTerminatedReasonOOMKilled]; foundStatus {
possibleStatuses[ci.StatusTerminatedReasonOOMKilled]++
if _, foundStatus := possibleStatuses[ci.StatusContainerTerminatedReasonOOMKilled]; foundStatus {
possibleStatuses[ci.StatusContainerTerminatedReasonOOMKilled]++
} else {
possibleStatuses[ci.StatusTerminatedReasonOOMKilled] = 1
possibleStatuses[ci.StatusContainerTerminatedReasonOOMKilled] = 1
}
}
}
}

for name, val := range possibleStatuses {
// desired prefix: pod_container_
metric.AddField(ci.MetricName(ci.TypePod, "container_"+name), val)
metric.AddField(ci.MetricName(ci.TypePod, name), val)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ func TestPodStore_addStatus_enhanced_metrics(t *testing.T) {
assert.Equal(t, "Running", metric.GetTag(ci.PodStatus))
val = metric.GetField(ci.ContainerRestartCount)
assert.Nil(t, val)
val = metric.GetField(ci.MetricName(ci.TypePod, "container_"+ci.StatusRunning))
val = metric.GetField(ci.MetricName(ci.TypePod, ci.StatusContainerRunning))
assert.NotNil(t, val)
assert.Equal(t, 2, val)

Expand Down Expand Up @@ -708,8 +708,8 @@ func TestPodStore_addStatus_enhanced_metrics(t *testing.T) {
metric = generateMetric(fields, tags)

podStore.addStatus(metric, pod)
assert.Equal(t, 2, metric.GetField(ci.MetricName(ci.TypePod, "container_"+ci.StatusTerminated)))
assert.Equal(t, 2, metric.GetField(ci.MetricName(ci.TypePod, "container_"+ci.StatusTerminatedReasonOOMKilled)))
assert.Equal(t, 2, metric.GetField(ci.MetricName(ci.TypePod, ci.StatusContainerTerminated)))
assert.Equal(t, 2, metric.GetField(ci.MetricName(ci.TypePod, ci.StatusContainerTerminatedReasonOOMKilled)))

pod.Status.ContainerStatuses[0].LastTerminationState.Terminated = nil
pod.Status.ContainerStatuses[0].State.Waiting = &corev1.ContainerStateWaiting{Reason: "CrashLoopBackOff"}
Expand All @@ -721,14 +721,14 @@ func TestPodStore_addStatus_enhanced_metrics(t *testing.T) {

podStore.addStatus(metric, pod)
//assert.Equal(t, "Waiting", metric.GetTag(ci.ContainerStatus))
assert.Equal(t, 2, metric.GetField(ci.MetricName(ci.TypePod, "container_"+ci.StatusWaiting)))
assert.Equal(t, 2, metric.GetField(ci.MetricName(ci.TypePod, "container_"+ci.StatusWaitingReasonCrashLoopBackOff)))
assert.Equal(t, 2, metric.GetField(ci.MetricName(ci.TypePod, ci.StatusContainerWaiting)))
assert.Equal(t, 2, metric.GetField(ci.MetricName(ci.TypePod, ci.StatusContainerWaitingReasonCrashLoopBackOff)))
// sparse metrics
assert.Nil(t, metric.GetField(ci.MetricName(ci.TypePod, "container_"+ci.StatusWaitingReasonImagePullError)))
assert.Nil(t, metric.GetField(ci.MetricName(ci.TypePod, "container_"+ci.StatusTerminatedReasonOOMKilled)))
assert.Nil(t, metric.GetField(ci.MetricName(ci.TypePod, "container_"+ci.StatusWaitingReasonStartError)))
assert.Nil(t, metric.GetField(ci.MetricName(ci.TypePod, "container_"+ci.StatusWaitingReasonCreateContainerError)))
assert.Nil(t, metric.GetField(ci.MetricName(ci.TypePod, "container_"+ci.StatusWaitingReasonCreateContainerConfigError)))
assert.Nil(t, metric.GetField(ci.MetricName(ci.TypePod, ci.StatusContainerWaitingReasonImagePullError)))
assert.Nil(t, metric.GetField(ci.MetricName(ci.TypePod, ci.StatusContainerTerminatedReasonOOMKilled)))
assert.Nil(t, metric.GetField(ci.MetricName(ci.TypePod, ci.StatusContainerWaitingReasonStartError)))
assert.Nil(t, metric.GetField(ci.MetricName(ci.TypePod, ci.StatusContainerWaitingReasonCreateContainerError)))
assert.Nil(t, metric.GetField(ci.MetricName(ci.TypePod, ci.StatusContainerWaitingReasonCreateContainerConfigError)))

pod.Status.ContainerStatuses[0].State.Waiting = &corev1.ContainerStateWaiting{Reason: "ImagePullBackOff"}
pod.Status.ContainerStatuses[1].State.Waiting = &corev1.ContainerStateWaiting{Reason: "StartError"}
Expand All @@ -738,35 +738,35 @@ func TestPodStore_addStatus_enhanced_metrics(t *testing.T) {

podStore.addStatus(metric, pod)
assert.Equal(t, "Succeeded", metric.GetTag(ci.PodStatus))
assert.Equal(t, 2, metric.GetField(ci.MetricName(ci.TypePod, "container_"+ci.StatusWaiting)))
assert.Equal(t, 1, metric.GetField(ci.MetricName(ci.TypePod, "container_"+ci.StatusWaitingReasonImagePullError)))
assert.Equal(t, 1, metric.GetField(ci.MetricName(ci.TypePod, "container_"+ci.StatusWaitingReasonStartError)))
assert.Equal(t, 2, metric.GetField(ci.MetricName(ci.TypePod, ci.StatusContainerWaiting)))
assert.Equal(t, 1, metric.GetField(ci.MetricName(ci.TypePod, ci.StatusContainerWaitingReasonImagePullError)))
assert.Equal(t, 1, metric.GetField(ci.MetricName(ci.TypePod, ci.StatusContainerWaitingReasonStartError)))

pod.Status.ContainerStatuses[0].State.Waiting = &corev1.ContainerStateWaiting{Reason: "ErrImagePull"}
metric = generateMetric(fields, tags)
podStore.addStatus(metric, pod)
assert.Equal(t, 1, metric.GetField(ci.MetricName(ci.TypePod, "container_"+ci.StatusWaitingReasonImagePullError)))
assert.Equal(t, 1, metric.GetField(ci.MetricName(ci.TypePod, ci.StatusContainerWaitingReasonImagePullError)))

pod.Status.ContainerStatuses[0].State.Waiting = &corev1.ContainerStateWaiting{Reason: "InvalidImageName"}
metric = generateMetric(fields, tags)
podStore.addStatus(metric, pod)
assert.Equal(t, 1, metric.GetField(ci.MetricName(ci.TypePod, "container_"+ci.StatusWaitingReasonImagePullError)))
assert.Equal(t, 1, metric.GetField(ci.MetricName(ci.TypePod, ci.StatusContainerWaitingReasonImagePullError)))

pod.Status.ContainerStatuses[0].State.Waiting = &corev1.ContainerStateWaiting{Reason: "CreateContainerError"}
metric = generateMetric(fields, tags)
podStore.addStatus(metric, pod)
assert.Equal(t, 1, metric.GetField(ci.MetricName(ci.TypePod, "container_"+ci.StatusWaitingReasonCreateContainerError)))
assert.Equal(t, 1, metric.GetField(ci.MetricName(ci.TypePod, ci.StatusContainerWaitingReasonCreateContainerError)))

pod.Status.ContainerStatuses[0].State.Waiting = &corev1.ContainerStateWaiting{Reason: "CreateContainerConfigError"}
metric = generateMetric(fields, tags)
podStore.addStatus(metric, pod)
assert.Equal(t, 1, metric.GetField(ci.MetricName(ci.TypePod, "container_"+ci.StatusWaitingReasonCreateContainerConfigError)))
assert.Equal(t, 1, metric.GetField(ci.MetricName(ci.TypePod, ci.StatusContainerWaitingReasonCreateContainerConfigError)))

pod.Status.ContainerStatuses[0].State.Waiting = &corev1.ContainerStateWaiting{Reason: "StartError"}
pod.Status.ContainerStatuses[1].State.Waiting = nil
metric = generateMetric(fields, tags)
podStore.addStatus(metric, pod)
assert.Equal(t, 1, metric.GetField(ci.MetricName(ci.TypePod, "container_"+ci.StatusWaitingReasonStartError)))
assert.Equal(t, 1, metric.GetField(ci.MetricName(ci.TypePod, ci.StatusContainerWaitingReasonStartError)))

// test delta of restartCount
pod.Status.ContainerStatuses[0].RestartCount = 3
Expand Down Expand Up @@ -803,7 +803,7 @@ func TestPodStore_addStatus_without_enhanced_metrics(t *testing.T) {
assert.Equal(t, "Running", metric.GetTag(ci.ContainerStatus))
val = metric.GetField(ci.ContainerRestartCount)
assert.Nil(t, val)
assert.False(t, metric.HasField(ci.MetricName(ci.TypeContainer, ci.StatusRunning)))
assert.False(t, metric.HasField(ci.MetricName(ci.TypeContainer, ci.StatusContainerRunning)))

pod.Status.ContainerStatuses[0].State.Running = nil
pod.Status.ContainerStatuses[0].State.Terminated = &corev1.ContainerStateTerminated{}
Expand All @@ -825,7 +825,7 @@ func TestPodStore_addStatus_without_enhanced_metrics(t *testing.T) {
assert.Equal(t, "Terminated", metric.GetTag(ci.ContainerStatus))
assert.Equal(t, "OOMKilled", metric.GetTag(ci.ContainerLastTerminationReason))
assert.Equal(t, int(1), metric.GetField(ci.ContainerRestartCount).(int))
assert.False(t, metric.HasField(ci.MetricName(ci.TypeContainer, ci.StatusTerminated)))
assert.False(t, metric.HasField(ci.MetricName(ci.TypeContainer, ci.StatusContainerTerminated)))

pod.Status.ContainerStatuses[0].State.Terminated = nil
pod.Status.ContainerStatuses[0].State.Waiting = &corev1.ContainerStateWaiting{Reason: "CrashLoopBackOff"}
Expand All @@ -835,8 +835,8 @@ func TestPodStore_addStatus_without_enhanced_metrics(t *testing.T) {

podStore.addStatus(metric, pod)
assert.Equal(t, "Waiting", metric.GetTag(ci.ContainerStatus))
assert.False(t, metric.HasField(ci.MetricName(ci.TypeContainer, ci.StatusWaiting)))
assert.False(t, metric.HasField(ci.MetricName(ci.TypeContainer, ci.StatusWaitingReasonCrashLoopBackOff)))
assert.False(t, metric.HasField(ci.MetricName(ci.TypeContainer, ci.StatusContainerWaiting)))
assert.False(t, metric.HasField(ci.MetricName(ci.TypeContainer, ci.StatusContainerWaitingReasonCrashLoopBackOff)))

pod.Status.ContainerStatuses[0].State.Waiting = &corev1.ContainerStateWaiting{Reason: "SomeOtherReason"}

Expand All @@ -845,8 +845,8 @@ func TestPodStore_addStatus_without_enhanced_metrics(t *testing.T) {

podStore.addStatus(metric, pod)
assert.Equal(t, "Waiting", metric.GetTag(ci.ContainerStatus))
assert.False(t, metric.HasField(ci.MetricName(ci.TypeContainer, ci.StatusWaiting)))
assert.False(t, metric.HasField(ci.MetricName(ci.TypeContainer, ci.StatusWaitingReasonCrashLoopBackOff)))
assert.False(t, metric.HasField(ci.MetricName(ci.TypeContainer, ci.StatusContainerWaiting)))
assert.False(t, metric.HasField(ci.MetricName(ci.TypeContainer, ci.StatusContainerWaitingReasonCrashLoopBackOff)))

// test delta of restartCount
pod.Status.ContainerStatuses[0].RestartCount = 3
Expand Down

0 comments on commit a512dfa

Please sign in to comment.