Skip to content

Commit 16e8f54

Browse files
authored
Merge pull request #1519 from harjas27/container_started_time
capture start time for containers in terminated state
2 parents 17204a5 + 6324b16 commit 16e8f54

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

internal/store/pod.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,12 @@ func createPodContainerStateStartedFamilyGenerator() generator.FamilyGenerator {
292292
LabelValues: []string{cs.Name},
293293
Value: float64((cs.State.Running.StartedAt).Unix()),
294294
})
295+
} else if cs.State.Terminated != nil {
296+
ms = append(ms, &metric.Metric{
297+
LabelKeys: []string{"container"},
298+
LabelValues: []string{cs.Name},
299+
Value: float64((cs.State.Terminated.StartedAt).Unix()),
300+
})
295301
}
296302
}
297303

internal/store/pod_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,56 @@ func TestPodStore(t *testing.T) {
365365
"kube_pod_init_container_status_terminated_reason",
366366
},
367367
},
368+
{
369+
Obj: &v1.Pod{
370+
ObjectMeta: metav1.ObjectMeta{
371+
Name: "pod1",
372+
Namespace: "ns1",
373+
UID: "uid1",
374+
},
375+
Status: v1.PodStatus{
376+
ContainerStatuses: []v1.ContainerStatus{
377+
{
378+
Name: "container1",
379+
State: v1.ContainerState{
380+
Terminated: &v1.ContainerStateTerminated{
381+
StartedAt: metav1.Time{
382+
Time: time.Unix(1501777018, 0),
383+
},
384+
Reason: "Completed",
385+
},
386+
},
387+
},
388+
},
389+
},
390+
},
391+
Want: `
392+
# HELP kube_pod_container_status_running Describes whether the container is currently in running state.
393+
# HELP kube_pod_container_state_started Start time in unix timestamp for a pod container.
394+
# HELP kube_pod_container_status_terminated Describes whether the container is currently in terminated state.
395+
# HELP kube_pod_container_status_terminated_reason Describes the reason the container is currently in terminated state.
396+
# HELP kube_pod_container_status_waiting Describes whether the container is currently in waiting state.
397+
# HELP kube_pod_container_status_waiting_reason Describes the reason the container is currently in waiting state.
398+
# TYPE kube_pod_container_status_running gauge
399+
# TYPE kube_pod_container_state_started gauge
400+
# TYPE kube_pod_container_status_terminated gauge
401+
# TYPE kube_pod_container_status_terminated_reason gauge
402+
# TYPE kube_pod_container_status_waiting gauge
403+
# TYPE kube_pod_container_status_waiting_reason gauge
404+
kube_pod_container_state_started{container="container1",namespace="ns1",pod="pod1",uid="uid1"} 1.501777018e+09
405+
kube_pod_container_status_running{container="container1",namespace="ns1",pod="pod1",uid="uid1"} 0
406+
kube_pod_container_status_terminated_reason{container="container1",namespace="ns1",pod="pod1",reason="Completed",uid="uid1"} 1
407+
kube_pod_container_status_terminated{container="container1",namespace="ns1",pod="pod1",uid="uid1"} 1
408+
kube_pod_container_status_waiting{container="container1",namespace="ns1",pod="pod1",uid="uid1"} 0
409+
`,
410+
MetricNames: []string{
411+
"kube_pod_container_status_running",
412+
"kube_pod_container_state_started",
413+
"kube_pod_container_status_waiting",
414+
"kube_pod_container_status_terminated",
415+
"kube_pod_container_status_terminated_reason",
416+
},
417+
},
368418
{
369419
Obj: &v1.Pod{
370420
ObjectMeta: metav1.ObjectMeta{
@@ -378,6 +428,9 @@ func TestPodStore(t *testing.T) {
378428
Name: "container2",
379429
State: v1.ContainerState{
380430
Terminated: &v1.ContainerStateTerminated{
431+
StartedAt: metav1.Time{
432+
Time: time.Unix(1501777018, 0),
433+
},
381434
Reason: "OOMKilled",
382435
},
383436
},
@@ -395,17 +448,20 @@ func TestPodStore(t *testing.T) {
395448
},
396449
Want: `
397450
# HELP kube_pod_container_status_running Describes whether the container is currently in running state.
451+
# HELP kube_pod_container_state_started Start time in unix timestamp for a pod container.
398452
# HELP kube_pod_container_status_terminated Describes whether the container is currently in terminated state.
399453
# HELP kube_pod_container_status_terminated_reason Describes the reason the container is currently in terminated state.
400454
# HELP kube_pod_container_status_waiting Describes whether the container is currently in waiting state.
401455
# HELP kube_pod_container_status_waiting_reason Describes the reason the container is currently in waiting state.
402456
# TYPE kube_pod_container_status_running gauge
457+
# TYPE kube_pod_container_state_started gauge
403458
# TYPE kube_pod_container_status_terminated gauge
404459
# TYPE kube_pod_container_status_terminated_reason gauge
405460
# TYPE kube_pod_container_status_waiting gauge
406461
# TYPE kube_pod_container_status_waiting_reason gauge
407462
kube_pod_container_status_running{container="container2",namespace="ns2",pod="pod2",uid="uid2"} 0
408463
kube_pod_container_status_running{container="container3",namespace="ns2",pod="pod2",uid="uid2"} 0
464+
kube_pod_container_state_started{container="container2",namespace="ns2",pod="pod2",uid="uid2"} 1.501777018e+09
409465
kube_pod_container_status_terminated_reason{container="container2",namespace="ns2",pod="pod2",reason="OOMKilled",uid="uid2"} 1
410466
kube_pod_container_status_terminated{container="container2",namespace="ns2",pod="pod2",uid="uid2"} 1
411467
kube_pod_container_status_terminated{container="container3",namespace="ns2",pod="pod2",uid="uid2"} 0
@@ -415,6 +471,7 @@ func TestPodStore(t *testing.T) {
415471
`,
416472
MetricNames: []string{
417473
"kube_pod_container_status_running",
474+
"kube_pod_container_state_started",
418475
"kube_pod_container_status_waiting",
419476
"kube_pod_container_status_terminated",
420477
"kube_pod_container_status_terminated_reason",

0 commit comments

Comments
 (0)