Skip to content

Commit 929f4ac

Browse files
authored
Merge pull request #1685 from whitebear009/hpa-metric-type
Change the processing type from int to float in kube_horizontalpodautoscaler_spec_target_metric
2 parents cf19fde + c04de9a commit 929f4ac

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

internal/store/horizontalpodautoscaler.go

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ const (
3636
value metricTargetType = iota
3737
utilization
3838
average
39-
40-
metricTargetTypeCount // Used as a length argument to arrays
4139
)
4240

4341
func (m metricTargetType) String() string {
@@ -134,53 +132,53 @@ func hpaMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generat
134132
for _, m := range a.Spec.Metrics {
135133
var metricName string
136134

137-
var v [metricTargetTypeCount]int64
138-
var ok [metricTargetTypeCount]bool
135+
// The variable maps the type of metric to the corresponding value
136+
metricMap := make(map[metricTargetType]float64)
139137

140138
switch m.Type {
141139
case autoscaling.ObjectMetricSourceType:
142140
metricName = m.Object.Metric.Name
143141

144-
v[value], ok[value] = m.Object.Target.Value.AsInt64()
142+
if m.Object.Target.Value != nil {
143+
metricMap[value] = float64(m.Object.Target.Value.MilliValue()) / 1000
144+
}
145145
if m.Object.Target.AverageValue != nil {
146-
v[average], ok[average] = m.Object.Target.AverageValue.AsInt64()
146+
metricMap[average] = float64(m.Object.Target.AverageValue.MilliValue()) / 1000
147147
}
148148
case autoscaling.PodsMetricSourceType:
149149
metricName = m.Pods.Metric.Name
150150

151-
v[average], ok[average] = m.Pods.Target.AverageValue.AsInt64()
151+
metricMap[average] = float64(m.Pods.Target.AverageValue.MilliValue()) / 1000
152152
case autoscaling.ResourceMetricSourceType:
153153
metricName = string(m.Resource.Name)
154154

155-
if ok[utilization] = (m.Resource.Target.AverageUtilization != nil); ok[utilization] {
156-
v[utilization] = int64(*m.Resource.Target.AverageUtilization)
155+
if m.Resource.Target.AverageUtilization != nil {
156+
metricMap[utilization] = float64(*m.Resource.Target.AverageUtilization)
157157
}
158158

159159
if m.Resource.Target.AverageValue != nil {
160-
v[average], ok[average] = m.Resource.Target.AverageValue.AsInt64()
160+
metricMap[average] = float64(m.Resource.Target.AverageValue.MilliValue()) / 1000
161161
}
162162
case autoscaling.ExternalMetricSourceType:
163163
metricName = m.External.Metric.Name
164164

165165
if m.External.Target.Value != nil {
166-
v[value], ok[value] = m.External.Target.Value.AsInt64()
166+
metricMap[value] = float64(m.External.Target.Value.MilliValue()) / 1000
167167
}
168168
if m.External.Target.AverageValue != nil {
169-
v[average], ok[average] = m.External.Target.AverageValue.AsInt64()
169+
metricMap[average] = float64(m.External.Target.AverageValue.MilliValue()) / 1000
170170
}
171171
default:
172172
// Skip unsupported metric type
173173
continue
174174
}
175175

176-
for i := range ok {
177-
if ok[i] {
178-
ms = append(ms, &metric.Metric{
179-
LabelKeys: targetMetricLabels,
180-
LabelValues: []string{metricName, metricTargetType(i).String()},
181-
Value: float64(v[i]),
182-
})
183-
}
176+
for metricTypeIndex, metricValue := range metricMap {
177+
ms = append(ms, &metric.Metric{
178+
LabelKeys: targetMetricLabels,
179+
LabelValues: []string{metricName, metricTypeIndex.String()},
180+
Value: metricValue,
181+
})
184182
}
185183
}
186184
return &metric.Family{Metrics: ms}

internal/store/horizontalpodautoscaler_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ func TestHPAStore(t *testing.T) {
8484
},
8585
},
8686
},
87+
{
88+
Type: autoscaling.ObjectMetricSourceType,
89+
Object: &autoscaling.ObjectMetricSource{
90+
Metric: autoscaling.MetricIdentifier{
91+
Name: "connections",
92+
},
93+
Target: autoscaling.MetricTarget{
94+
Value: resourcePtr(resource.MustParse("0.5")),
95+
AverageValue: resourcePtr(resource.MustParse("0.7")),
96+
},
97+
},
98+
},
8799
{
88100
Type: autoscaling.PodsMetricSourceType,
89101
Pods: &autoscaling.PodsMetricSource{
@@ -195,6 +207,8 @@ func TestHPAStore(t *testing.T) {
195207
kube_horizontalpodautoscaler_spec_target_metric{horizontalpodautoscaler="hpa1",metric_name="events",metric_target_type="average",namespace="ns1"} 30
196208
kube_horizontalpodautoscaler_spec_target_metric{horizontalpodautoscaler="hpa1",metric_name="hits",metric_target_type="average",namespace="ns1"} 12
197209
kube_horizontalpodautoscaler_spec_target_metric{horizontalpodautoscaler="hpa1",metric_name="hits",metric_target_type="value",namespace="ns1"} 10
210+
kube_horizontalpodautoscaler_spec_target_metric{horizontalpodautoscaler="hpa1",metric_name="connections",metric_target_type="average",namespace="ns1"} 0.7
211+
kube_horizontalpodautoscaler_spec_target_metric{horizontalpodautoscaler="hpa1",metric_name="connections",metric_target_type="value",namespace="ns1"} 0.5
198212
kube_horizontalpodautoscaler_spec_target_metric{horizontalpodautoscaler="hpa1",metric_name="memory",metric_target_type="average",namespace="ns1"} 819200
199213
kube_horizontalpodautoscaler_spec_target_metric{horizontalpodautoscaler="hpa1",metric_name="memory",metric_target_type="utilization",namespace="ns1"} 80
200214
kube_horizontalpodautoscaler_spec_target_metric{horizontalpodautoscaler="hpa1",metric_name="sqs_jobs",metric_target_type="value",namespace="ns1"} 30

0 commit comments

Comments
 (0)