Path: blob/main/component/prometheus/relabel/relabel_test.go
4094 views
package relabel12import (3"math"4"testing"5"time"67"context"89"github.com/grafana/agent/component"10flow_relabel "github.com/grafana/agent/component/common/relabel"11"github.com/grafana/agent/component/prometheus"12"github.com/grafana/agent/pkg/flow/componenttest"13"github.com/grafana/agent/pkg/river"14"github.com/grafana/agent/pkg/util"15prom "github.com/prometheus/client_golang/prometheus"16"github.com/prometheus/prometheus/model/labels"17"github.com/prometheus/prometheus/model/relabel"18"github.com/prometheus/prometheus/model/value"19"github.com/prometheus/prometheus/storage"20"github.com/stretchr/testify/require"21)2223func TestCache(t *testing.T) {24relabeller := generateRelabel(t)25lbls := labels.FromStrings("__address__", "localhost")26relabeller.relabel(0, lbls)27require.Len(t, relabeller.cache, 1)28entry, found := relabeller.getFromCache(prometheus.GlobalRefMapping.GetOrAddGlobalRefID(lbls))29require.True(t, found)30require.NotNil(t, entry)31require.True(32t,33prometheus.GlobalRefMapping.GetOrAddGlobalRefID(entry.labels) != prometheus.GlobalRefMapping.GetOrAddGlobalRefID(lbls),34)35}3637func TestEviction(t *testing.T) {38relabeller := generateRelabel(t)39lbls := labels.FromStrings("__address__", "localhost")40relabeller.relabel(0, lbls)41require.Len(t, relabeller.cache, 1)42relabeller.relabel(math.Float64frombits(value.StaleNaN), lbls)43require.Len(t, relabeller.cache, 0)44}4546func TestUpdateReset(t *testing.T) {47relabeller := generateRelabel(t)48lbls := labels.FromStrings("__address__", "localhost")49relabeller.relabel(0, lbls)50require.Len(t, relabeller.cache, 1)51_ = relabeller.Update(Arguments{52MetricRelabelConfigs: []*flow_relabel.Config{},53})54require.Len(t, relabeller.cache, 0)55}5657func TestNil(t *testing.T) {58fanout := prometheus.NewInterceptor(nil, prometheus.WithAppendHook(func(ref storage.SeriesRef, _ labels.Labels, _ int64, _ float64, _ storage.Appender) (storage.SeriesRef, error) {59require.True(t, false)60return ref, nil61}))62relabeller, err := New(component.Options{63ID: "1",64Logger: util.TestFlowLogger(t),65OnStateChange: func(e component.Exports) {},66Registerer: prom.NewRegistry(),67}, Arguments{68ForwardTo: []storage.Appendable{fanout},69MetricRelabelConfigs: []*flow_relabel.Config{70{71SourceLabels: []string{"__address__"},72Regex: flow_relabel.Regexp(relabel.MustNewRegexp("(.+)")),73Action: "drop",74},75},76})77require.NotNil(t, relabeller)78require.NoError(t, err)7980lbls := labels.FromStrings("__address__", "localhost")81relabeller.relabel(0, lbls)82}8384func BenchmarkCache(b *testing.B) {85fanout := prometheus.NewInterceptor(nil, prometheus.WithAppendHook(func(ref storage.SeriesRef, l labels.Labels, _ int64, _ float64, _ storage.Appender) (storage.SeriesRef, error) {86require.True(b, l.Has("new_label"))87return ref, nil88}))89var entry storage.Appendable90_, _ = New(component.Options{91ID: "1",92Logger: util.TestFlowLogger(b),93OnStateChange: func(e component.Exports) {94newE := e.(Exports)95entry = newE.Receiver96},97Registerer: prom.NewRegistry(),98}, Arguments{99ForwardTo: []storage.Appendable{fanout},100MetricRelabelConfigs: []*flow_relabel.Config{101{102SourceLabels: []string{"__address__"},103Regex: flow_relabel.Regexp(relabel.MustNewRegexp("(.+)")),104TargetLabel: "new_label",105Replacement: "new_value",106Action: "replace",107},108},109})110111lbls := labels.FromStrings("__address__", "localhost")112app := entry.Appender(context.Background())113114for i := 0; i < b.N; i++ {115app.Append(0, lbls, time.Now().UnixMilli(), 0)116}117app.Commit()118}119120func generateRelabel(t *testing.T) *Component {121fanout := prometheus.NewInterceptor(nil, prometheus.WithAppendHook(func(ref storage.SeriesRef, l labels.Labels, _ int64, _ float64, _ storage.Appender) (storage.SeriesRef, error) {122require.True(t, l.Has("new_label"))123return ref, nil124}))125relabeller, err := New(component.Options{126ID: "1",127Logger: util.TestFlowLogger(t),128OnStateChange: func(e component.Exports) {},129Registerer: prom.NewRegistry(),130}, Arguments{131ForwardTo: []storage.Appendable{fanout},132MetricRelabelConfigs: []*flow_relabel.Config{133{134SourceLabels: []string{"__address__"},135Regex: flow_relabel.Regexp(relabel.MustNewRegexp("(.+)")),136TargetLabel: "new_label",137Replacement: "new_value",138Action: "replace",139},140},141})142require.NotNil(t, relabeller)143require.NoError(t, err)144return relabeller145}146147func TestRuleGetter(t *testing.T) {148// Set up the component Arguments.149originalCfg := `rule {150action = "keep"151source_labels = ["__name__"]152regex = "up"153}154forward_to = []`155var args Arguments156require.NoError(t, river.Unmarshal([]byte(originalCfg), &args))157158// Set up and start the component.159tc, err := componenttest.NewControllerFromID(nil, "prometheus.relabel")160require.NoError(t, err)161go func() {162err = tc.Run(componenttest.TestContext(t), args)163require.NoError(t, err)164}()165require.NoError(t, tc.WaitExports(time.Second))166167// Use the getter to retrieve the original relabeling rules.168exports := tc.Exports().(Exports)169gotOriginal := exports.Rules170171// Update the component with new relabeling rules and retrieve them.172updatedCfg := `rule {173action = "drop"174source_labels = ["__name__"]175regex = "up"176}177forward_to = []`178require.NoError(t, river.Unmarshal([]byte(updatedCfg), &args))179180require.NoError(t, tc.Update(args))181exports = tc.Exports().(Exports)182gotUpdated := exports.Rules183184require.NotEqual(t, gotOriginal, gotUpdated)185require.Len(t, gotOriginal, 1)186require.Len(t, gotUpdated, 1)187188require.Equal(t, gotOriginal[0].Action, flow_relabel.Keep)189require.Equal(t, gotUpdated[0].Action, flow_relabel.Drop)190require.Equal(t, gotUpdated[0].SourceLabels, gotOriginal[0].SourceLabels)191require.Equal(t, gotUpdated[0].Regex, gotOriginal[0].Regex)192}193194195