Skip to content

Commit 8fd0417

Browse files
feat: hpa created and deletion metrics
1 parent 28bf0e8 commit 8fd0417

File tree

3 files changed

+84
-17
lines changed

3 files changed

+84
-17
lines changed
Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
# Horizontal Pod Autoscaler Metrics
22

3-
| Metric name | Metric type | Description | Labels/tags | Status |
4-
| ---------------------------------------------------- | ----------- | ------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------ |
5-
| 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 |
3+
| Metric name | Metric type | Description | Labels/tags | Status |
4+
| ---------------------------------------------------- | ----------- | --------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------ |
5+
| 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 |
66
| 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 |
77
| 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 |
8-
| kube_horizontalpodautoscaler_metadata_generation | Gauge | | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; | STABLE |
9-
| kube_horizontalpodautoscaler_spec_max_replicas | Gauge | | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; | STABLE |
10-
| kube_horizontalpodautoscaler_spec_min_replicas | Gauge | | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; | STABLE |
11-
| 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 |
12-
| 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 |
13-
| 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 |
14-
| kube_horizontalpodautoscaler_status_current_replicas | Gauge | | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; | STABLE |
15-
| kube_horizontalpodautoscaler_status_desired_replicas | Gauge | | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; | STABLE |
8+
| kube_horizontalpodautoscaler_metadata_generation | Gauge | | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; | STABLE |
9+
| kube_horizontalpodautoscaler_spec_max_replicas | Gauge | | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; | STABLE |
10+
| kube_horizontalpodautoscaler_spec_min_replicas | Gauge | | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; | STABLE |
11+
| 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 |
12+
| 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 |
13+
| 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 |
14+
| kube_horizontalpodautoscaler_status_current_replicas | Gauge | | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; | STABLE |
15+
| kube_horizontalpodautoscaler_status_desired_replicas | Gauge | | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; | STABLE |
16+
| kube_horizontalpodautoscaler_created | Gauge | | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; | EXPERIMENTAL |
17+
| kube_horizontalpodautoscaler_deletion_timestamp | Gauge | | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; | EXPERIMENTAL |

internal/store/horizontalpodautoscaler.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ func hpaMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generat
6666
createHPAAnnotations(allowAnnotationsList),
6767
createHPALabels(allowLabelsList),
6868
createHPAStatusCondition(),
69+
createHPACreated(),
70+
createHPADeletionTimestamp(),
6971
}
7072
}
7173

@@ -413,3 +415,49 @@ func createHPAStatusCondition() generator.FamilyGenerator {
413415
}),
414416
)
415417
}
418+
419+
func createHPACreated() generator.FamilyGenerator {
420+
return *generator.NewFamilyGeneratorWithStability(
421+
"kube_horizontalpodautoscaler_created",
422+
"Unix creation timestamp",
423+
metric.Gauge,
424+
basemetrics.ALPHA,
425+
"",
426+
wrapHPAFunc(func(a *autoscaling.HorizontalPodAutoscaler) *metric.Family {
427+
ms := []*metric.Metric{}
428+
429+
if !a.CreationTimestamp.IsZero() {
430+
ms = append(ms, &metric.Metric{
431+
Value: float64(a.CreationTimestamp.Unix()),
432+
})
433+
}
434+
435+
return &metric.Family{
436+
Metrics: ms,
437+
}
438+
}),
439+
)
440+
}
441+
442+
func createHPADeletionTimestamp() generator.FamilyGenerator {
443+
return *generator.NewFamilyGeneratorWithStability(
444+
"kube_horizontalpodautoscaler_deletion_timestamp",
445+
"Unix deletion timestamp",
446+
metric.Gauge,
447+
basemetrics.ALPHA,
448+
"",
449+
wrapHPAFunc(func(a *autoscaling.HorizontalPodAutoscaler) *metric.Family {
450+
ms := []*metric.Metric{}
451+
452+
if !a.DeletionTimestamp.IsZero() {
453+
ms = append(ms, &metric.Metric{
454+
Value: float64(a.DeletionTimestamp.Unix()),
455+
})
456+
}
457+
458+
return &metric.Family{
459+
Metrics: ms,
460+
}
461+
}),
462+
)
463+
}

internal/store/horizontalpodautoscaler_test.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package store
1818

