diff --git a/component/prometheus/relabel/relabel.go b/component/prometheus/relabel/relabel.go index 349c5eeb1006..0f3b0c6dc9ac 100644 --- a/component/prometheus/relabel/relabel.go +++ b/component/prometheus/relabel/relabel.go @@ -56,6 +56,14 @@ func (arg *Arguments) SetToDefault() { } } +// Validate implements river.Validator. +func (arg *Arguments) Validate() error { + if arg.CacheSize <= 0 { + return fmt.Errorf("max_cache_size must be greater than 0 and is %d", arg.CacheSize) + } + return nil +} + // Exports holds values which are exported by the prometheus.relabel component. type Exports struct { Receiver storage.Appendable `river:"receiver,attr"` diff --git a/component/prometheus/relabel/relabel_test.go b/component/prometheus/relabel/relabel_test.go index c6d2b2699031..d029498555b8 100644 --- a/component/prometheus/relabel/relabel_test.go +++ b/component/prometheus/relabel/relabel_test.go @@ -44,11 +44,22 @@ func TestUpdateReset(t *testing.T) { relabeller.relabel(0, lbls) require.True(t, relabeller.cache.Len() == 1) _ = relabeller.Update(Arguments{ + CacheSize: 100000, MetricRelabelConfigs: []*flow_relabel.Config{}, }) require.True(t, relabeller.cache.Len() == 0) } +func TestValidator(t *testing.T) { + args := Arguments{CacheSize: 0} + err := args.Validate() + require.Error(t, err) + + args.CacheSize = 1 + err = args.Validate() + require.NoError(t, err) +} + func TestNil(t *testing.T) { ls := labelstore.New(nil, prom.DefaultRegisterer) fanout := prometheus.NewInterceptor(nil, ls, prometheus.WithAppendHook(func(ref storage.SeriesRef, _ labels.Labels, _ int64, _ float64, _ storage.Appender) (storage.SeriesRef, error) { @@ -72,6 +83,7 @@ func TestNil(t *testing.T) { Action: "drop", }, }, + CacheSize: 100000, }) require.NotNil(t, relabeller) require.NoError(t, err) @@ -129,7 +141,6 @@ func BenchmarkCache(b *testing.B) { lbls := labels.FromStrings("__address__", "localhost") app := entry.Appender(context.Background()) - for i := 0; i < b.N; i++ { app.Append(0, lbls, time.Now().UnixMilli(), 0) } @@ -161,6 +172,7 @@ func generateRelabel(t *testing.T) *Component { Action: "replace", }, }, + CacheSize: 100_000, }) require.NotNil(t, relabeller) require.NoError(t, err)