Handle 0 values in max collector and unknown metric type

This commit is contained in:
Arjun Naik 2019-01-10 11:23:31 +01:00
parent 37003d6513
commit 417f650caa
3 changed files with 19 additions and 8 deletions

View File

@ -24,7 +24,7 @@ func NewWeightedMaxCollector(interval time.Duration, weight float64, collectors
// GetMetrics gets metrics from all collectors and return the higest value. // GetMetrics gets metrics from all collectors and return the higest value.
func (c *WeightedMaxCollector) GetMetrics() ([]CollectedMetric, error) { func (c *WeightedMaxCollector) GetMetrics() ([]CollectedMetric, error) {
var max CollectedMetric var max *CollectedMetric
for _, collector := range c.collectors { for _, collector := range c.collectors {
values, err := collector.GetMetrics() values, err := collector.GetMetrics()
if err != nil { if err != nil {
@ -32,14 +32,19 @@ func (c *WeightedMaxCollector) GetMetrics() ([]CollectedMetric, error) {
} }
for _, value := range values { for _, value := range values {
if value.Custom.Value.MilliValue() > max.Custom.Value.MilliValue() { if max != nil {
max = value if value.Custom.Value.MilliValue() > max.Custom.Value.MilliValue() {
max = &value
}
} else {
max = &value
} }
} }
} }
max.Custom.Value = *resource.NewMilliQuantity(int64(float64(max.Custom.Value.MilliValue())*c.weight), resource.DecimalSI) max.Custom.Value = *resource.NewMilliQuantity(int64(float64(max.Custom.Value.MilliValue())*c.weight), resource.DecimalSI)
return []CollectedMetric{max}, nil
return []CollectedMetric{*max}, nil
} }
// Interval returns the interval at which the collector should run. // Interval returns the interval at which the collector should run.

View File

@ -103,7 +103,10 @@ func (c *SkipperCollector) getCollector() (Collector, error) {
return nil, err return nil, err
} }
backendWeight := getMaxWeights(ingress.Annotations, c.config.MetricLabels.MatchLabels[hostLabel]) backendWeight := 1.0
if c.config.MetricLabels != nil {
backendWeight = getMaxWeights(ingress.Annotations, c.config.MetricLabels.MatchLabels[hostLabel])
}
config := c.config config := c.config
@ -147,7 +150,6 @@ func (c *SkipperCollector) GetMetrics() ([]CollectedMetric, error) {
if len(values) != 1 { if len(values) != 1 {
return nil, fmt.Errorf("expected to only get one metric value, got %d", len(values)) return nil, fmt.Errorf("expected to only get one metric value, got %d", len(values))
} }
value := values[0] value := values[0]
return []CollectedMetric{value}, nil return []CollectedMetric{value}, nil
} }

View File

@ -2,6 +2,7 @@ package provider
import ( import (
"fmt" "fmt"
"github.com/sirupsen/logrus"
"sort" "sort"
"strings" "strings"
"sync" "sync"
@ -54,6 +55,8 @@ func (s *MetricStore) Insert(value collector.CollectedMetric) {
s.insertCustomMetric(value.Custom, value.Labels) s.insertCustomMetric(value.Custom, value.Labels)
case autoscalingv2beta2.ExternalMetricSourceType: case autoscalingv2beta2.ExternalMetricSourceType:
s.insertExternalMetric(value.External) s.insertExternalMetric(value.External)
default:
logrus.Errorf("value type unknown: %v", value.Type)
} }
} }
@ -85,7 +88,7 @@ func (s *MetricStore) insertCustomMetric(value custom_metrics.MetricValue, label
metrics, ok := s.customMetricsStore[value.Metric.Name] metrics, ok := s.customMetricsStore[value.Metric.Name]
if !ok { if !ok {
s.customMetricsStore[value.Metric.Name] = map[schema.GroupResource]map[string]map[string]customMetricsStoredMetric{ s.customMetricsStore[value.Metric.Name] = map[schema.GroupResource]map[string]map[string]customMetricsStoredMetric{
groupResource: map[string]map[string]customMetricsStoredMetric{ groupResource: {
value.DescribedObject.Namespace: map[string]customMetricsStoredMetric{ value.DescribedObject.Namespace: map[string]customMetricsStoredMetric{
value.DescribedObject.Name: metric, value.DescribedObject.Name: metric,
}, },
@ -97,7 +100,7 @@ func (s *MetricStore) insertCustomMetric(value custom_metrics.MetricValue, label
group, ok := metrics[groupResource] group, ok := metrics[groupResource]
if !ok { if !ok {
metrics[groupResource] = map[string]map[string]customMetricsStoredMetric{ metrics[groupResource] = map[string]map[string]customMetricsStoredMetric{
value.DescribedObject.Namespace: map[string]customMetricsStoredMetric{ value.DescribedObject.Namespace: {
value.DescribedObject.Name: metric, value.DescribedObject.Name: metric,
}, },
} }
@ -113,6 +116,7 @@ func (s *MetricStore) insertCustomMetric(value custom_metrics.MetricValue, label
} }
namespace[value.DescribedObject.Name] = metric namespace[value.DescribedObject.Name] = metric
} }
// insertExternalMetric inserts an external metric into the store. // insertExternalMetric inserts an external metric into the store.