Skip to content

Commit e159d75

Browse files
authored
Make the default reported value and error configurable (#85)
Signed-off-by: Jirka Kremser <jiri.kremser@gmail.com>
1 parent 370e056 commit e159d75

File tree

8 files changed

+120
-57
lines changed

8 files changed

+120
-57
lines changed

helmchart/otel-add-on/templates/deployment.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ spec:
4646
value: {{ .Values.settings.metricStore.lazySeries | quote }}
4747
- name: METRIC_STORE_LAZY_AGGREGATES
4848
value: {{ .Values.settings.metricStore.lazyAggregates | quote }}
49+
- name: METRIC_STORE_ERROR_IF_NOT_FOUND
50+
value: {{ .Values.settings.metricStore.errIfNotFound | quote }}
51+
- name: METRIC_STORE_VALUE_IF_NOT_FOUND
52+
value: {{ .Values.settings.metricStore.valueIfNotFound | quote }}
4953
- name: IS_ACTIVE_POLLING_INTERVAL_MS
5054
value: {{ .Values.settings.isActivePollingIntervalMilliseconds | quote }}
5155
- name: INTERNAL_METRICS_PORT

helmchart/otel-add-on/values.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ settings:
2424
# (by default, we calculate and store all of them - sum, rate, min, max, etc.)
2525
lazyAggregates: false
2626

27+
# -- when enabled, the scaler will be returning error to KEDA's `GetMetrics()` call
28+
errIfNotFound: false
29+
30+
# -- default value, that is reported in case of error or if the value is not in the mem store
31+
valueIfNotFound: 0.
32+
2733
# -- how often (in milliseconds) should the IsActive method be tried
2834
isActivePollingIntervalMilliseconds: 500
2935

main.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ func main() {
4848
cfg := util.MustParseConfig()
4949
otlpReceiverPort := cfg.OTLPReceiverPort
5050
restApiPort := cfg.RestApiPort
51-
metricStoreRetentionSeconds := cfg.MetricStoreRetentionSeconds
52-
lazySeries := cfg.MetricStoreLazySeries
53-
lazyAggregates := cfg.MetricStoreLazyAggregates
5451

5552
lvl := util.SetupLog(cfg.NoColor)
5653
isDebug = util.IsDebug(lvl)
@@ -61,7 +58,7 @@ func main() {
6158

6259
ctx := util.ContextWithLogger(ctrl.SetupSignalHandler(), setupLog)
6360
eg, ctx := errgroup.WithContext(ctx)
64-
ms := metric.NewMetricStore(metricStoreRetentionSeconds, lazySeries, lazyAggregates)
61+
ms := metric.NewMetricStore(cfg)
6562
mp := metric.NewParser()
6663

6764
eg.Go(func() error {

metric/mem_store.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,20 @@ type ms struct {
2121
metricsExporter *InternalMetrics
2222
lazySeries bool
2323
lazyAggregates bool
24+
valIfNotFound float64
2425
subscriptions map[t.MetricName][]t.OperationOverTime
2526
}
2627

27-
func NewMetricStore(stalePeriodSeconds int, lazySeries, lazyAggregates bool) t.MemStore {
28+
func NewMetricStore(cfg *util.Config) t.MemStore {
2829
m := Metrics()
2930
m.Init()
3031
return ms{
3132
store: &t.Map[string, *t.Map[t.LabelsHash, *t.MetricData]]{},
32-
stalePeriodSeconds: stalePeriodSeconds,
33+
stalePeriodSeconds: cfg.MetricStoreRetentionSeconds,
3334
metricsExporter: m,
34-
lazySeries: lazySeries,
35-
lazyAggregates: lazyAggregates,
35+
lazySeries: cfg.MetricStoreLazySeries,
36+
lazyAggregates: cfg.MetricStoreLazyAggregates,
37+
valIfNotFound: cfg.MetricStoreValueIfNotFound,
3638
subscriptions: map[t.MetricName][]t.OperationOverTime{},
3739
}
3840
}
@@ -51,20 +53,20 @@ func (m ms) Get(unescapedName t.MetricName, searchLabels t.Labels, timeOp t.Oper
5153
func (m ms) get(name t.MetricName, searchLabels t.Labels, timeOp t.OperationOverTime, vecOp t.AggregationOverVectors) (float64, t.Found, error) {
5254
now := time.Now().Unix()
5355
if err := util.CheckTimeOp(timeOp); err != nil {
54-
return -1., false, err
56+
return m.valIfNotFound, false, err
5557
}
5658
if err := checkVectorAggregation(vecOp); err != nil {
57-
return -1., false, err
59+
return m.valIfNotFound, false, err
5860
}
5961
if m.lazySeries || m.lazyAggregates {
6062
if firstTime := subscribe(m.subscriptions, m.lazyAggregates, name, timeOp); firstTime {
61-
return -1., false, nil
63+
return m.valIfNotFound, false, nil
6264
}
6365
}
6466
storedMetrics, foundMetric := m.store.Load(string(name))
6567
if !foundMetric {
6668
// not found
67-
return -1., false, nil
69+
return m.valIfNotFound, false, nil
6870
}
6971
if md, f := storedMetrics.Load(hashOfMap(searchLabels)); f {
7072
// found exact label match
@@ -74,7 +76,7 @@ func (m ms) get(name t.MetricName, searchLabels t.Labels, timeOp t.OperationOver
7476
if !m.isStale(md.LastUpdate, now) {
7577
ret, f := md.AggregatesOverTime.Load(timeOp)
7678
if !f {
77-
return -1., false, fmt.Errorf("unknown OperationOverTime: %s", timeOp)
79+
return m.valIfNotFound, false, fmt.Errorf("unknown OperationOverTime: %s", timeOp)
7880
}
7981
return ret, true, nil
8082
} else {

metric/mem_store_lazy_test.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ import (
77
"go.opentelemetry.io/collector/pdata/pcommon"
88

99
ty "github.com/kedify/otel-add-on/types"
10+
"github.com/kedify/otel-add-on/util"
1011
)
1112

1213
func TestLazyMemStorePutOneAndGetNothing(t *testing.T) {
1314
// setup
14-
ms := NewMetricStore(5, true, false)
15+
ms := NewMetricStore(&util.Config{
16+
MetricStoreRetentionSeconds: 5,
17+
MetricStoreLazySeries: true,
18+
})
1519
ms.Put(ty.NewMetricEntry{
1620
Name: "metric1",
1721
MeasurementTime: pcommon.Timestamp(time.Now().Unix()),
@@ -28,7 +32,10 @@ func TestLazyMemStorePutOneAndGetNothing(t *testing.T) {
2832

2933
func TestLazyMemStoreGetPutAndGet(t *testing.T) {
3034
// setup
31-
ms := NewMetricStore(70, true, false)
35+
ms := NewMetricStore(&util.Config{
36+
MetricStoreRetentionSeconds: 70,
37+
MetricStoreLazySeries: true,
38+
})
3239
labels := map[string]any{
3340
"a": "1",
3441
}
@@ -48,7 +55,10 @@ func TestLazyMemStoreGetPutAndGet(t *testing.T) {
4855

4956
func TestMemStoreLazyAggregatesFirstCallNotFound(t *testing.T) {
5057
// setup
51-
ms := NewMetricStore(5, false, true)
58+
ms := NewMetricStore(&util.Config{
59+
MetricStoreRetentionSeconds: 5,
60+
MetricStoreLazyAggregates: true,
61+
})
5262
ms.Put(ty.NewMetricEntry{
5363
Name: "metric1",
5464
MeasurementTime: pcommon.Timestamp(time.Now().Unix()),
@@ -65,7 +75,10 @@ func TestMemStoreLazyAggregatesFirstCallNotFound(t *testing.T) {
6575

6676
func TestMemStoreLazyAggregatesOneAgg(t *testing.T) {
6777
// setup
68-
ms := NewMetricStore(70, false, true)
78+
ms := NewMetricStore(&util.Config{
79+
MetricStoreRetentionSeconds: 70,
80+
MetricStoreLazyAggregates: true,
81+
})
6982
labels := map[string]any{
7083
"a": "1",
7184
}
@@ -82,7 +95,10 @@ func TestMemStoreLazyAggregatesOneAgg(t *testing.T) {
8295

8396
func TestMemStoreLazyAggregatesAddingOnTheFly(t *testing.T) {
8497
// setup
85-
ms := NewMetricStore(70, false, true)
98+
ms := NewMetricStore(&util.Config{
99+
MetricStoreRetentionSeconds: 70,
100+
MetricStoreLazyAggregates: true,
101+
})
86102
labels := map[string]any{
87103
"a": "1",
88104
}
@@ -103,7 +119,11 @@ func TestMemStoreLazyAggregatesAddingOnTheFly(t *testing.T) {
103119

104120
func TestLazyMemStoreAndLazyAggregatesComplex(t *testing.T) {
105121
// setup
106-
ms := NewMetricStore(700, true, true)
122+
ms := NewMetricStore(&util.Config{
123+
MetricStoreRetentionSeconds: 700,
124+
MetricStoreLazySeries: true,
125+
MetricStoreLazyAggregates: true,
126+
})
107127
labels := map[string]any{
108128
"a": "1",
109129
"b": "2",

0 commit comments

Comments
 (0)