1919
import (
2020
"testing"
21+
"time"
2122

2223
autoscaling "k8s.io/api/autoscaling/v2"
2324
v1 "k8s.io/api/core/v1"
@@ -46,6 +47,8 @@ func TestHPAStore(t *testing.T) {
4647
# HELP kube_horizontalpodautoscaler_status_condition [STABLE] The condition of this autoscaler.
4748
# HELP kube_horizontalpodautoscaler_status_current_replicas [STABLE] Current number of replicas of pods managed by this autoscaler.
4849
# HELP kube_horizontalpodautoscaler_status_desired_replicas [STABLE] Desired number of replicas of pods managed by this autoscaler.
50+
# HELP kube_horizontalpodautoscaler_created Unix creation timestamp
51+
# HELP kube_horizontalpodautoscaler_deletion_timestamp Unix deletion timestamp
4952
# TYPE kube_horizontalpodautoscaler_info gauge
5053
# TYPE kube_horizontalpodautoscaler_annotations gauge
5154
# TYPE kube_horizontalpodautoscaler_labels gauge
@@ -57,15 +60,19 @@ func TestHPAStore(t *testing.T) {
5760
# TYPE kube_horizontalpodautoscaler_status_condition gauge
5861
# TYPE kube_horizontalpodautoscaler_status_current_replicas gauge
5962
# TYPE kube_horizontalpodautoscaler_status_desired_replicas gauge
63+
# TYPE kube_horizontalpodautoscaler_created gauge
64+
# TYPE kube_horizontalpodautoscaler_deletion_timestamp gauge
6065
`
6166
cases := []generateMetricsTestCase{
6267
{
6368
// Verify populating base metric.
6469
Obj: &autoscaling.HorizontalPodAutoscaler{
6570
ObjectMeta: metav1.ObjectMeta{
66-
Generation: 2,
67-
Name: "hpa1",
68-
Namespace: "ns1",
71+
Generation: 2,
72+
Name: "hpa1",
73+
CreationTimestamp: metav1.Time{Time: time.Unix(1500000000, 0)},
74+
DeletionTimestamp: &metav1.Time{Time: time.Unix(1800000000, 0)},
75+
Namespace: "ns1",
6976
Labels: map[string]string{
7077
"app": "foobar",
7178
},
@@ -233,6 +240,8 @@ func TestHPAStore(t *testing.T) {
233240
kube_horizontalpodautoscaler_status_condition{condition="AbleToScale",horizontalpodautoscaler="hpa1",namespace="ns1",status="unknown"} 0
234241
kube_horizontalpodautoscaler_status_current_replicas{horizontalpodautoscaler="hpa1",namespace="ns1"} 2
235242
kube_horizontalpodautoscaler_status_desired_replicas{horizontalpodautoscaler="hpa1",namespace="ns1"} 2
243+
kube_horizontalpodautoscaler_created{horizontalpodautoscaler="hpa1",namespace="ns1"} 1.5e+09
244+
kube_horizontalpodautoscaler_deletion_timestamp{horizontalpodautoscaler="hpa1",namespace="ns1"} 1.8e+09
236245
`,
237246
MetricNames: []string{
238247
"kube_horizontalpodautoscaler_info",
@@ -246,6 +255,8 @@ func TestHPAStore(t *testing.T) {
246255
"kube_horizontalpodautoscaler_status_condition",
247256
"kube_horizontalpodautoscaler_annotations",
248257
"kube_horizontalpodautoscaler_labels",
258+
"kube_horizontalpodautoscaler_created",
259+
"kube_horizontalpodautoscaler_deletion_timestamp",
249260
},
250261
},
251262
{
@@ -255,9 +266,11 @@ func TestHPAStore(t *testing.T) {
255266
},
256267
Obj: &autoscaling.HorizontalPodAutoscaler{
257268
ObjectMeta: metav1.ObjectMeta{
258-
Generation: 2,
259-
Name: "hpa2",
260-
Namespace: "ns1",
269+
Generation: 2,
270+
Name: "hpa2",
271+
CreationTimestamp: metav1.Time{Time: time.Unix(1500000000, 0)},
272+
DeletionTimestamp: &metav1.Time{Time: time.Unix(1800000000, 0)},
273+
Namespace: "ns1",
261274
Labels: map[string]string{
262275
"app": "foobar",
263276
},
@@ -408,6 +421,8 @@ func TestHPAStore(t *testing.T) {
408421
kube_horizontalpodautoscaler_status_condition{condition="AbleToScale",horizontalpodautoscaler="hpa2",namespace="ns1",status="unknown"} 0
409422
kube_horizontalpodautoscaler_status_current_replicas{horizontalpodautoscaler="hpa2",namespace="ns1"} 2
410423
kube_horizontalpodautoscaler_status_desired_replicas{horizontalpodautoscaler="hpa2",namespace="ns1"} 2
424+
kube_horizontalpodautoscaler_created{horizontalpodautoscaler="hpa2",namespace="ns1"} 1.5e+09
425+
kube_horizontalpodautoscaler_deletion_timestamp{horizontalpodautoscaler="hpa2",namespace="ns1"} 1.8e+09
411426
`,
412427
MetricNames: []string{
413428
"kube_horizontalpodautoscaler_info",
@@ -421,6 +436,8 @@ func TestHPAStore(t *testing.T) {
421436
"kube_horizontalpodautoscaler_status_condition",
422437
"kube_horizontalpodautoscaler_annotation",
423438
"kube_horizontalpodautoscaler_labels",
439+
"kube_horizontalpodautoscaler_created",
440+
"kube_horizontalpodautoscaler_deletion_timestamp",
424441
},
425442
},
426443
}

0 commit comments

Comments
 (0)