Skip to content

Commit e3056ae

Browse files
authored
Merge pull request kubernetes#1648 from Serializator/add-kube-horizontalpodautoscaler-info-metric
Add "kube_horizontalpodautoscaler_info" metric
2 parents 840d30c + bb02daf commit e3056ae

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

docs/horizontalpodautoscaler-metrics.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
| Metric name | Metric type | Labels/tags | Status |
44
| -------------------------------- | ----------- | ------------------------------------------------------------- | ------ |
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 |
56
| kube_horizontalpodautoscaler_annotations | Gauge | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; | EXPERIMENTAL |
67
| kube_horizontalpodautoscaler_labels | Gauge | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; | STABLE |
78
| kube_horizontalpodautoscaler_metadata_generation | Gauge | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; | STABLE |

internal/store/horizontalpodautoscaler.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,29 @@ var (
5656

5757
func hpaMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generator.FamilyGenerator {
5858
return []generator.FamilyGenerator{
59+
*generator.NewFamilyGenerator(
60+
"kube_horizontalpodautoscaler_info",
61+
"Information about this autoscaler.",
62+
metric.Gauge,
63+
"",
64+
wrapHPAFunc(func(a *autoscaling.HorizontalPodAutoscaler) *metric.Family {
65+
labelKeys := []string{"scaletargetref_kind", "scaletargetref_name"}
66+
labelValues := []string{a.Spec.ScaleTargetRef.Kind, a.Spec.ScaleTargetRef.Name}
67+
if a.Spec.ScaleTargetRef.APIVersion != "" {
68+
labelKeys = append([]string{"scaletargetref_api_version"}, labelKeys...)
69+
labelValues = append([]string{a.Spec.ScaleTargetRef.APIVersion}, labelValues...)
70+
}
71+
return &metric.Family{
72+
Metrics: []*metric.Metric{
73+
{
74+
LabelKeys: labelKeys,
75+
LabelValues: labelValues,
76+
Value: 1,
77+
},
78+
},
79+
}
80+
}),
81+
),
5982
*generator.NewFamilyGenerator(
6083
"kube_horizontalpodautoscaler_metadata_generation",
6184
"The generation observed by the HorizontalPodAutoscaler controller.",

internal/store/horizontalpodautoscaler_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func TestHPAStore(t *testing.T) {
3535
// Fixed metadata on type and help text. We prepend this to every expected
3636
// output so we only have to modify a single place when doing adjustments.
3737
const metadata = `
38+
# HELP kube_horizontalpodautoscaler_info Information about this autoscaler.
3839
# HELP kube_horizontalpodautoscaler_annotations Kubernetes annotations converted to Prometheus labels.
3940
# HELP kube_horizontalpodautoscaler_labels Kubernetes labels converted to Prometheus labels.
4041
# HELP kube_horizontalpodautoscaler_metadata_generation The generation observed by the HorizontalPodAutoscaler controller.
@@ -44,6 +45,7 @@ func TestHPAStore(t *testing.T) {
4445
# HELP kube_horizontalpodautoscaler_status_condition The condition of this autoscaler.
4546
# HELP kube_horizontalpodautoscaler_status_current_replicas Current number of replicas of pods managed by this autoscaler.
4647
# HELP kube_horizontalpodautoscaler_status_desired_replicas Desired number of replicas of pods managed by this autoscaler.
48+
# TYPE kube_horizontalpodautoscaler_info gauge
4749
# TYPE kube_horizontalpodautoscaler_annotations gauge
4850
# TYPE kube_horizontalpodautoscaler_labels gauge
4951
# TYPE kube_horizontalpodautoscaler_metadata_generation gauge
@@ -183,6 +185,7 @@ func TestHPAStore(t *testing.T) {
183185
},
184186
},
185187
Want: metadata + `
188+
kube_horizontalpodautoscaler_info{horizontalpodautoscaler="hpa1",namespace="ns1",scaletargetref_api_version="apps/v1",scaletargetref_kind="Deployment",scaletargetref_name="deployment1"} 1
186189
kube_horizontalpodautoscaler_annotations{horizontalpodautoscaler="hpa1",namespace="ns1"} 1
187190
kube_horizontalpodautoscaler_labels{horizontalpodautoscaler="hpa1",namespace="ns1"} 1
188191
kube_horizontalpodautoscaler_metadata_generation{horizontalpodautoscaler="hpa1",namespace="ns1"} 2
@@ -203,6 +206,7 @@ func TestHPAStore(t *testing.T) {
203206
kube_horizontalpodautoscaler_status_desired_replicas{horizontalpodautoscaler="hpa1",namespace="ns1"} 2
204207
`,
205208
MetricNames: []string{
209+
"kube_horizontalpodautoscaler_info",
206210
"kube_horizontalpodautoscaler_metadata_generation",
207211
"kube_horizontalpodautoscaler_spec_max_replicas",
208212
"kube_horizontalpodautoscaler_spec_min_replicas",
@@ -278,9 +282,8 @@ func TestHPAStore(t *testing.T) {
278282
},
279283
},
280284
ScaleTargetRef: autoscaling.CrossVersionObjectReference{
281-
APIVersion: "apps/v1",
282-
Kind: "Deployment",
283-
Name: "deployment1",
285+
Kind: "Deployment",
286+
Name: "deployment1",
284287
},
285288
},
286289
Status: autoscaling.HorizontalPodAutoscalerStatus{
@@ -341,6 +344,7 @@ func TestHPAStore(t *testing.T) {
341344
},
342345
},
343346
Want: metadata + `
347+
kube_horizontalpodautoscaler_info{horizontalpodautoscaler="hpa2",namespace="ns1",scaletargetref_kind="Deployment",scaletargetref_name="deployment1"} 1
344348
kube_horizontalpodautoscaler_annotations{annotation_app_k8s_io_owner="@foo",horizontalpodautoscaler="hpa2",namespace="ns1"} 1
345349
kube_horizontalpodautoscaler_labels{horizontalpodautoscaler="hpa2",namespace="ns1"} 1
346350
kube_horizontalpodautoscaler_metadata_generation{horizontalpodautoscaler="hpa2",namespace="ns1"} 2
@@ -357,6 +361,7 @@ func TestHPAStore(t *testing.T) {
357361
kube_horizontalpodautoscaler_status_desired_replicas{horizontalpodautoscaler="hpa2",namespace="ns1"} 2
358362
`,
359363
MetricNames: []string{
364+
"kube_horizontalpodautoscaler_info",
360365
"kube_horizontalpodautoscaler_metadata_generation",
361366
"kube_horizontalpodautoscaler_spec_max_replicas",
362367
"kube_horizontalpodautoscaler_spec_min_replicas",

0 commit comments

Comments
 (0)