51
51
descHorizontalPodAutoscalerLabelsDefaultLabels = []string {"namespace" , "horizontalpodautoscaler" }
52
52
53
53
targetMetricLabels = []string {"metric_name" , "metric_target_type" }
54
- containerMetricLabels = []string {"metric_name" , "metric_target_type" , "container " }
55
- objectMetricLabels = []string {"metric_name" , "metric_target_type" , "full_target_name " }
54
+ containerMetricLabels = []string {"metric_name" , "metric_target_type" , "container_name " }
55
+ objectMetricLabels = []string {"metric_name" , "metric_target_type" , "target_name " }
56
56
)
57
57
58
58
func hpaMetricFamilies (allowAnnotationsList , allowLabelsList []string ) []generator.FamilyGenerator {
@@ -62,10 +62,10 @@ func hpaMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generat
62
62
createHPASpecMaxReplicas (),
63
63
createHPASpecMinReplicas (),
64
64
createHPASpecTargetContainerMetric (),
65
- createHPASpecTargetObjectMetric (),
66
65
createHPAStatusTargetContainerMetric (),
67
66
createHPAStatusTargetObjectMetric (),
68
- createHPASpecTargetMetric (),
67
+ createHPASpecTargetMetric (true ),
68
+ createHPASpecTargetMetric (false ),
69
69
createHPAStatusTargetMetric (),
70
70
createHPAStatusCurrentReplicas (),
71
71
createHPAStatusDesiredReplicas (),
@@ -236,59 +236,16 @@ func createHPASpecTargetContainerMetric() generator.FamilyGenerator {
236
236
)
237
237
}
238
238
239
- func createHPASpecTargetObjectMetric () generator.FamilyGenerator {
240
- return * generator .NewFamilyGeneratorWithStability (
241
- "kube_horizontalpodautoscaler_spec_target_object_metric" ,
242
- "The object metric specifications used by this autoscaler when calculating the desired replica count." ,
243
- metric .Gauge ,
244
- basemetrics .ALPHA ,
245
- "" ,
246
- wrapHPAFunc (func (a * autoscaling.HorizontalPodAutoscaler ) * metric.Family {
247
- ms := make ([]* metric.Metric , 0 , len (a .Spec .Metrics ))
248
- for _ , m := range a .Spec .Metrics {
249
- var metricName string
250
- var metricTarget autoscaling.MetricTarget
251
- var fullTargetName string
252
- // The variable maps the type of metric to the corresponding value
253
- metricMap := make (map [metricTargetType ]float64 )
254
-
255
- switch m .Type {
256
- case autoscaling .ObjectMetricSourceType :
257
- metricName = m .Object .Metric .Name
258
- metricTarget = m .Object .Target
259
- fullTargetName = m .Object .DescribedObject .Name
260
- default :
261
- // Skip unsupported metric type
262
- continue
263
- }
264
-
265
- if metricTarget .Value != nil {
266
- metricMap [value ] = convertValueToFloat64 (metricTarget .Value )
267
- }
268
- if metricTarget .AverageValue != nil {
269
- metricMap [average ] = convertValueToFloat64 (metricTarget .AverageValue )
270
- }
271
- if metricTarget .AverageUtilization != nil {
272
- metricMap [utilization ] = float64 (* metricTarget .AverageUtilization )
273
- }
274
-
275
- for metricTypeIndex , metricValue := range metricMap {
276
- ms = append (ms , & metric.Metric {
277
- LabelKeys : objectMetricLabels ,
278
- LabelValues : []string {metricName , metricTypeIndex .String (), fullTargetName },
279
- Value : metricValue ,
280
- })
281
- }
282
- }
283
- return & metric.Family {Metrics : ms }
284
- }),
285
- )
286
- }
287
-
288
- func createHPASpecTargetMetric () generator.FamilyGenerator {
239
+ func createHPASpecTargetMetric (CollectContainerResourceMetricSourceType bool ) generator.FamilyGenerator {
240
+ metricName := "kube_horizontalpodautoscaler_spec_target_metric"
241
+ metricDescription := "The metric specifications used by this autoscaler when calculating the desired replica count."
242
+ if CollectContainerResourceMetricSourceType {
243
+ metricName = "kube_horizontalpodautoscaler_spec_target_object_metric"
244
+ metricDescription = "The object metric specifications used by this autoscaler when calculating the desired replica count."
245
+ }
289
246
return * generator .NewFamilyGeneratorWithStability (
290
- "kube_horizontalpodautoscaler_spec_target_metric" ,
291
- "The metric specifications used by this autoscaler when calculating the desired replica count." ,
247
+ metricName ,
248
+ metricDescription ,
292
249
metric .Gauge ,
293
250
basemetrics .ALPHA ,
294
251
"" ,
@@ -299,17 +256,38 @@ func createHPASpecTargetMetric() generator.FamilyGenerator {
299
256
var metricTarget autoscaling.MetricTarget
300
257
// The variable maps the type of metric to the corresponding value
301
258
metricMap := make (map [metricTargetType ]float64 )
259
+ var fullTargetName string
302
260
303
261
switch m .Type {
304
262
case autoscaling .PodsMetricSourceType :
263
+ if CollectContainerResourceMetricSourceType {
264
+ // skip this metric if collecting container resource metric source type
265
+ continue
266
+ }
305
267
metricName = m .Pods .Metric .Name
306
268
metricTarget = m .Pods .Target
307
269
case autoscaling .ResourceMetricSourceType :
270
+ if CollectContainerResourceMetricSourceType {
271
+ // skip this metric if collecting container resource metric source type
272
+ continue
273
+ }
308
274
metricName = string (m .Resource .Name )
309
275
metricTarget = m .Resource .Target
310
276
case autoscaling .ExternalMetricSourceType :
277
+ if CollectContainerResourceMetricSourceType {
278
+ // skip this metric if collecting container resource metric source type
279
+ continue
280
+ }
311
281
metricName = m .External .Metric .Name
312
282
metricTarget = m .External .Target
283
+ case autoscaling .ObjectMetricSourceType :
284
+ if ! CollectContainerResourceMetricSourceType {
285
+ // skip this metric if not collecting container resource metric source type
286
+ continue
287
+ }
288
+ metricName = m .Object .Metric .Name
289
+ metricTarget = m .Object .Target
290
+ fullTargetName = m .Object .DescribedObject .Name
313
291
default :
314
292
// Skip unsupported metric type
315
293
continue
@@ -326,9 +304,18 @@ func createHPASpecTargetMetric() generator.FamilyGenerator {
326
304
}
327
305
328
306
for metricTypeIndex , metricValue := range metricMap {
307
+
308
+ labelValues := []string {metricName , metricTypeIndex .String ()}
309
+ metricLabels := targetMetricLabels
310
+
311
+ // use correct labels and values when collecting container resource metrics
312
+ if m .Type == autoscaling .ObjectMetricSourceType && CollectContainerResourceMetricSourceType {
313
+ labelValues = append (labelValues , fullTargetName )
314
+ metricLabels = objectMetricLabels
315
+ }
329
316
ms = append (ms , & metric.Metric {
330
- LabelKeys : targetMetricLabels ,
331
- LabelValues : [] string { metricName , metricTypeIndex . String ()} ,
317
+ LabelKeys : metricLabels ,
318
+ LabelValues : labelValues ,
332
319
Value : metricValue ,
333
320
})
334
321
}
0 commit comments