From 6379aab2ef45034937990ceb3a0042a239034697 Mon Sep 17 00:00:00 2001 From: Szymon Grzemski Date: Tue, 18 May 2021 14:44:26 +0200 Subject: [PATCH 1/5] Added kube_pod_status_ready_time and kube_pod_status_containers_ready_time --- docs/pod-metrics.md | 2 ++ internal/store/pod.go | 54 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/docs/pod-metrics.md b/docs/pod-metrics.md index 1d1db19d0d..9aadec3a02 100644 --- a/docs/pod-metrics.md +++ b/docs/pod-metrics.md @@ -9,6 +9,8 @@ | kube_pod_labels | Gauge | Kubernetes labels converted to Prometheus labels | | `pod`=<pod-name>
`namespace`=<pod-namespace>
`label_POD_LABEL`=<POD_LABEL>
`uid`=<pod-uid> | STABLE | | kube_pod_status_phase | Gauge | The pods current phase | | `pod`=<pod-name>
`namespace`=<pod-namespace>
`phase`=<Pending\|Running\|Succeeded\|Failed\|Unknown>
`uid`=<pod-uid> | STABLE | | kube_pod_status_ready | Gauge | Describes whether the pod is ready to serve requests | | `pod`=<pod-name>
`namespace`=<pod-namespace>
`condition`=<true\|false\|unknown>
`uid`=<pod-uid> | STABLE | +| kube_pod_status_ready_time | Gauge | Time when pod passed readiness probes. | seconds | `pod`=<pod-name>
`namespace`=<pod-namespace>
`uid`=<pod-uid> | STABLE | +| kube_pod_status_containers_ready_time | Gauge | Time when pod containers entered Ready state. | seconds | `pod`=<pod-name>
`namespace`=<pod-namespace>
`uid`=<pod-uid> | STABLE | | kube_pod_status_scheduled | Gauge | Describes the status of the scheduling process for the pod | |`pod`=<pod-name>
`namespace`=<pod-namespace>
`condition`=<true\|false\|unknown>
`uid`=<pod-uid> | STABLE | | kube_pod_container_info | Gauge | Information about a container in a pod | | `container`=<container-name>
`pod`=<pod-name>
`namespace`=<pod-namespace>
`image`=<image-name>
`image_id`=<image-id>
`container_id`=<containerid>
`uid`=<pod-uid> | STABLE | | kube_pod_container_status_waiting | Gauge | Describes whether the container is currently in waiting state | | `container`=<container-name>
`pod`=<pod-name>
`namespace`=<pod-namespace>
`uid`=<pod-uid> | STABLE | diff --git a/internal/store/pod.go b/internal/store/pod.go index 1589b7ab14..972f5fef3a 100644 --- a/internal/store/pod.go +++ b/internal/store/pod.go @@ -85,6 +85,8 @@ func podMetricFamilies(allowLabelsList []string) []generator.FamilyGenerator { createPodStartTimeFamilyGenerator(), createPodStatusPhaseFamilyGenerator(), createPodStatusReadyFamilyGenerator(), + createPodStatusReadyTimeFamilyGenerator(), + createPodStatusContainersReadyTimeFamilyGenerator(), createPodStatusReasonFamilyGenerator(), createPodStatusScheduledFamilyGenerator(), createPodStatusScheduledTimeFamilyGenerator(), @@ -1362,6 +1364,32 @@ func createPodStartTimeFamilyGenerator() generator.FamilyGenerator { ) } +func createPodStatusContainersReadyTimeFamilyGenerator() generator.FamilyGenerator { + return *generator.NewFamilyGenerator( + "kube_pod_status_containers_ready_time", + "Readiness achieved time in unix timestamp for a pod containers.", + metric.Gauge, + "", + wrapPodFunc(func(p *v1.Pod) *metric.Family { + ms := []*metric.Metric{} + + for _, c := range p.Status.Conditions { + if c.Type == v1.ContainersReady { + ms = append(ms, &metric.Metric{ + LabelKeys: []string{}, + LabelValues: []string{}, + Value: float64((c.LastTransitionTime).Unix()), + }) + } + } + + return &metric.Family{ + Metrics: ms, + } + }), + ) +} + func createPodStatusPhaseFamilyGenerator() generator.FamilyGenerator { return *generator.NewFamilyGenerator( "kube_pod_status_phase", @@ -1433,6 +1461,32 @@ func createPodStatusReadyFamilyGenerator() generator.FamilyGenerator { ) } +func createPodStatusReadyTimeFamilyGenerator() generator.FamilyGenerator { + return *generator.NewFamilyGenerator( + "kube_pod_status_ready_time", + "Readiness achieved time in unix timestamp for a pod.", + metric.Gauge, + "", + wrapPodFunc(func(p *v1.Pod) *metric.Family { + ms := []*metric.Metric{} + + for _, c := range p.Status.Conditions { + if c.Type == v1.PodReady { + ms = append(ms, &metric.Metric{ + LabelKeys: []string{}, + LabelValues: []string{}, + Value: float64((c.LastTransitionTime).Unix()), + }) + } + } + + return &metric.Family{ + Metrics: ms, + } + }), + ) +} + func createPodStatusReasonFamilyGenerator() generator.FamilyGenerator { return *generator.NewFamilyGenerator( "kube_pod_status_reason", From af5241f653d756e2ce871230111eb014c17d112c Mon Sep 17 00:00:00 2001 From: Szymon Grzemski Date: Tue, 18 May 2021 14:45:57 +0200 Subject: [PATCH 2/5] Applied gofmt --- internal/store/pod.go | 86 +++++++++++++++++++++---------------------- 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/internal/store/pod.go b/internal/store/pod.go index 972f5fef3a..72a76d2f0d 100644 --- a/internal/store/pod.go +++ b/internal/store/pod.go @@ -1365,29 +1365,29 @@ func createPodStartTimeFamilyGenerator() generator.FamilyGenerator { } func createPodStatusContainersReadyTimeFamilyGenerator() generator.FamilyGenerator { - return *generator.NewFamilyGenerator( - "kube_pod_status_containers_ready_time", - "Readiness achieved time in unix timestamp for a pod containers.", - metric.Gauge, - "", - wrapPodFunc(func(p *v1.Pod) *metric.Family { - ms := []*metric.Metric{} + return *generator.NewFamilyGenerator( + "kube_pod_status_containers_ready_time", + "Readiness achieved time in unix timestamp for a pod containers.", + metric.Gauge, + "", + wrapPodFunc(func(p *v1.Pod) *metric.Family { + ms := []*metric.Metric{} for _, c := range p.Status.Conditions { if c.Type == v1.ContainersReady { - ms = append(ms, &metric.Metric{ - LabelKeys: []string{}, - LabelValues: []string{}, - Value: float64((c.LastTransitionTime).Unix()), - }) - } - } - - return &metric.Family{ - Metrics: ms, - } - }), - ) + ms = append(ms, &metric.Metric{ + LabelKeys: []string{}, + LabelValues: []string{}, + Value: float64((c.LastTransitionTime).Unix()), + }) + } + } + + return &metric.Family{ + Metrics: ms, + } + }), + ) } func createPodStatusPhaseFamilyGenerator() generator.FamilyGenerator { @@ -1462,29 +1462,29 @@ func createPodStatusReadyFamilyGenerator() generator.FamilyGenerator { } func createPodStatusReadyTimeFamilyGenerator() generator.FamilyGenerator { - return *generator.NewFamilyGenerator( - "kube_pod_status_ready_time", - "Readiness achieved time in unix timestamp for a pod.", - metric.Gauge, - "", - wrapPodFunc(func(p *v1.Pod) *metric.Family { - ms := []*metric.Metric{} - - for _, c := range p.Status.Conditions { - if c.Type == v1.PodReady { - ms = append(ms, &metric.Metric{ - LabelKeys: []string{}, - LabelValues: []string{}, - Value: float64((c.LastTransitionTime).Unix()), - }) - } - } - - return &metric.Family{ - Metrics: ms, - } - }), - ) + return *generator.NewFamilyGenerator( + "kube_pod_status_ready_time", + "Readiness achieved time in unix timestamp for a pod.", + metric.Gauge, + "", + wrapPodFunc(func(p *v1.Pod) *metric.Family { + ms := []*metric.Metric{} + + for _, c := range p.Status.Conditions { + if c.Type == v1.PodReady { + ms = append(ms, &metric.Metric{ + LabelKeys: []string{}, + LabelValues: []string{}, + Value: float64((c.LastTransitionTime).Unix()), + }) + } + } + + return &metric.Family{ + Metrics: ms, + } + }), + ) } func createPodStatusReasonFamilyGenerator() generator.FamilyGenerator { From a0fb2b434fd92821d07dccbb4253087fd6ad94e9 Mon Sep 17 00:00:00 2001 From: Szymon Grzemski Date: Tue, 18 May 2021 15:43:46 +0200 Subject: [PATCH 3/5] Added tests for kube_pod_status_ready_time and kube_pod_status_containers_ready_time --- internal/store/pod_test.go | 22 +++++++++++++++++----- main_test.go | 8 ++++++-- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/internal/store/pod_test.go b/internal/store/pod_test.go index 949c9134c6..ea2e358dd3 100644 --- a/internal/store/pod_test.go +++ b/internal/store/pod_test.go @@ -1075,13 +1075,19 @@ func TestPodStore(t *testing.T) { { Type: v1.PodReady, Status: v1.ConditionTrue, + LastTransitionTime: metav1.Time{ + Time: time.Unix(1501666018, 0), + }, }, }, }, }, Want: ` # HELP kube_pod_status_ready Describes whether the pod is ready to serve requests. - # TYPE kube_pod_status_ready gauge + # HELP kube_pod_status_ready_time Readiness achieved time in unix timestamp for a pod. + # TYPE kube_pod_status_ready gauge + # TYPE kube_pod_status_ready_time gauge + kube_pod_status_ready_time{namespace="ns1",pod="pod1",uid="uid1"} 1.501666018e+09 kube_pod_status_ready{condition="false",namespace="ns1",pod="pod1",uid="uid1"} 0 kube_pod_status_ready{condition="true",namespace="ns1",pod="pod1",uid="uid1"} 1 kube_pod_status_ready{condition="unknown",namespace="ns1",pod="pod1",uid="uid1"} 0 @@ -1100,16 +1106,22 @@ func TestPodStore(t *testing.T) { { Type: v1.PodReady, Status: v1.ConditionFalse, + LastTransitionTime: metav1.Time{ + Time: time.Unix(1501666018, 0), + }, }, }, }, }, Want: ` # HELP kube_pod_status_ready Describes whether the pod is ready to serve requests. - # TYPE kube_pod_status_ready gauge - kube_pod_status_ready{condition="false",namespace="ns2",pod="pod2",uid="uid2"} 1 - kube_pod_status_ready{condition="true",namespace="ns2",pod="pod2",uid="uid2"} 0 - kube_pod_status_ready{condition="unknown",namespace="ns2",pod="pod2",uid="uid2"} 0 + # HELP kube_pod_status_ready_time Readiness achieved time in unix timestamp for a pod. + # TYPE kube_pod_status_ready gauge + # TYPE kube_pod_status_ready_time gauge + kube_pod_status_ready_time{namespace="ns2",pod="pod2",uid="uid2"} 1.501666018e+09 + kube_pod_status_ready{condition="false",namespace="ns2",pod="pod2",uid="uid2"} 1 + kube_pod_status_ready{condition="true",namespace="ns2",pod="pod2",uid="uid2"} 0 + kube_pod_status_ready{condition="unknown",namespace="ns2",pod="pod2",uid="uid2"} 0 `, MetricNames: []string{"kube_pod_status_ready"}, }, diff --git a/main_test.go b/main_test.go index 69086f8d6b..68772b806a 100644 --- a/main_test.go +++ b/main_test.go @@ -203,14 +203,16 @@ func TestFullScrapeCycle(t *testing.T) { # HELP kube_pod_labels Kubernetes labels converted to Prometheus labels. # HELP kube_pod_overhead_cpu_cores The pod overhead in regards to cpu cores associated with running a pod. # HELP kube_pod_overhead_memory_bytes The pod overhead in regards to memory associated with running a pod. -# HELP kube_pod_runtimeclass_name_info The runtimeclass associated with the pod. # HELP kube_pod_owner Information about the Pod's owner. # HELP kube_pod_restart_policy Describes the restart policy in use by this pod. +# HELP kube_pod_runtimeclass_name_info The runtimeclass associated with the pod. # HELP kube_pod_spec_volumes_persistentvolumeclaims_info Information about persistentvolumeclaim volumes in a pod. # HELP kube_pod_spec_volumes_persistentvolumeclaims_readonly Describes whether a persistentvolumeclaim is mounted read only. # HELP kube_pod_start_time Start time in unix timestamp for a pod. +# HELP kube_pod_status_containers_ready_time Readiness achieved time in unix timestamp for a pod containers. # HELP kube_pod_status_phase The pods current phase. # HELP kube_pod_status_ready Describes whether the pod is ready to serve requests. +# HELP kube_pod_status_ready_time Readiness achieved time in unix timestamp for a pod. # HELP kube_pod_status_reason The pod status reasons # HELP kube_pod_status_scheduled Describes the status of the scheduling process for the pod. # HELP kube_pod_status_scheduled_time Unix timestamp when pod moved into scheduled status @@ -253,14 +255,16 @@ func TestFullScrapeCycle(t *testing.T) { # TYPE kube_pod_labels gauge # TYPE kube_pod_overhead_cpu_cores gauge # TYPE kube_pod_overhead_memory_bytes gauge -# TYPE kube_pod_runtimeclass_name_info gauge # TYPE kube_pod_owner gauge # TYPE kube_pod_restart_policy gauge +# TYPE kube_pod_runtimeclass_name_info gauge # TYPE kube_pod_spec_volumes_persistentvolumeclaims_info gauge # TYPE kube_pod_spec_volumes_persistentvolumeclaims_readonly gauge # TYPE kube_pod_start_time gauge +# TYPE kube_pod_status_containers_ready_time gauge # TYPE kube_pod_status_phase gauge # TYPE kube_pod_status_ready gauge +# TYPE kube_pod_status_ready_time gauge # TYPE kube_pod_status_reason gauge # TYPE kube_pod_status_scheduled gauge # TYPE kube_pod_status_scheduled_time gauge From 1d33b0fc0b19be6295dabffcd31b3daf695087fa Mon Sep 17 00:00:00 2001 From: Szymon Grzemski Date: Tue, 18 May 2021 17:27:46 +0200 Subject: [PATCH 4/5] Added two more families, so I had to fix benchamrk tests --- internal/store/pod_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/store/pod_test.go b/internal/store/pod_test.go index ea2e358dd3..2a0f03d16d 100644 --- a/internal/store/pod_test.go +++ b/internal/store/pod_test.go @@ -1625,7 +1625,7 @@ func BenchmarkPodStore(b *testing.B) { }, } - expectedFamilies := 50 + expectedFamilies := 52 for n := 0; n < b.N; n++ { families := f(pod) if len(families) != expectedFamilies { From fb65f2d6f58f916db34ccd27695bd734f63af0cd Mon Sep 17 00:00:00 2001 From: Ankit Patel Date: Mon, 24 Jan 2022 17:52:50 -0800 Subject: [PATCH 5/5] rename metric cuz forked --- docs/pod-metrics.md | 2 +- internal/store/pod.go | 2 +- internal/store/pod_test.go | 12 ++++++------ main_test.go | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/docs/pod-metrics.md b/docs/pod-metrics.md index 3aafdbed1c..a846cfd881 100644 --- a/docs/pod-metrics.md +++ b/docs/pod-metrics.md @@ -10,7 +10,7 @@ | kube_pod_labels | Gauge | Kubernetes labels converted to Prometheus labels | | `pod`=<pod-name>
`namespace`=<pod-namespace>
`label_POD_LABEL`=<POD_LABEL>
`uid`=<pod-uid> | STABLE | | kube_pod_status_phase | Gauge | The pods current phase | | `pod`=<pod-name>
`namespace`=<pod-namespace>
`phase`=<Pending\|Running\|Succeeded\|Failed\|Unknown>
`uid`=<pod-uid> | STABLE | | kube_pod_status_ready | Gauge | Describes whether the pod is ready to serve requests | | `pod`=<pod-name>
`namespace`=<pod-namespace>
`condition`=<true\|false\|unknown>
`uid`=<pod-uid> | STABLE | -| kube_pod_status_ready_time | Gauge | Time when pod passed readiness probes. | seconds | `pod`=<pod-name>
`namespace`=<pod-namespace>
`uid`=<pod-uid> | STABLE | +| affirm_kube_pod_status_ready_time | Gauge | Time when pod passed readiness probes. | seconds | `pod`=<pod-name>
`namespace`=<pod-namespace>
`uid`=<pod-uid> | STABLE | | kube_pod_status_containers_ready_time | Gauge | Time when pod containers entered Ready state. | seconds | `pod`=<pod-name>
`namespace`=<pod-namespace>
`uid`=<pod-uid> | STABLE | | kube_pod_status_scheduled | Gauge | Describes the status of the scheduling process for the pod | |`pod`=<pod-name>
`namespace`=<pod-namespace>
`condition`=<true\|false\|unknown>
`uid`=<pod-uid> | STABLE | | kube_pod_container_info | Gauge | Information about a container in a pod | | `container`=<container-name>
`pod`=<pod-name>
`namespace`=<pod-namespace>
`image`=<image-name>
`image_id`=<image-id>
`image_spec`=<image-spec>
`container_id`=<containerid>
`uid`=<pod-uid> | STABLE | diff --git a/internal/store/pod.go b/internal/store/pod.go index 91eb69ce5d..223b5e90f7 100644 --- a/internal/store/pod.go +++ b/internal/store/pod.go @@ -1280,7 +1280,7 @@ func createPodStatusReadyFamilyGenerator() generator.FamilyGenerator { func createPodStatusReadyTimeFamilyGenerator() generator.FamilyGenerator { return *generator.NewFamilyGenerator( - "kube_pod_status_ready_time", + "affirm_kube_pod_status_ready_time", "Readiness achieved time in unix timestamp for a pod.", metric.Gauge, "", diff --git a/internal/store/pod_test.go b/internal/store/pod_test.go index f29bab81ac..8b7bc54cf7 100644 --- a/internal/store/pod_test.go +++ b/internal/store/pod_test.go @@ -1375,10 +1375,10 @@ func TestPodStore(t *testing.T) { }, Want: ` # HELP kube_pod_status_ready Describes whether the pod is ready to serve requests. - # HELP kube_pod_status_ready_time Readiness achieved time in unix timestamp for a pod. + # HELP affirm_kube_pod_status_ready_time Readiness achieved time in unix timestamp for a pod. # TYPE kube_pod_status_ready gauge - # TYPE kube_pod_status_ready_time gauge - kube_pod_status_ready_time{namespace="ns1",pod="pod1",uid="uid1"} 1.501666018e+09 + # TYPE affirm_kube_pod_status_ready_time gauge + affirm_kube_pod_status_ready_time{namespace="ns1",pod="pod1",uid="uid1"} 1.501666018e+09 kube_pod_status_ready{condition="false",namespace="ns1",pod="pod1",uid="uid1"} 0 kube_pod_status_ready{condition="true",namespace="ns1",pod="pod1",uid="uid1"} 1 kube_pod_status_ready{condition="unknown",namespace="ns1",pod="pod1",uid="uid1"} 0 @@ -1406,10 +1406,10 @@ func TestPodStore(t *testing.T) { }, Want: ` # HELP kube_pod_status_ready Describes whether the pod is ready to serve requests. - # HELP kube_pod_status_ready_time Readiness achieved time in unix timestamp for a pod. + # HELP affirm_kube_pod_status_ready_time Readiness achieved time in unix timestamp for a pod. # TYPE kube_pod_status_ready gauge - # TYPE kube_pod_status_ready_time gauge - kube_pod_status_ready_time{namespace="ns2",pod="pod2",uid="uid2"} 1.501666018e+09 + # TYPE affirm_kube_pod_status_ready_time gauge + affirm_kube_pod_status_ready_time{namespace="ns2",pod="pod2",uid="uid2"} 1.501666018e+09 kube_pod_status_ready{condition="false",namespace="ns2",pod="pod2",uid="uid2"} 1 kube_pod_status_ready{condition="true",namespace="ns2",pod="pod2",uid="uid2"} 0 kube_pod_status_ready{condition="unknown",namespace="ns2",pod="pod2",uid="uid2"} 0 diff --git a/main_test.go b/main_test.go index 92a7b70806..b049fc74a7 100644 --- a/main_test.go +++ b/main_test.go @@ -211,7 +211,7 @@ func TestFullScrapeCycle(t *testing.T) { # HELP kube_pod_status_containers_ready_time Readiness achieved time in unix timestamp for a pod containers. # HELP kube_pod_status_phase The pods current phase. # HELP kube_pod_status_ready Describes whether the pod is ready to serve requests. -# HELP kube_pod_status_ready_time Readiness achieved time in unix timestamp for a pod. +# HELP affirm_kube_pod_status_ready_time Readiness achieved time in unix timestamp for a pod. # HELP kube_pod_status_reason The pod status reasons # HELP kube_pod_status_scheduled Describes the status of the scheduling process for the pod. # HELP kube_pod_status_scheduled_time Unix timestamp when pod moved into scheduled status @@ -256,7 +256,7 @@ func TestFullScrapeCycle(t *testing.T) { # TYPE kube_pod_status_containers_ready_time gauge # TYPE kube_pod_status_phase gauge # TYPE kube_pod_status_ready gauge -# TYPE kube_pod_status_ready_time gauge +# TYPE affirm_kube_pod_status_ready_time gauge # TYPE kube_pod_status_reason gauge # TYPE kube_pod_status_scheduled gauge # TYPE kube_pod_status_scheduled_time gauge