From d50771094ac66553f9b2b18dd8d5c4712d397462 Mon Sep 17 00:00:00 2001 From: Muhammad Muaaz Saleem Date: Tue, 20 Nov 2018 13:15:45 +0100 Subject: [PATCH] Making metrics TTL configurable per instance of metric store Avoiding the global var and making metrics TTL configurable per metric store Signed-off-by: Muhammad Muaaz Saleem --- pkg/provider/hpa.go | 10 ++++++---- pkg/provider/metric_store.go | 12 +++++------- pkg/provider/metrics_store_test.go | 24 +++++++++++------------- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/pkg/provider/hpa.go b/pkg/provider/hpa.go index 618485a..2009431 100644 --- a/pkg/provider/hpa.go +++ b/pkg/provider/hpa.go @@ -81,10 +81,12 @@ func NewHPAProvider(client kubernetes.Interface, interval, collectorInterval tim interval: interval, collectorInterval: collectorInterval, metricSink: metricsc, - metricStore: NewMetricStore(), - collectorFactory: collectorFactory, - recorder: recorder.CreateEventRecorder(client), - logger: log.WithFields(log.Fields{"provider": "hpa"}), + metricStore: NewMetricStore(func() time.Time { + return time.Now().UTC().Add(15 * time.Minute) + }), + collectorFactory: collectorFactory, + recorder: recorder.CreateEventRecorder(client), + logger: log.WithFields(log.Fields{"provider": "hpa"}), } } diff --git a/pkg/provider/metric_store.go b/pkg/provider/metric_store.go index b0d285c..5db06fd 100644 --- a/pkg/provider/metric_store.go +++ b/pkg/provider/metric_store.go @@ -34,18 +34,16 @@ type externalMetricsStoredMetric struct { type MetricStore struct { customMetricsStore map[string]map[schema.GroupResource]map[string]map[string]customMetricsStoredMetric externalMetricsStore map[string]map[string]externalMetricsStoredMetric + metricsTTLCalculator func() time.Time sync.RWMutex } -var metricsTTL = func() time.Time { - return time.Now().UTC().Add(15 * time.Minute) -} - // NewMetricStore initializes an empty Metrics Store. -func NewMetricStore() *MetricStore { +func NewMetricStore(ttlCalculator func() time.Time) *MetricStore { return &MetricStore{ customMetricsStore: make(map[string]map[schema.GroupResource]map[string]map[string]customMetricsStoredMetric, 0), externalMetricsStore: make(map[string]map[string]externalMetricsStoredMetric, 0), + metricsTTLCalculator: ttlCalculator, } } @@ -81,7 +79,7 @@ func (s *MetricStore) insertCustomMetric(value custom_metrics.MetricValue, label metric := customMetricsStoredMetric{ Value: value, Labels: labels, - TTL: metricsTTL(), // TODO: make metricsTTL configurable + TTL: s.metricsTTLCalculator(), // TODO: make TTL configurable } metrics, ok := s.customMetricsStore[value.MetricName] @@ -122,7 +120,7 @@ func (s *MetricStore) insertExternalMetric(metric external_metrics.ExternalMetri storedMetric := externalMetricsStoredMetric{ Value: metric, - TTL: metricsTTL(), // TODO: make metricsTTL configurable + TTL: s.metricsTTLCalculator(), // TODO: make TTL configurable } labelsKey := hashLabelMap(metric.MetricLabels) diff --git a/pkg/provider/metrics_store_test.go b/pkg/provider/metrics_store_test.go index f2c6046..ce39da2 100644 --- a/pkg/provider/metrics_store_test.go +++ b/pkg/provider/metrics_store_test.go @@ -6,7 +6,6 @@ import ( "github.com/zalando-incubator/kube-metrics-adapter/pkg/collector" "k8s.io/api/autoscaling/v2beta1" "k8s.io/apimachinery/pkg/api/resource" - "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/types" @@ -128,7 +127,9 @@ func TestInternalMetricStorage(t *testing.T) { for _, tc := range metricStoreTests { t.Run(tc.test, func(t *testing.T) { - metricsStore := NewMetricStore() + metricsStore := NewMetricStore(func() time.Time { + return time.Now().UTC().Add(15 * time.Minute) + }) // Insert a metric with value metricsStore.Insert(tc.insert) @@ -186,7 +187,9 @@ func TestExternalMetricStorage(t *testing.T) { for _, tc := range metricStoreTests { t.Run(tc.test, func(t *testing.T) { - metricsStore := NewMetricStore() + metricsStore := NewMetricStore(func() time.Time { + return time.Now().UTC().Add(15 * time.Minute) + }) // Insert a metric with value metricsStore.Insert(tc.insert) @@ -206,13 +209,10 @@ func TestExternalMetricStorage(t *testing.T) { } func TestMetricsExpiration(t *testing.T) { - metricStore := NewMetricStore() - // Temporarily Override global TTL to test expiration - tmpTTL := metricsTTL - metricsTTL = func() time.Time { + metricStore := NewMetricStore(func() time.Time { return time.Now().UTC().Add(time.Hour * -1) - } + }) customMetric := collector.CollectedMetric{ Type: v2beta1.MetricSourceType("Object"), @@ -224,7 +224,6 @@ func TestMetricsExpiration(t *testing.T) { Kind: "Node", APIVersion: "core/v1", }, - Timestamp: v1.Time{Time: metricsTTL()}, }, } @@ -233,7 +232,6 @@ func TestMetricsExpiration(t *testing.T) { External: external_metrics.ExternalMetricValue{ MetricName: "metric-per-unit", Value: *resource.NewQuantity(0, ""), - Timestamp: v1.Time{Time: metricsTTL()}, }, } @@ -248,12 +246,12 @@ func TestMetricsExpiration(t *testing.T) { externalMetricInfos := metricStore.ListAllExternalMetrics() require.Len(t, externalMetricInfos, 0) - // set metricTTL to original - metricsTTL = tmpTTL } func TestMetricsNonExpiration(t *testing.T) { - metricStore := NewMetricStore() + metricStore := NewMetricStore(func() time.Time { + return time.Now().UTC().Add(15 * time.Minute) + }) customMetric := collector.CollectedMetric{ Type: v2beta1.MetricSourceType("Object"),