Skip to content

feat: Add HorizontalPodAutoscaler created and deletionTimestamp info into metrics #2675

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

Open
wants to merge 1 commit 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
24 changes: 13 additions & 11 deletions docs/metrics/workload/horizontalpodautoscaler-metrics.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# Horizontal Pod Autoscaler Metrics

| Metric name | Metric type | Description | Labels/tags | Status |
| ---------------------------------------------------- | ----------- | ------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------ |
| kube_horizontalpodautoscaler_info | Gauge | | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; <br> `scaletargetref_api_version`=&lt;hpa-target-api-version&gt; <br> `scaletargetref_kind`=&lt;hpa-target-kind&gt; <br> `scaletargetref_name`=&lt;hpa-target-name&gt; | EXPERIMENTAL |
| Metric name | Metric type | Description | Labels/tags | Status |
| ---------------------------------------------------- | ----------- | --------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------ |
| kube_horizontalpodautoscaler_info | Gauge | | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; <br> `scaletargetref_api_version`=&lt;hpa-target-api-version&gt; <br> `scaletargetref_kind`=&lt;hpa-target-kind&gt; <br> `scaletargetref_name`=&lt;hpa-target-name&gt; | EXPERIMENTAL |
| kube_horizontalpodautoscaler_annotations | Gauge | Kubernetes annotations converted to Prometheus labels controlled via [--metric-annotations-allowlist](../../developer/cli-arguments.md) | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; | EXPERIMENTAL |
| kube_horizontalpodautoscaler_labels | Gauge | Kubernetes labels converted to Prometheus labels controlled via [--metric-labels-allowlist](../../developer/cli-arguments.md) | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; | STABLE |
| kube_horizontalpodautoscaler_metadata_generation | Gauge | | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; | STABLE |
| kube_horizontalpodautoscaler_spec_max_replicas | Gauge | | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; | STABLE |
| kube_horizontalpodautoscaler_spec_min_replicas | Gauge | | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; | STABLE |
| kube_horizontalpodautoscaler_spec_target_metric | Gauge | | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; <br> `metric_name`=&lt;metric-name&gt; <br> `metric_target_type`=&lt;value\|utilization\|average&gt; | EXPERIMENTAL |
| kube_horizontalpodautoscaler_status_target_metric | Gauge | | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; <br> `metric_name`=&lt;metric-name&gt; <br> `metric_target_type`=&lt;value\|utilization\|average&gt; | EXPERIMENTAL |
| kube_horizontalpodautoscaler_status_condition | Gauge | | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; <br> `condition`=&lt;hpa-condition&gt; <br> `status`=&lt;true\|false\|unknown&gt; | STABLE |
| kube_horizontalpodautoscaler_status_current_replicas | Gauge | | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; | STABLE |
| kube_horizontalpodautoscaler_status_desired_replicas | Gauge | | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; | STABLE |
| kube_horizontalpodautoscaler_metadata_generation | Gauge | | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; | STABLE |
| kube_horizontalpodautoscaler_spec_max_replicas | Gauge | | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; | STABLE |
| kube_horizontalpodautoscaler_spec_min_replicas | Gauge | | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; | STABLE |
| kube_horizontalpodautoscaler_spec_target_metric | Gauge | | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; <br> `metric_name`=&lt;metric-name&gt; <br> `metric_target_type`=&lt;value\|utilization\|average&gt; | EXPERIMENTAL |
| kube_horizontalpodautoscaler_status_target_metric | Gauge | | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; <br> `metric_name`=&lt;metric-name&gt; <br> `metric_target_type`=&lt;value\|utilization\|average&gt; | EXPERIMENTAL |
| kube_horizontalpodautoscaler_status_condition | Gauge | | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; <br> `condition`=&lt;hpa-condition&gt; <br> `status`=&lt;true\|false\|unknown&gt; | STABLE |
| kube_horizontalpodautoscaler_status_current_replicas | Gauge | | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; | STABLE |
| kube_horizontalpodautoscaler_status_desired_replicas | Gauge | | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; | STABLE |
| kube_horizontalpodautoscaler_created | Gauge | | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; | EXPERIMENTAL |
| kube_horizontalpodautoscaler_deletion_timestamp | Gauge | | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; | EXPERIMENTAL |
48 changes: 48 additions & 0 deletions internal/store/horizontalpodautoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func hpaMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generat
createHPAAnnotations(allowAnnotationsList),
createHPALabels(allowLabelsList),
createHPAStatusCondition(),
createHPACreated(),
createHPADeletionTimestamp(),
}
}

Expand Down Expand Up @@ -413,3 +415,49 @@ func createHPAStatusCondition() generator.FamilyGenerator {
}),
)
}

