mirror of
https://github.com/zalando-incubator/kube-metrics-adapter.git
synced 2024-12-31 14:24:07 +00:00
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 <muhammad.muaaz.saleem@zalando.de>
This commit is contained in:
@ -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"}),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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"),
|
||||
|
Reference in New Issue
Block a user