Skip to content

Commit 2f9d426

Browse files
committed
fix(horizontalpodautoscaler): add selector labels where applicable for hpa spec|status target metrics
1 parent c853e41 commit 2f9d426

File tree

3 files changed

+70
-23
lines changed

3 files changed

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

3-
| Metric name | Metric type | Description | Labels/tags | Status |
4-
| ---------------------------------------------------- | ----------- | ------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------ |
3+
| Metric name | Metric type | Description | Labels/tags | Status |
4+
| ---------------------------------------------------- | ----------- | ------------------------------------------------------------------------------------------------------------------------- |---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| ------------ |
55
| 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 |
6-
| 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 |
7-
| 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 |
6+
| 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 |
7+
| 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 <br> `selectorLabel.$labelKey`=&lt;labelValue&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<br> `selectorLabel.$labelKey`=&lt;labelValue&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 |

internal/store/horizontalpodautoscaler.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,22 @@ func createHPASpecTargetMetric() generator.FamilyGenerator {
194194
var metricTarget autoscaling.MetricTarget
195195
// The variable maps the type of metric to the corresponding value
196196
metricMap := make(map[metricTargetType]float64)
197+
// Object|Pods|External metrics can use the same name for mutltiple metrics. Add SelectorLabels to the metric labels
198+
selectorLabels := make(map[string]string)
197199

198200
switch m.Type {
199201
case autoscaling.ObjectMetricSourceType:
200202
metricName = m.Object.Metric.Name
201203
metricTarget = m.Object.Target
204+
if m.Object.Metric.Selector != nil {
205+
selectorLabels = m.Object.Metric.Selector.MatchLabels
206+
}
202207
case autoscaling.PodsMetricSourceType:
203208
metricName = m.Pods.Metric.Name
204209
metricTarget = m.Pods.Target
210+
if m.Pods.Metric.Selector != nil {
211+
selectorLabels = m.Pods.Metric.Selector.MatchLabels
212+
}
205213
case autoscaling.ResourceMetricSourceType:
206214
metricName = string(m.Resource.Name)
207215
metricTarget = m.Resource.Target
@@ -211,6 +219,9 @@ func createHPASpecTargetMetric() generator.FamilyGenerator {
211219
case autoscaling.ExternalMetricSourceType:
212220
metricName = m.External.Metric.Name
213221
metricTarget = m.External.Target
222+
if m.External.Metric.Selector != nil {
223+
selectorLabels = m.External.Metric.Selector.MatchLabels
224+
}
214225
default:
215226
// Skip unsupported metric type
216227
continue
@@ -227,9 +238,10 @@ func createHPASpecTargetMetric() generator.FamilyGenerator {
227238
}
228239

229240
for metricTypeIndex, metricValue := range metricMap {
241+
selectorLabelKeys, selectorLabelValues := kubeMapToPrometheusLabels("selectorlabel", selectorLabels)
230242
ms = append(ms, &metric.Metric{
231-
LabelKeys: targetMetricLabels,
232-
LabelValues: []string{metricName, metricTypeIndex.String()},
243+
LabelKeys: append(targetMetricLabels, selectorLabelKeys...),
244+
LabelValues: append([]string{metricName, metricTypeIndex.String()}, selectorLabelValues...),
233245
Value: metricValue,
234246
})
235247
}
@@ -253,14 +265,22 @@ func createHPAStatusTargetMetric() generator.FamilyGenerator {
253265
var currentMetric autoscaling.MetricValueStatus
254266
// The variable maps the type of metric to the corresponding value
255267
metricMap := make(map[metricTargetType]float64)
268+
// Object|Pods|External metrics can use the same name for mutltiple metrics. Add SelectorLabels to the metric labels
269+
selectorLabels := make(map[string]string)
256270

257271
switch m.Type {
258272
case autoscaling.ObjectMetricSourceType:
259273
metricName = m.Object.Metric.Name
260274
currentMetric = m.Object.Current
275+
if m.Object.Metric.Selector != nil {
276+
selectorLabels = m.Object.Metric.Selector.MatchLabels
277+
}
261278
case autoscaling.PodsMetricSourceType:
262279
metricName = m.Pods.Metric.Name
263280
currentMetric = m.Pods.Current
281+
if m.Pods.Metric.Selector != nil {
282+
selectorLabels = m.Pods.Metric.Selector.MatchLabels
283+
}
264284
case autoscaling.ResourceMetricSourceType:
265285
metricName = string(m.Resource.Name)
266286
currentMetric = m.Resource.Current
@@ -270,6 +290,9 @@ func createHPAStatusTargetMetric() generator.FamilyGenerator {
270290
case autoscaling.ExternalMetricSourceType:
271291
metricName = m.External.Metric.Name
272292
currentMetric = m.External.Current
293+
if m.External.Metric.Selector != nil {
294+
selectorLabels = m.External.Metric.Selector.MatchLabels
295+
}
273296
default:
274297
// Skip unsupported metric type
275298
continue
@@ -286,9 +309,10 @@ func createHPAStatusTargetMetric() generator.FamilyGenerator {
286309
}
287310

288311
for metricTypeIndex, metricValue := range metricMap {
312+
selectorLabelKeys, selectorLabelValues := kubeMapToPrometheusLabels("selectorlabel", selectorLabels)
289313
ms = append(ms, &metric.Metric{
290-
LabelKeys: targetMetricLabels,
291-
LabelValues: []string{metricName, metricTypeIndex.String()},
314+
LabelKeys: append(targetMetricLabels, selectorLabelKeys...),
315+
LabelValues: append([]string{metricName, metricTypeIndex.String()}, selectorLabelValues...),
292316
Value: metricValue,
293317
})
294318
}

0 commit comments

Comments
 (0)