func createHPACreated() generator.FamilyGenerator {
return *generator.NewFamilyGeneratorWithStability(
"kube_horizontalpodautoscaler_created",
"Unix creation timestamp",
metric.Gauge,
basemetrics.ALPHA,
"",
wrapHPAFunc(func(a *autoscaling.HorizontalPodAutoscaler) *metric.Family {
ms := []*metric.Metric{}

if !a.CreationTimestamp.IsZero() {
ms = append(ms, &metric.Metric{
Value: float64(a.CreationTimestamp.Unix()),
})
}

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

func createHPADeletionTimestamp() generator.FamilyGenerator {
return *generator.NewFamilyGeneratorWithStability(
"kube_horizontalpodautoscaler_deletion_timestamp",
"Unix deletion timestamp",
metric.Gauge,
basemetrics.ALPHA,
"",
wrapHPAFunc(func(a *autoscaling.HorizontalPodAutoscaler) *metric.Family {
ms := []*metric.Metric{}

if !a.DeletionTimestamp.IsZero() {
ms = append(ms, &metric.Metric{
Value: float64(a.DeletionTimestamp.Unix()),
})
}

return &metric.Family{
Metrics: ms,
}
}),
)
}
29 changes: 23 additions & 6 deletions internal/store/horizontalpodautoscaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package store

import (
"testing"
"time"

autoscaling "k8s.io/api/autoscaling/v2"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -46,6 +47,8 @@ func TestHPAStore(t *testing.T) {
# HELP kube_horizontalpodautoscaler_status_condition [STABLE] The condition of this autoscaler.
# HELP kube_horizontalpodautoscaler_status_current_replicas [STABLE] Current number of replicas of pods managed by this autoscaler.
# HELP kube_horizontalpodautoscaler_status_desired_replicas [STABLE] Desired number of replicas of pods managed by this autoscaler.
# HELP kube_horizontalpodautoscaler_created Unix creation timestamp
# HELP kube_horizontalpodautoscaler_deletion_timestamp Unix deletion timestamp
# TYPE kube_horizontalpodautoscaler_info gauge
# TYPE kube_horizontalpodautoscaler_annotations gauge
# TYPE kube_horizontalpodautoscaler_labels gauge
Expand All @@ -57,15 +60,19 @@ func TestHPAStore(t *testing.T) {
# TYPE kube_horizontalpodautoscaler_status_condition gauge
# TYPE kube_horizontalpodautoscaler_status_current_replicas gauge
# TYPE kube_horizontalpodautoscaler_status_desired_replicas gauge
# TYPE kube_horizontalpodautoscaler_created gauge
# TYPE kube_horizontalpodautoscaler_deletion_timestamp gauge
`
cases := []generateMetricsTestCase{
{
// Verify populating base metric.
Obj: &autoscaling.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Generation: 2,
Name: "hpa1",
Namespace: "ns1",
Generation: 2,
Name: "hpa1",
CreationTimestamp: metav1.Time{Time: time.Unix(1500000000, 0)},
DeletionTimestamp: &metav1.Time{Time: time.Unix(1800000000, 0)},
Namespace: "ns1",
Labels: map[string]string{
"app": "foobar",
},
Expand Down Expand Up @@ -233,6 +240,8 @@ func TestHPAStore(t *testing.T) {
kube_horizontalpodautoscaler_status_condition{condition="AbleToScale",horizontalpodautoscaler="hpa1",namespace="ns1",status="unknown"} 0
kube_horizontalpodautoscaler_status_current_replicas{horizontalpodautoscaler="hpa1",namespace="ns1"} 2
kube_horizontalpodautoscaler_status_desired_replicas{horizontalpodautoscaler="hpa1",namespace="ns1"} 2
kube_horizontalpodautoscaler_created{horizontalpodautoscaler="hpa1",namespace="ns1"} 1.5e+09
kube_horizontalpodautoscaler_deletion_timestamp{horizontalpodautoscaler="hpa1",namespace="ns1"} 1.8e+09
`,
MetricNames: []string{
"kube_horizontalpodautoscaler_info",
Expand All @@ -246,6 +255,8 @@ func TestHPAStore(t *testing.T) {
"kube_horizontalpodautoscaler_status_condition",
"kube_horizontalpodautoscaler_annotations",
"kube_horizontalpodautoscaler_labels",
"kube_horizontalpodautoscaler_created",
"kube_horizontalpodautoscaler_deletion_timestamp",
},
},
{
Expand All @@ -255,9 +266,11 @@ func TestHPAStore(t *testing.T) {
},
Obj: &autoscaling.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Generation: 2,
Name: "hpa2",
Namespace: "ns1",
Generation: 2,
Name: "hpa2",
CreationTimestamp: metav1.Time{Time: time.Unix(1500000000, 0)},
DeletionTimestamp: &metav1.Time{Time: time.Unix(1800000000, 0)},
Namespace: "ns1",
Labels: map[string]string{
"app": "foobar",
},
Expand Down Expand Up @@ -408,6 +421,8 @@ func TestHPAStore(t *testing.T) {
kube_horizontalpodautoscaler_status_condition{condition="AbleToScale",horizontalpodautoscaler="hpa2",namespace="ns1",status="unknown"} 0
kube_horizontalpodautoscaler_status_current_replicas{horizontalpodautoscaler="hpa2",namespace="ns1"} 2
kube_horizontalpodautoscaler_status_desired_replicas{horizontalpodautoscaler="hpa2",namespace="ns1"} 2
kube_horizontalpodautoscaler_created{horizontalpodautoscaler="hpa2",namespace="ns1"} 1.5e+09
kube_horizontalpodautoscaler_deletion_timestamp{horizontalpodautoscaler="hpa2",namespace="ns1"} 1.8e+09
`,
MetricNames: []string{
"kube_horizontalpodautoscaler_info",
Expand All @@ -421,6 +436,8 @@ func TestHPAStore(t *testing.T) {
"kube_horizontalpodautoscaler_status_condition",
"kube_horizontalpodautoscaler_annotation",
"kube_horizontalpodautoscaler_labels",
"kube_horizontalpodautoscaler_created",
"kube_horizontalpodautoscaler_deletion_timestamp",
},
},
}
Expand Down