diff --git a/pkg/provider/metric_store.go b/pkg/provider/metric_store.go index 176eecc..b0d285c 100644 --- a/pkg/provider/metric_store.go +++ b/pkg/provider/metric_store.go @@ -17,7 +17,7 @@ import ( "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. type customMetricsStoredMetric struct { Value custom_metrics.MetricValue @@ -37,6 +37,10 @@ type MetricStore struct { sync.RWMutex } +var metricsTTL = func() time.Time { + return time.Now().UTC().Add(15 * time.Minute) +} + // NewMetricStore initializes an empty Metrics Store. func NewMetricStore() *MetricStore { return &MetricStore{ @@ -77,7 +81,7 @@ func (s *MetricStore) insertCustomMetric(value custom_metrics.MetricValue, label metric := customMetricsStoredMetric{ Value: value, Labels: labels, - TTL: time.Now().UTC().Add(15 * time.Minute), // TODO: make TTL configurable + TTL: metricsTTL(), // TODO: make metricsTTL configurable } metrics, ok := s.customMetricsStore[value.MetricName] @@ -118,7 +122,7 @@ func (s *MetricStore) insertExternalMetric(metric external_metrics.ExternalMetri storedMetric := externalMetricsStoredMetric{ Value: metric, - TTL: time.Now().UTC().Add(15 * time.Minute), // TODO: make TTL configurable + TTL: metricsTTL(), // TODO: make metricsTTL configurable } 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 -// considered expired if its TTL is before time.Now(). +// considered expired if its metricsTTL is before time.Now(). func (s *MetricStore) RemoveExpired() { s.Lock() defer s.Unlock() diff --git a/pkg/provider/metrics_store_test.go b/pkg/provider/metrics_store_test.go index defa2e3..94a5ec9 100644 --- a/pkg/provider/metrics_store_test.go +++ b/pkg/provider/metrics_store_test.go @@ -208,8 +208,10 @@ func TestExternalMetricStorage(t *testing.T) { func TestMetricsExpiration(t *testing.T) { metricStore := NewMetricStore() - oldTime := v1.Time{Time: time.Now().UTC().Add(time.Hour * -1)} - + // Override global TTL to test expiration + metricsTTL = func() time.Time { + return time.Now().UTC().Add(time.Hour * -1) + } customMetric := collector.CollectedMetric{ Type: v2beta1.MetricSourceType("Object"), @@ -221,20 +223,19 @@ func TestMetricsExpiration(t *testing.T) { Kind: "Node", APIVersion: "core/v1", }, - Timestamp: oldTime, + Timestamp: v1.Time{Time: metricsTTL()}, }, } - externalMetric := collector.CollectedMetric { + externalMetric := collector.CollectedMetric{ Type: v2beta1.MetricSourceType("External"), External: external_metrics.ExternalMetricValue{ MetricName: "metric-per-unit", Value: *resource.NewQuantity(0, ""), - Timestamp: oldTime, + Timestamp: v1.Time{Time: metricsTTL()}, }, } - metricStore.Insert(customMetric) metricStore.Insert(externalMetric) metricStore.RemoveExpired()