Skip to content

Commit 967eb0e

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

File tree

3 files changed

+77
-23
lines changed

3 files changed

+77
-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: 38 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,15 @@ func createHPASpecTargetMetric() generator.FamilyGenerator {
227238
}
228239

229240
for metricTypeIndex, metricValue := range metricMap {
241+
labelKeys := targetMetricLabels
242+
labelValues := []string{metricName, metricTypeIndex.String()}
243+
for k, v := range selectorLabels {
244+
labelKeys = append(labelKeys, "selectorlabel."+k)
245+
labelValues = append(labelValues, v)
246+
}
230247
ms = append(ms, &metric.Metric{
231-
LabelKeys: targetMetricLabels,
232-
LabelValues: []string{metricName, metricTypeIndex.String()},
248+
LabelKeys: labelKeys,
249+
LabelValues: labelValues,
233250
Value: metricValue,
234251
})
235252
}
@@ -253,14 +270,22 @@ func createHPAStatusTargetMetric() generator.FamilyGenerator {
253270
var currentMetric autoscaling.MetricValueStatus
254271
// The variable maps the type of metric to the corresponding value
255272
metricMap := make(map[metricTargetType]float64)
273+
// Object|Pods|External metrics can use the same name for mutltiple metrics. Add SelectorLabels to the metric labels
274+
selectorLabels := make(map[string]string)
256275

257276
switch m.Type {
258277
case autoscaling.ObjectMetricSourceType:
259278
metricName = m.Object.Metric.Name
260279
currentMetric = m.Object.Current
280+
if m.Object.Metric.Selector != nil {
281+
selectorLabels = m.Object.Metric.Selector.MatchLabels
282+
}
261283
case autoscaling.PodsMetricSourceType:
262284
metricName = m.Pods.Metric.Name
263285
currentMetric = m.Pods.Current
286+
if m.Pods.Metric.Selector != nil {
287+
selectorLabels = m.Pods.Metric.Selector.MatchLabels
288+
}
264289
case autoscaling.ResourceMetricSourceType:
265290
metricName = string(m.Resource.Name)
266291
currentMetric = m.Resource.Current
@@ -270,6 +295,9 @@ func createHPAStatusTargetMetric() generator.FamilyGenerator {
270295
case autoscaling.ExternalMetricSourceType:
271296
metricName = m.External.Metric.Name
272297
currentMetric = m.External.Current
298+
if m.External.Metric.Selector != nil {
299+
selectorLabels = m.External.Metric.Selector.MatchLabels
300+
}
273301
default:
274302
// Skip unsupported metric type
275303
continue
@@ -286,9 +314,15 @@ func createHPAStatusTargetMetric() generator.FamilyGenerator {
286314
}
287315

288316
for metricTypeIndex, metricValue := range metricMap {
317+
labelKeys := targetMetricLabels
318+
labelValues := []string{metricName, metricTypeIndex.String()}
319+
for k, v := range selectorLabels {
320+
labelKeys = append(labelKeys, "selectorlabel."+k)
321+
labelValues = append(labelValues, v)
322+
}
289323
ms = append(ms, &metric.Metric{
290-
LabelKeys: targetMetricLabels,
291-
LabelValues: []string{metricName, metricTypeIndex.String()},
324+
LabelKeys: labelKeys,
325+
LabelValues: labelValues,
292326
Value: metricValue,
293327
})
294328
}

0 commit comments

Comments
 (0)