Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add start and termination timestamps for init containers #2638

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/metrics/workload/pod-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
| kube_pod_init_container_status_restarts_total | Counter | The number of restarts for the init container | integer | `container`=&lt;container-name&gt; <br> `namespace`=&lt;pod-namespace&gt; <br> `pod`=&lt;pod-name&gt; <br> `uid`=&lt;pod-uid&gt; | STABLE | - |
| kube_pod_init_container_resource_limits | Gauge | The number of CPU cores requested limit by an init container | `cpu`=&lt;core&gt; <br> `memory`=&lt;bytes&gt; | `resource`=&lt;resource-name&gt; <br> `unit`=&lt;resource-unit&gt; <br> `container`=&lt;container-name&gt; <br> `pod`=&lt;pod-name&gt; <br> `namespace`=&lt;pod-namespace&gt; <br> `node`=&lt; node-name&gt; <br> `uid`=&lt;pod-uid&gt; | EXPERIMENTAL | - |
| kube_pod_init_container_resource_requests | Gauge | The number of CPU cores requested by an init container | `cpu`=&lt;core&gt; <br> `memory`=&lt;bytes&gt; | `resource`=&lt;resource-name&gt; <br> `unit`=&lt;resource-unit&gt; <br> `container`=&lt;container-name&gt; <br> `pod`=&lt;pod-name&gt; <br> `namespace`=&lt;pod-namespace&gt; <br> `node`=&lt; node-name&gt; <br> `uid`=&lt;pod-uid&gt; | EXPERIMENTAL | - |
| kube_pod_init_container_state_started | Gauge | Start time in unix timestamp for a pod init container | seconds | `container`=&lt;container-name&gt; <br> `pod`=&lt;pod-name&gt; <br> `namespace`=&lt;pod-namespace&gt; <br> `uid`=&lt;pod-uid&gt; | EXPERIMENTAL | - |
| kube_pod_init_container_status_last_terminated_timestamp | Gauge | Last terminated time for a pod init container 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 | - |
| kube_pod_spec_volumes_persistentvolumeclaims_info | Gauge | Information about persistentvolumeclaim volumes in a pod | | `pod`=&lt;pod-name&gt; <br> `namespace`=&lt;pod-namespace&gt; <br> `volume`=&lt;volume-name&gt; <br> `persistentvolumeclaim`=&lt;persistentvolumeclaim-claimname&gt; <br> `uid`=&lt;pod-uid&gt; | STABLE | - |
| kube_pod_spec_volumes_persistentvolumeclaims_readonly | Gauge | Describes whether a persistentvolumeclaim is mounted read only | bool | `pod`=&lt;pod-name&gt; <br> `namespace`=&lt;pod-namespace&gt; <br> `volume`=&lt;volume-name&gt; <br> `persistentvolumeclaim`=&lt;persistentvolumeclaim-claimname&gt; <br> `uid`=&lt;pod-uid&gt; | STABLE | - |
| kube_pod_status_reason | Gauge | The pod status reasons | | `pod`=&lt;pod-name&gt; <br> `namespace`=&lt;pod-namespace&gt; <br> `reason`=&lt;Evicted\|NodeAffinity\|NodeLost\|Shutdown\|UnexpectedAdmissionError&gt; <br> `uid`=&lt;pod-uid&gt; | EXPERIMENTAL | - |
Expand Down
61 changes: 61 additions & 0 deletions internal/store/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ func podMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generat
createPodInitContainerInfoFamilyGenerator(),
createPodInitContainerResourceLimitsFamilyGenerator(),
createPodInitContainerResourceRequestsFamilyGenerator(),
createPodInitContainerStateStartedFamilyGenerator(),
createPodInitContainerStatusLastTerminatedReasonFamilyGenerator(),
createPodInitContainerStatusLastTerminatedTimestampFamilyGenerator(),
createPodInitContainerStatusReadyFamilyGenerator(),
createPodInitContainerStatusRestartsTotalFamilyGenerator(),
createPodInitContainerStatusRunningFamilyGenerator(),
Expand Down Expand Up @@ -858,6 +860,39 @@ func createPodInitContainerResourceRequestsFamilyGenerator() generator.FamilyGen
)
}

func createPodInitContainerStateStartedFamilyGenerator() generator.FamilyGenerator {
return *generator.NewFamilyGeneratorWithStability(
"kube_pod_init_container_state_started",
"Start time in unix timestamp for a pod init container.",
metric.Gauge,
basemetrics.ALPHA,
"",
wrapPodFunc(func(p *v1.Pod) *metric.Family {
ms := []*metric.Metric{}

for _, cs := range p.Status.InitContainerStatuses {
if cs.State.Running != nil {
ms = append(ms, &metric.Metric{
LabelKeys: []string{"container"},
LabelValues: []string{cs.Name},
Value: float64((cs.State.Running.StartedAt).Unix()),
})
} else if cs.State.Terminated != nil {
ms = append(ms, &metric.Metric{
LabelKeys: []string{"container"},
LabelValues: []string{cs.Name},
Value: float64((cs.State.Terminated.StartedAt).Unix()),
})
}
}

return &metric.Family{
Metrics: ms,
}
}),
)
}

func createPodInitContainerStatusLastTerminatedReasonFamilyGenerator() generator.FamilyGenerator {
return *generator.NewFamilyGeneratorWithStability(
"kube_pod_init_container_status_last_terminated_reason",
Expand All @@ -883,6 +918,32 @@ func createPodInitContainerStatusLastTerminatedReasonFamilyGenerator() generator
)
}

func createPodInitContainerStatusLastTerminatedTimestampFamilyGenerator() generator.FamilyGenerator {
return *generator.NewFamilyGeneratorWithStability(
"kube_pod_init_container_status_last_terminated_timestamp",
"Last terminated time for a pod init container in unix timestamp.",
metric.Gauge,
basemetrics.ALPHA,
"",
wrapPodFunc(func(p *v1.Pod) *metric.Family {
ms := make([]*metric.Metric, 0, len(p.Status.ContainerStatuses))
for _, cs := range p.Status.InitContainerStatuses {
if cs.LastTerminationState.Terminated != nil {
ms = append(ms, &metric.Metric{
LabelKeys: []string{"container"},
LabelValues: []string{cs.Name},
Value: float64(cs.LastTerminationState.Terminated.FinishedAt.Unix()),
})
}
}

return &metric.Family{
Metrics: ms,
}
}),
)
}

func createPodInitContainerStatusReadyFamilyGenerator() generator.FamilyGenerator {
return *generator.NewFamilyGeneratorWithStability(
"kube_pod_init_container_status_ready",
Expand Down
Loading