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

Add "kube_horizontalpodautoscaler_info" metric #1648

Merged
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
1 change: 1 addition & 0 deletions docs/horizontalpodautoscaler-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

| Metric name | Metric type | 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 | `horizontalpodautoscaler`=&lt;hpa-name&gt; <br> `namespace`=&lt;hpa-namespace&gt; | EXPERIMENTAL |
| kube_horizontalpodautoscaler_labels | 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 |
Expand Down
23 changes: 23 additions & 0 deletions internal/store/horizontalpodautoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,29 @@ var (

func hpaMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generator.FamilyGenerator {
return []generator.FamilyGenerator{
*generator.NewFamilyGenerator(
"kube_horizontalpodautoscaler_info",
"Information about this autoscaler.",
metric.Gauge,
"",
wrapHPAFunc(func(a *autoscaling.HorizontalPodAutoscaler) *metric.Family {
labelKeys := []string{"scaletargetref_kind", "scaletargetref_name"}
labelValues := []string{a.Spec.ScaleTargetRef.Kind, a.Spec.ScaleTargetRef.Name}
if a.Spec.ScaleTargetRef.APIVersion != "" {
labelKeys = append([]string{"scaletargetref_api_version"}, labelKeys...)
labelValues = append([]string{a.Spec.ScaleTargetRef.APIVersion}, labelValues...)
}
return &metric.Family{
Metrics: []*metric.Metric{
{
LabelKeys: labelKeys,
LabelValues: labelValues,
Value: 1,
},
},
}
}),
),
*generator.NewFamilyGenerator(
"kube_horizontalpodautoscaler_metadata_generation",
"The generation observed by the HorizontalPodAutoscaler controller.",
Expand Down
11 changes: 8 additions & 3 deletions internal/store/horizontalpodautoscaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func TestHPAStore(t *testing.T) {
// Fixed metadata on type and help text. We prepend this to every expected
// output so we only have to modify a single place when doing adjustments.
const metadata = `
# HELP kube_horizontalpodautoscaler_info Information about this autoscaler.
# HELP kube_horizontalpodautoscaler_annotations Kubernetes annotations converted to Prometheus labels.
# HELP kube_horizontalpodautoscaler_labels Kubernetes labels converted to Prometheus labels.
# HELP kube_horizontalpodautoscaler_metadata_generation The generation observed by the HorizontalPodAutoscaler controller.
Expand All @@ -44,6 +45,7 @@ func TestHPAStore(t *testing.T) {
# HELP kube_horizontalpodautoscaler_status_condition The condition of this autoscaler.
# HELP kube_horizontalpodautoscaler_status_current_replicas Current number of replicas of pods managed by this autoscaler.
# HELP kube_horizontalpodautoscaler_status_desired_replicas Desired number of replicas of pods managed by this autoscaler.
# TYPE kube_horizontalpodautoscaler_info gauge
# TYPE kube_horizontalpodautoscaler_annotations gauge
# TYPE kube_horizontalpodautoscaler_labels gauge
# TYPE kube_horizontalpodautoscaler_metadata_generation gauge
Expand Down Expand Up @@ -183,6 +185,7 @@ func TestHPAStore(t *testing.T) {
},
},
Want: metadata + `
kube_horizontalpodautoscaler_info{horizontalpodautoscaler="hpa1",namespace="ns1",scaletargetref_api_version="apps/v1",scaletargetref_kind="Deployment",scaletargetref_name="deployment1"} 1
kube_horizontalpodautoscaler_annotations{horizontalpodautoscaler="hpa1",namespace="ns1"} 1
kube_horizontalpodautoscaler_labels{horizontalpodautoscaler="hpa1",namespace="ns1"} 1
kube_horizontalpodautoscaler_metadata_generation{horizontalpodautoscaler="hpa1",namespace="ns1"} 2
Expand All @@ -203,6 +206,7 @@ func TestHPAStore(t *testing.T) {
kube_horizontalpodautoscaler_status_desired_replicas{horizontalpodautoscaler="hpa1",namespace="ns1"} 2
`,
MetricNames: []string{
"kube_horizontalpodautoscaler_info",
"kube_horizontalpodautoscaler_metadata_generation",
"kube_horizontalpodautoscaler_spec_max_replicas",
"kube_horizontalpodautoscaler_spec_min_replicas",
Expand Down Expand Up @@ -278,9 +282,8 @@ func TestHPAStore(t *testing.T) {
},
},
ScaleTargetRef: autoscaling.CrossVersionObjectReference{
APIVersion: "apps/v1",
Kind: "Deployment",
Name: "deployment1",
Kind: "Deployment",
Name: "deployment1",
},
},
Status: autoscaling.HorizontalPodAutoscalerStatus{
Expand Down Expand Up @@ -341,6 +344,7 @@ func TestHPAStore(t *testing.T) {
},
},
Want: metadata + `
kube_horizontalpodautoscaler_info{horizontalpodautoscaler="hpa2",namespace="ns1",scaletargetref_kind="Deployment",scaletargetref_name="deployment1"} 1
kube_horizontalpodautoscaler_annotations{annotation_app_k8s_io_owner="@foo",horizontalpodautoscaler="hpa2",namespace="ns1"} 1
kube_horizontalpodautoscaler_labels{horizontalpodautoscaler="hpa2",namespace="ns1"} 1
kube_horizontalpodautoscaler_metadata_generation{horizontalpodautoscaler="hpa2",namespace="ns1"} 2
Expand All @@ -357,6 +361,7 @@ func TestHPAStore(t *testing.T) {
kube_horizontalpodautoscaler_status_desired_replicas{horizontalpodautoscaler="hpa2",namespace="ns1"} 2
`,
MetricNames: []string{
"kube_horizontalpodautoscaler_info",
"kube_horizontalpodautoscaler_metadata_generation",
"kube_horizontalpodautoscaler_spec_max_replicas",
"kube_horizontalpodautoscaler_spec_min_replicas",
Expand Down