Skip to content

Commit 109413e

Browse files
authored
Merge pull request #2148 from opeco17/feature/implement-pod-initialized-time
feat: implement kube_pod_status_initialized_time
2 parents 95f4ad2 + 3e4d91c commit 109413e

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

docs/pod-metrics.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
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 | - |
2626
| 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 | - |
27+
| 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 |
2728
| 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 |
2829
| kube_pod_status_container_ready_time | Gauge | Time when the container of the pod entered Ready state. | seconds | `pod`=&lt;pod-name&gt; <br> `namespace`=&lt;pod-namespace&gt; <br> `uid`=&lt;pod-uid&gt; | EXPERIMENTAL |
2930
| kube_pod_container_status_restarts_total | Counter | The number of container restarts per container | | `container`=&lt;container-name&gt; <br> `namespace`=&lt;pod-namespace&gt; <br> `pod`=&lt;pod-name&gt; <br> `uid`=&lt;pod-uid&gt; | STABLE | - |

internal/store/pod.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func podMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generat
8585
createPodStatusQosClassFamilyGenerator(),
8686
createPodStatusReadyFamilyGenerator(),
8787
createPodStatusReadyTimeFamilyGenerator(),
88+
createPodStatusInitializedTimeFamilyGenerator(),
8889
createPodStatusContainerReadyTimeFamilyGenerator(),
8990
createPodStatusReasonFamilyGenerator(),
9091
createPodStatusScheduledFamilyGenerator(),
@@ -1337,6 +1338,33 @@ func createPodStatusPhaseFamilyGenerator() generator.FamilyGenerator {
13371338
)
13381339
}
13391340

1341+
func createPodStatusInitializedTimeFamilyGenerator() generator.FamilyGenerator {
1342+
return *generator.NewFamilyGeneratorWithStability(
1343+
"kube_pod_status_initialized_time",
1344+
"Initialized time in unix timestamp for a pod.",
1345+
metric.Gauge,
1346+
basemetrics.ALPHA,
1347+
"",
1348+
wrapPodFunc(func(p *v1.Pod) *metric.Family {
1349+
ms := []*metric.Metric{}
1350+
1351+
for _, c := range p.Status.Conditions {
1352+
if c.Type == v1.PodInitialized && c.Status == v1.ConditionTrue {
1353+
ms = append(ms, &metric.Metric{
1354+
LabelKeys: []string{},
1355+
LabelValues: []string{},
1356+
Value: float64((c.LastTransitionTime).Unix()),
1357+
})
1358+
}
1359+
}
1360+
1361+
return &metric.Family{
1362+
Metrics: ms,
1363+
}
1364+
}),
1365+
)
1366+
}
1367+
13401368
func createPodStatusContainerReadyTimeFamilyGenerator() generator.FamilyGenerator {
13411369
return *generator.NewFamilyGeneratorWithStability(
13421370
"kube_pod_status_container_ready_time",

internal/store/pod_test.go

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,57 @@ func TestPodStore(t *testing.T) {
14341434
`,
14351435
MetricNames: []string{"kube_pod_status_reason"},
14361436
},
1437+
{
1438+
Obj: &v1.Pod{
1439+
ObjectMeta: metav1.ObjectMeta{
1440+
Name: "pod1",
1441+
Namespace: "ns1",
1442+
UID: "uid1",
1443+
},
1444+
Status: v1.PodStatus{
1445+
Conditions: []v1.PodCondition{
1446+
{
1447+
Type: v1.PodInitialized,
1448+
Status: v1.ConditionTrue,
1449+
LastTransitionTime: metav1.Time{
1450+
Time: time.Unix(1501666018, 0),
1451+
},
1452+
},
1453+
},
1454+
},
1455+
},
1456+
Want: `
1457+
# HELP kube_pod_status_initialized_time Initialized time in unix timestamp for a pod.
1458+
# TYPE kube_pod_status_initialized_time gauge
1459+
kube_pod_status_initialized_time{namespace="ns1",pod="pod1",uid="uid1"} 1.501666018e+09
1460+
`,
1461+
MetricNames: []string{"kube_pod_status_initialized_time"},
1462+
},
1463+
{
1464+
Obj: &v1.Pod{
1465+
ObjectMeta: metav1.ObjectMeta{
1466+
Name: "pod1",
1467+
Namespace: "ns1",
1468+
UID: "uid1",
1469+
},
1470+
Status: v1.PodStatus{
1471+
Conditions: []v1.PodCondition{
1472+
{
1473+
Type: v1.PodInitialized,
1474+
Status: v1.ConditionFalse,
1475+
LastTransitionTime: metav1.Time{
1476+
Time: time.Unix(1501666018, 0),
1477+
},
1478+
},
1479+
},
1480+
},
1481+
},
1482+
Want: `
1483+
# HELP kube_pod_status_initialized_time Initialized time in unix timestamp for a pod.
1484+
# TYPE kube_pod_status_initialized_time gauge
1485+
`,
1486+
MetricNames: []string{"kube_pod_status_initialized_time"},
1487+
},
14371488
{
14381489
Obj: &v1.Pod{
14391490
ObjectMeta: metav1.ObjectMeta{
@@ -2181,7 +2232,7 @@ func BenchmarkPodStore(b *testing.B) {
21812232
},
21822233
}
21832234

2184-
expectedFamilies := 51
2235+
expectedFamilies := 52
21852236
for n := 0; n < b.N; n++ {
21862237
families := f(pod)
21872238
if len(families) != expectedFamilies {

pkg/app/server_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ func TestFullScrapeCycle(t *testing.T) {
242242
# HELP kube_pod_spec_volumes_persistentvolumeclaims_readonly [STABLE] Describes whether a persistentvolumeclaim is mounted read only.
243243
# HELP kube_pod_start_time [STABLE] Start time in unix timestamp for a pod.
244244
# HELP kube_pod_status_container_ready_time Readiness achieved time in unix timestamp for a pod containers.
245+
# HELP kube_pod_status_initialized_time Initialized time in unix timestamp for a pod.
245246
# HELP kube_pod_status_qos_class The pods current qosClass.
246247
# HELP kube_pod_status_phase [STABLE] The pods current phase.
247248
# HELP kube_pod_status_ready_time Readiness achieved time in unix timestamp for a pod.
@@ -292,6 +293,7 @@ func TestFullScrapeCycle(t *testing.T) {
292293
# TYPE kube_pod_spec_volumes_persistentvolumeclaims_readonly gauge
293294
# TYPE kube_pod_start_time gauge
294295
# TYPE kube_pod_status_container_ready_time gauge
296+
# TYPE kube_pod_status_initialized_time gauge
295297
# TYPE kube_pod_status_phase gauge
296298
# TYPE kube_pod_status_qos_class gauge
297299
# TYPE kube_pod_status_ready gauge

0 commit comments

Comments
 (0)