Skip to content

Commit

Permalink
Merge pull request #1685 from whitebear009/hpa-metric-type
Browse files Browse the repository at this point in the history
Change the processing type from int to float in kube_horizontalpodautoscaler_spec_target_metric
  • Loading branch information
k8s-ci-robot committed Feb 18, 2022
2 parents cf19fde + c04de9a commit 929f4ac
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
38 changes: 18 additions & 20 deletions internal/store/horizontalpodautoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ const (
value metricTargetType = iota
utilization
average

metricTargetTypeCount // Used as a length argument to arrays
)

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

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

switch m.Type {
case autoscaling.ObjectMetricSourceType:
metricName = m.Object.Metric.Name

v[value], ok[value] = m.Object.Target.Value.AsInt64()
if m.Object.Target.Value != nil {
metricMap[value] = float64(m.Object.Target.Value.MilliValue()) / 1000
}
if m.Object.Target.AverageValue != nil {
v[average], ok[average] = m.Object.Target.AverageValue.AsInt64()
metricMap[average] = float64(m.Object.Target.AverageValue.MilliValue()) / 1000
}
case autoscaling.PodsMetricSourceType:
metricName = m.Pods.Metric.Name

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

if ok[utilization] = (m.Resource.Target.AverageUtilization != nil); ok[utilization] {
v[utilization] = int64(*m.Resource.Target.AverageUtilization)
if m.Resource.Target.AverageUtilization != nil {
metricMap[utilization] = float64(*m.Resource.Target.AverageUtilization)
}

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

if m.External.Target.Value != nil {
v[value], ok[value] = m.External.Target.Value.AsInt64()
metricMap[value] = float64(m.External.Target.Value.MilliValue()) / 1000
}
if m.External.Target.AverageValue != nil {
v[average], ok[average] = m.External.Target.AverageValue.AsInt64()
metricMap[average] = float64(m.External.Target.AverageValue.MilliValue()) / 1000
}
default:
// Skip unsupported metric type
continue
}

for i := range ok {
if ok[i] {
ms = append(ms, &metric.Metric{
LabelKeys: targetMetricLabels,
LabelValues: []string{metricName, metricTargetType(i).String()},
Value: float64(v[i]),
})
}
for metricTypeIndex, metricValue := range metricMap {
ms = append(ms, &metric.Metric{
LabelKeys: targetMetricLabels,
LabelValues: []string{metricName, metricTypeIndex.String()},
Value: metricValue,
})
}
}
return &metric.Family{Metrics: ms}
Expand Down
14 changes: 14 additions & 0 deletions internal/store/horizontalpodautoscaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ func TestHPAStore(t *testing.T) {
},
},
},
{
Type: autoscaling.ObjectMetricSourceType,
Object: &autoscaling.ObjectMetricSource{
Metric: autoscaling.MetricIdentifier{
Name: "connections",
},
Target: autoscaling.MetricTarget{
Value: resourcePtr(resource.MustParse("0.5")),
AverageValue: resourcePtr(resource.MustParse("0.7")),
},
},
},
{
Type: autoscaling.PodsMetricSourceType,
Pods: &autoscaling.PodsMetricSource{
Expand Down Expand Up @@ -195,6 +207,8 @@ func TestHPAStore(t *testing.T) {
kube_horizontalpodautoscaler_spec_target_metric{horizontalpodautoscaler="hpa1",metric_name="events",metric_target_type="average",namespace="ns1"} 30
kube_horizontalpodautoscaler_spec_target_metric{horizontalpodautoscaler="hpa1",metric_name="hits",metric_target_type="average",namespace="ns1"} 12
kube_horizontalpodautoscaler_spec_target_metric{horizontalpodautoscaler="hpa1",metric_name="hits",metric_target_type="value",namespace="ns1"} 10
kube_horizontalpodautoscaler_spec_target_metric{horizontalpodautoscaler="hpa1",metric_name="connections",metric_target_type="average",namespace="ns1"} 0.7
kube_horizontalpodautoscaler_spec_target_metric{horizontalpodautoscaler="hpa1",metric_name="connections",metric_target_type="value",namespace="ns1"} 0.5
kube_horizontalpodautoscaler_spec_target_metric{horizontalpodautoscaler="hpa1",metric_name="memory",metric_target_type="average",namespace="ns1"} 819200
kube_horizontalpodautoscaler_spec_target_metric{horizontalpodautoscaler="hpa1",metric_name="memory",metric_target_type="utilization",namespace="ns1"} 80
kube_horizontalpodautoscaler_spec_target_metric{horizontalpodautoscaler="hpa1",metric_name="sqs_jobs",metric_target_type="value",namespace="ns1"} 30
Expand Down

0 comments on commit 929f4ac

Please sign in to comment.