mirror of
https://github.com/zalando-incubator/kube-metrics-adapter.git
synced 2025-03-11 12:43:54 +00:00
Making TTL configurable, at least globally
Making metrics expiration TTL configurable globally configurable. This is so RemoveExpired can be tested. Not making the TTL configurable per metric yet because it could change behaviour in production. Signed-off-by: Muhammad Muaaz Saleem <muhammad.muaaz.saleem@zalando.de>
This commit is contained in:
@ -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()
|
||||
|
@ -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()
|
||||
|
Reference in New Issue
Block a user