Skip to content

Commit 51ccb66

Browse files
feat: add kube_pod_container_status_last_terminated_timestamp metric
Signed-off-by: Tetiana Kravchenko <[email protected]>
1 parent fde27ec commit 51ccb66

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

docs/pod-metrics.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
| kube_pod_container_status_terminated_reason | Gauge | Describes the reason the container is currently in terminated state | | `container`=&lt;container-name&gt; <br> `pod`=&lt;pod-name&gt; <br> `namespace`=&lt;pod-namespace&gt; <br> `reason`=&lt;container-terminated-reason&gt; <br> `uid`=&lt;pod-uid&gt; | EXPERIMENTAL | - |
2424
| kube_pod_container_status_last_terminated_reason | Gauge | Describes the last reason the container was in terminated state | | `container`=&lt;container-name&gt; <br> `pod`=&lt;pod-name&gt; <br> `namespace`=&lt;pod-namespace&gt; <br> `reason`=&lt;last-terminated-reason&gt; <br> `uid`=&lt;pod-uid&gt; | EXPERIMENTAL | - |
2525
| kube_pod_container_status_last_terminated_exitcode | Gauge | Describes the exit code for the last container in terminated state. | | `container`=&lt;container-name&gt; <br> `pod`=&lt;pod-name&gt; <br> `namespace`=&lt;pod-namespace&gt; <br> `uid`=&lt;pod-uid&gt; | EXPERIMENTAL | - |
26+
| kube_pod_container_status_last_terminated_timestamp | Gauge | Last terminated time for a pod container, when container was terminated with kube_pod_container_status_last_terminated_reason, in unix timestamp. | | `container`=&lt;container-name&gt; <br> `pod`=&lt;pod-name&gt; <br> `namespace`=&lt;pod-namespace&gt; <br> `uid`=&lt;pod-uid&gt; | EXPERIMENTAL | - |
2627
| kube_pod_container_status_ready | Gauge | Describes whether the containers readiness check succeeded | | `container`=&lt;container-name&gt; <br> `pod`=&lt;pod-name&gt; <br> `namespace`=&lt;pod-namespace&gt; <br> `uid`=&lt;pod-uid&gt; | STABLE | - |
2728
| kube_pod_status_initialized_time | Gauge | Time when the pod is initialized. | seconds | `pod`=&lt;pod-name&gt; <br> `namespace`=&lt;pod-namespace&gt; <br> `uid`=&lt;pod-uid&gt; | EXPERIMENTAL |
2829
| kube_pod_status_ready_time | Gauge | Time when pod passed readiness probes. | seconds | `pod`=&lt;pod-name&gt; <br> `namespace`=&lt;pod-namespace&gt; <br> `uid`=&lt;pod-uid&gt; | EXPERIMENTAL |

internal/store/pod.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func podMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generat
4949
createPodContainerStateStartedFamilyGenerator(),
5050
createPodContainerStatusLastTerminatedReasonFamilyGenerator(),
5151
createPodContainerStatusLastTerminatedExitCodeFamilyGenerator(),
52+
createPodContainerStatusLastTerminatedTimestampFamilyGenerator(),
5253
createPodContainerStatusReadyFamilyGenerator(),
5354
createPodContainerStatusRestartsTotalFamilyGenerator(),
5455
createPodContainerStatusRunningFamilyGenerator(),
@@ -375,6 +376,32 @@ func createPodContainerStatusLastTerminatedExitCodeFamilyGenerator() generator.F
375376
)
376377
}
377378

379+
func createPodContainerStatusLastTerminatedTimestampFamilyGenerator() generator.FamilyGenerator {
380+
return *generator.NewFamilyGeneratorWithStability(
381+
"kube_pod_container_status_last_terminated_timestamp",
382+
"Last terminated time for a pod container, when container was terminated with kube_pod_container_status_last_terminated_reason, in unix timestamp.",
383+
metric.Gauge,
384+
basemetrics.ALPHA,
385+
"",
386+
wrapPodFunc(func(p *v1.Pod) *metric.Family {
387+
ms := make([]*metric.Metric, 0, len(p.Status.ContainerStatuses))
388+
for _, cs := range p.Status.ContainerStatuses {
389+
if cs.LastTerminationState.Terminated != nil {
390+
ms = append(ms, &metric.Metric{
391+
LabelKeys: []string{"container"},
392+
LabelValues: []string{cs.Name},
393+
Value: float64(cs.LastTerminationState.Terminated.FinishedAt.Unix()),
394+
})
395+
}
396+
}
397+
398+
return &metric.Family{
399+
Metrics: ms,
400+
}
401+
}),
402+
)
403+
}
404+
378405
func createPodContainerStatusReadyFamilyGenerator() generator.FamilyGenerator {
379406
return *generator.NewFamilyGeneratorWithStability(
380407
"kube_pod_container_status_ready",

internal/store/pod_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,9 @@ func TestPodStore(t *testing.T) {
720720
Terminated: &v1.ContainerStateTerminated{
721721
Reason: "OOMKilled",
722722
ExitCode: 137,
723+
FinishedAt: metav1.Time{
724+
Time: time.Unix(1501779547, 0),
725+
},
723726
},
724727
},
725728
},
@@ -729,6 +732,7 @@ func TestPodStore(t *testing.T) {
729732
Want: `
730733
# HELP kube_pod_container_status_last_terminated_reason Describes the last reason the container was in terminated state.
731734
# HELP kube_pod_container_status_last_terminated_exitcode Describes the exit code for the last container in terminated state.
735+
# HELP kube_pod_container_status_last_terminated_timestamp Last terminated time for a pod container, when container was terminated with kube_pod_container_status_last_terminated_reason, in unix timestamp.
732736
# HELP kube_pod_container_status_running [STABLE] Describes whether the container is currently in running state.
733737
# HELP kube_pod_container_status_terminated [STABLE] Describes whether the container is currently in terminated state.
734738
# HELP kube_pod_container_status_terminated_reason Describes the reason the container is currently in terminated state.
@@ -737,6 +741,7 @@ func TestPodStore(t *testing.T) {
737741
# HELP kube_pod_container_state_started [STABLE] Start time in unix timestamp for a pod container.
738742
# TYPE kube_pod_container_status_last_terminated_reason gauge
739743
# TYPE kube_pod_container_status_last_terminated_exitcode gauge
744+
# TYPE kube_pod_container_status_last_terminated_timestamp gauge
740745
# TYPE kube_pod_container_status_running gauge
741746
# TYPE kube_pod_container_status_terminated gauge
742747
# TYPE kube_pod_container_status_terminated_reason gauge
@@ -749,10 +754,12 @@ func TestPodStore(t *testing.T) {
749754
kube_pod_container_status_waiting{container="container7",namespace="ns6",pod="pod6",uid="uid6"} 0
750755
kube_pod_container_status_last_terminated_reason{container="container7",namespace="ns6",pod="pod6",reason="OOMKilled",uid="uid6"} 1
751756
kube_pod_container_status_last_terminated_exitcode{container="container7",namespace="ns6",pod="pod6",uid="uid6"} 137
757+
kube_pod_container_status_last_terminated_timestamp{container="container7",namespace="ns6",pod="pod6",uid="uid6"} 1.501779547e+09
752758
`,
753759
MetricNames: []string{
754760
"kube_pod_container_status_last_terminated_reason",
755761
"kube_pod_container_status_last_terminated_exitcode",
762+
"kube_pod_container_status_last_terminated_timestamp",
756763
"kube_pod_container_status_running",
757764
"kube_pod_container_state_started",
758765
"kube_pod_container_status_terminated",

0 commit comments

Comments
 (0)