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.
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.

View File

@ -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
}

View File

@ -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.