Skip to content

Commit 04b7f64

Browse files
feat: add hpa created metric
1 parent 28bf0e8 commit 04b7f64

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

docs/metrics/workload/horizontalpodautoscaler-metrics.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
| 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 |
1414
| kube_horizontalpodautoscaler_status_current_replicas | Gauge | | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; | STABLE |
1515
| 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 |

internal/store/horizontalpodautoscaler.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func hpaMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generat
6666
createHPAAnnotations(allowAnnotationsList),
6767
createHPALabels(allowLabelsList),
6868
createHPAStatusCondition(),
69+
createHPACreated(),
6970
}
7071
}
7172

@@ -413,3 +414,26 @@ func createHPAStatusCondition() generator.FamilyGenerator {
413414
}),
414415
)
415416
}
417+
418+
func createHPACreated() generator.FamilyGenerator {
419+
return *generator.NewFamilyGeneratorWithStability(
420+
"kube_horizontalpodautoscaler_created",
421+
"Unix creation timestamp",
422+
metric.Gauge,
423+
basemetrics.ALPHA,
424+
"",
425+
wrapHPAFunc(func(a *autoscaling.HorizontalPodAutoscaler) *metric.Family {
426+
ms := []*metric.Metric{}
427+
428+
if !a.CreationTimestamp.IsZero() {
429+
ms = append(ms, &metric.Metric{
430+
Value: float64(a.CreationTimestamp.Unix()),
431+
})
432+
}
433+
434+
return &metric.Family{
435+
Metrics: ms,
436+
}
437+
}),
438+
)
439+
}

internal/store/horizontalpodautoscaler_test.go

Lines changed: 15 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,7 @@ 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
4951
# TYPE kube_horizontalpodautoscaler_info gauge
5052
# TYPE kube_horizontalpodautoscaler_annotations gauge
5153
# TYPE kube_horizontalpodautoscaler_labels gauge
@@ -57,15 +59,17 @@ func TestHPAStore(t *testing.T) {
5759
# TYPE kube_horizontalpodautoscaler_status_condition gauge
5860
# TYPE kube_horizontalpodautoscaler_status_current_replicas gauge
5961
# TYPE kube_horizontalpodautoscaler_status_desired_replicas gauge
62+
# TYPE kube_horizontalpodautoscaler_created gauge
6063
`
6164
cases := []generateMetricsTestCase{
6265
{
6366
// Verify populating base metric.
6467
Obj: &autoscaling.HorizontalPodAutoscaler{
6568
ObjectMeta: metav1.ObjectMeta{
66-
Generation: 2,
67-
Name: "hpa1",
68-
Namespace: "ns1",
69+
Generation: 2,
70+
Name: "hpa1",
71+
CreationTimestamp: metav1.Time{Time: time.Unix(1500000000, 0)},
72+
Namespace: "ns1",
6973
Labels: map[string]string{
7074
"app": "foobar",
7175
},
@@ -233,6 +237,7 @@ func TestHPAStore(t *testing.T) {
233237
kube_horizontalpodautoscaler_status_condition{condition="AbleToScale",horizontalpodautoscaler="hpa1",namespace="ns1",status="unknown"} 0
234238
kube_horizontalpodautoscaler_status_current_replicas{horizontalpodautoscaler="hpa1",namespace="ns1"} 2
235239
kube_horizontalpodautoscaler_status_desired_replicas{horizontalpodautoscaler="hpa1",namespace="ns1"} 2
240+
kube_horizontalpodautoscaler_created{horizontalpodautoscaler="hpa1",namespace="ns1"} 1.5e+09
236241
`,
237242
MetricNames: []string{
238243
"kube_horizontalpodautoscaler_info",
@@ -246,6 +251,7 @@ func TestHPAStore(t *testing.T) {
246251
"kube_horizontalpodautoscaler_status_condition",
247252
"kube_horizontalpodautoscaler_annotations",
248253
"kube_horizontalpodautoscaler_labels",
254+
"kube_horizontalpodautoscaler_created",
249255
},
250256
},
251257
{
@@ -255,9 +261,10 @@ func TestHPAStore(t *testing.T) {
255261
},
256262
Obj: &autoscaling.HorizontalPodAutoscaler{
257263
ObjectMeta: metav1.ObjectMeta{
258-
Generation: 2,
259-
Name: "hpa2",
260-
Namespace: "ns1",
264+
Generation: 2,
265+
Name: "hpa2",
266+
CreationTimestamp: metav1.Time{Time: time.Unix(1500000000, 0)},
267+
Namespace: "ns1",
261268
Labels: map[string]string{
262269
"app": "foobar",
263270
},
@@ -408,6 +415,7 @@ func TestHPAStore(t *testing.T) {
408415
kube_horizontalpodautoscaler_status_condition{condition="AbleToScale",horizontalpodautoscaler="hpa2",namespace="ns1",status="unknown"} 0
409416
kube_horizontalpodautoscaler_status_current_replicas{horizontalpodautoscaler="hpa2",namespace="ns1"} 2
410417
kube_horizontalpodautoscaler_status_desired_replicas{horizontalpodautoscaler="hpa2",namespace="ns1"} 2
418+
kube_horizontalpodautoscaler_created{horizontalpodautoscaler="hpa2",namespace="ns1"} 1.5e+09
411419
`,
412420
MetricNames: []string{
413421
"kube_horizontalpodautoscaler_info",
@@ -421,6 +429,7 @@ func TestHPAStore(t *testing.T) {
421429
"kube_horizontalpodautoscaler_status_condition",
422430
"kube_horizontalpodautoscaler_annotation",
423431
"kube_horizontalpodautoscaler_labels",
432+
"kube_horizontalpodautoscaler_created",
424433
},
425434
},
426435
}

0 commit comments

Comments
 (0)