From 417f650caa2a1243e7eb86953359a4169002d9fe Mon Sep 17 00:00:00 2001 From: Arjun Naik Date: Thu, 10 Jan 2019 11:23:31 +0100 Subject: [PATCH] Handle 0 values in max collector and unknown metric type --- pkg/collector/max_collector.go | 13 +++++++++---- pkg/collector/skipper_collector.go | 6 ++++-- pkg/provider/metric_store.go | 8 ++++++-- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/pkg/collector/max_collector.go b/pkg/collector/max_collector.go index 5aa2514..34484ea 100644 --- a/pkg/collector/max_collector.go +++ b/pkg/collector/max_collector.go @@ -24,7 +24,7 @@ func NewWeightedMaxCollector(interval time.Duration, weight float64, collectors // GetMetrics gets metrics from all collectors and return the higest value. func (c *WeightedMaxCollector) GetMetrics() ([]CollectedMetric, error) { - var max CollectedMetric + var max *CollectedMetric for _, collector := range c.collectors { values, err := collector.GetMetrics() if err != nil { @@ -32,14 +32,19 @@ func (c *WeightedMaxCollector) GetMetrics() ([]CollectedMetric, error) { } for _, value := range values { - if value.Custom.Value.MilliValue() > max.Custom.Value.MilliValue() { - max = value + if max != nil { + 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) - return []CollectedMetric{max}, nil + + return []CollectedMetric{*max}, nil } // Interval returns the interval at which the collector should run. diff --git a/pkg/collector/skipper_collector.go b/pkg/collector/skipper_collector.go index 5e2a807..88c6a84 100644 --- a/pkg/collector/skipper_collector.go +++ b/pkg/collector/skipper_collector.go @@ -103,7 +103,10 @@ func (c *SkipperCollector) getCollector() (Collector, error) { 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 @@ -147,7 +150,6 @@ func (c *SkipperCollector) GetMetrics() ([]CollectedMetric, error) { if len(values) != 1 { return nil, fmt.Errorf("expected to only get one metric value, got %d", len(values)) } - value := values[0] return []CollectedMetric{value}, nil } diff --git a/pkg/provider/metric_store.go b/pkg/provider/metric_store.go index 184ac96..882547b 100644 --- a/pkg/provider/metric_store.go +++ b/pkg/provider/metric_store.go @@ -2,6 +2,7 @@ package provider import ( "fmt" + "github.com/sirupsen/logrus" "sort" "strings" "sync" @@ -54,6 +55,8 @@ func (s *MetricStore) Insert(value collector.CollectedMetric) { s.insertCustomMetric(value.Custom, value.Labels) case autoscalingv2beta2.ExternalMetricSourceType: 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] if !ok { 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.Name: metric, }, @@ -97,7 +100,7 @@ func (s *MetricStore) insertCustomMetric(value custom_metrics.MetricValue, label group, ok := metrics[groupResource] if !ok { metrics[groupResource] = map[string]map[string]customMetricsStoredMetric{ - value.DescribedObject.Namespace: map[string]customMetricsStoredMetric{ + value.DescribedObject.Namespace: { value.DescribedObject.Name: metric, }, } @@ -113,6 +116,7 @@ func (s *MetricStore) insertCustomMetric(value custom_metrics.MetricValue, label } namespace[value.DescribedObject.Name] = metric + } // insertExternalMetric inserts an external metric into the store.