Merge pull request #14 from zalando-incubator/unit-tests

Adding Unit tests to metric_store.go
This commit is contained in:
Mikkel Oscar Lyderik Larsen
2018-11-22 16:57:42 +01:00
committed by GitHub
3 changed files with 896 additions and 9 deletions

View File

@ -81,10 +81,12 @@ func NewHPAProvider(client kubernetes.Interface, interval, collectorInterval tim
interval: interval, interval: interval,
collectorInterval: collectorInterval, collectorInterval: collectorInterval,
metricSink: metricsc, metricSink: metricsc,
metricStore: NewMetricStore(), metricStore: NewMetricStore(func() time.Time {
collectorFactory: collectorFactory, return time.Now().UTC().Add(15 * time.Minute)
recorder: recorder.CreateEventRecorder(client), }),
logger: log.WithFields(log.Fields{"provider": "hpa"}), collectorFactory: collectorFactory,
recorder: recorder.CreateEventRecorder(client),
logger: log.WithFields(log.Fields{"provider": "hpa"}),
} }
} }

View File

@ -17,7 +17,7 @@ import (
"k8s.io/metrics/pkg/apis/external_metrics" "k8s.io/metrics/pkg/apis/external_metrics"
) )
// customMetricsStoredMetric is a wrapper around custom_metrics.MetricValue with a TTL used // customMetricsStoredMetric is a wrapper around custom_metrics.MetricValue with a metricsTTL used
// to clean up stale metrics from the customMetricsStore. // to clean up stale metrics from the customMetricsStore.
type customMetricsStoredMetric struct { type customMetricsStoredMetric struct {
Value custom_metrics.MetricValue Value custom_metrics.MetricValue
@ -34,14 +34,16 @@ type externalMetricsStoredMetric struct {
type MetricStore struct { type MetricStore struct {
customMetricsStore map[string]map[schema.GroupResource]map[string]map[string]customMetricsStoredMetric customMetricsStore map[string]map[schema.GroupResource]map[string]map[string]customMetricsStoredMetric
externalMetricsStore map[string]map[string]externalMetricsStoredMetric externalMetricsStore map[string]map[string]externalMetricsStoredMetric
metricsTTLCalculator func() time.Time
sync.RWMutex sync.RWMutex
} }
// NewMetricStore initializes an empty Metrics Store. // NewMetricStore initializes an empty Metrics Store.
func NewMetricStore() *MetricStore { func NewMetricStore(ttlCalculator func() time.Time) *MetricStore {
return &MetricStore{ return &MetricStore{
customMetricsStore: make(map[string]map[schema.GroupResource]map[string]map[string]customMetricsStoredMetric, 0), customMetricsStore: make(map[string]map[schema.GroupResource]map[string]map[string]customMetricsStoredMetric, 0),
externalMetricsStore: make(map[string]map[string]externalMetricsStoredMetric, 0), externalMetricsStore: make(map[string]map[string]externalMetricsStoredMetric, 0),
metricsTTLCalculator: ttlCalculator,
} }
} }
@ -77,7 +79,7 @@ func (s *MetricStore) insertCustomMetric(value custom_metrics.MetricValue, label
metric := customMetricsStoredMetric{ metric := customMetricsStoredMetric{
Value: value, Value: value,
Labels: labels, Labels: labels,
TTL: time.Now().UTC().Add(15 * time.Minute), // TODO: make TTL configurable TTL: s.metricsTTLCalculator(), // TODO: make TTL configurable
} }
metrics, ok := s.customMetricsStore[value.MetricName] metrics, ok := s.customMetricsStore[value.MetricName]
@ -99,6 +101,7 @@ func (s *MetricStore) insertCustomMetric(value custom_metrics.MetricValue, label
value.DescribedObject.Name: metric, value.DescribedObject.Name: metric,
}, },
} }
return
} }
namespace, ok := group[value.DescribedObject.Namespace] namespace, ok := group[value.DescribedObject.Namespace]
@ -106,6 +109,7 @@ func (s *MetricStore) insertCustomMetric(value custom_metrics.MetricValue, label
group[value.DescribedObject.Namespace] = map[string]customMetricsStoredMetric{ group[value.DescribedObject.Namespace] = map[string]customMetricsStoredMetric{
value.DescribedObject.Name: metric, value.DescribedObject.Name: metric,
} }
return
} }
namespace[value.DescribedObject.Name] = metric namespace[value.DescribedObject.Name] = metric
@ -118,7 +122,7 @@ func (s *MetricStore) insertExternalMetric(metric external_metrics.ExternalMetri
storedMetric := externalMetricsStoredMetric{ storedMetric := externalMetricsStoredMetric{
Value: metric, Value: metric,
TTL: time.Now().UTC().Add(15 * time.Minute), // TODO: make TTL configurable TTL: s.metricsTTLCalculator(), // TODO: make TTL configurable
} }
labelsKey := hashLabelMap(metric.MetricLabels) labelsKey := hashLabelMap(metric.MetricLabels)
@ -270,7 +274,7 @@ func (s *MetricStore) ListAllExternalMetrics() []provider.ExternalMetricInfo {
} }
// RemoveExpired removes expired metrics from the Metrics Store. A metric is // RemoveExpired removes expired metrics from the Metrics Store. A metric is
// considered expired if its TTL is before time.Now(). // considered expired if its metricsTTL is before time.Now().
func (s *MetricStore) RemoveExpired() { func (s *MetricStore) RemoveExpired() {
s.Lock() s.Lock()
defer s.Unlock() defer s.Unlock()

File diff suppressed because it is too large Load Diff