2022-12-20 23:32:45 +01:00
|
|
|
package remotewrite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promrelabel"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestApplyRelabeling(t *testing.T) {
|
2023-08-15 13:47:48 +02:00
|
|
|
f := func(pcs *promrelabel.ParsedConfigs, sTss, sExpTss string) {
|
2022-12-20 23:32:45 +01:00
|
|
|
rctx := &relabelCtx{}
|
|
|
|
tss, expTss := parseSeries(sTss), parseSeries(sExpTss)
|
2023-08-15 13:47:48 +02:00
|
|
|
gotTss := rctx.applyRelabeling(tss, pcs)
|
2022-12-20 23:32:45 +01:00
|
|
|
if !reflect.DeepEqual(gotTss, expTss) {
|
|
|
|
t.Fatalf("expected to have: \n%v;\ngot: \n%v", expTss, gotTss)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-15 13:47:48 +02:00
|
|
|
f(nil, "up", "up")
|
2022-12-20 23:32:45 +01:00
|
|
|
|
|
|
|
pcs, err := promrelabel.ParseRelabelConfigsData([]byte(`
|
|
|
|
- target_label: "foo"
|
|
|
|
replacement: "aaa"
|
|
|
|
- action: labeldrop
|
|
|
|
regex: "env.*"
|
|
|
|
`))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
2023-08-15 13:47:48 +02:00
|
|
|
f(pcs, `up{foo="baz", env="prod"}`, `up{foo="aaa"}`)
|
2022-12-20 23:32:45 +01:00
|
|
|
|
|
|
|
oldVal := *usePromCompatibleNaming
|
|
|
|
*usePromCompatibleNaming = true
|
2023-08-15 13:47:48 +02:00
|
|
|
f(nil, `foo.bar`, `foo_bar`)
|
|
|
|
*usePromCompatibleNaming = oldVal
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAppendExtraLabels(t *testing.T) {
|
|
|
|
f := func(extraLabels []prompbmarshal.Label, sTss, sExpTss string) {
|
2023-09-08 23:17:16 +02:00
|
|
|
t.Helper()
|
2023-08-15 13:47:48 +02:00
|
|
|
rctx := &relabelCtx{}
|
|
|
|
tss, expTss := parseSeries(sTss), parseSeries(sExpTss)
|
2023-08-17 14:35:26 +02:00
|
|
|
rctx.appendExtraLabels(tss, extraLabels)
|
|
|
|
if !reflect.DeepEqual(tss, expTss) {
|
|
|
|
t.Fatalf("expected to have: \n%v;\ngot: \n%v", expTss, tss)
|
2023-08-15 13:47:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
f(nil, "up", "up")
|
|
|
|
f([]prompbmarshal.Label{{Name: "foo", Value: "bar"}}, "up", `up{foo="bar"}`)
|
|
|
|
f([]prompbmarshal.Label{{Name: "foo", Value: "bar"}}, `up{foo="baz"}`, `up{foo="bar"}`)
|
|
|
|
f([]prompbmarshal.Label{{Name: "baz", Value: "qux"}}, `up{foo="baz"}`, `up{foo="baz",baz="qux"}`)
|
|
|
|
|
|
|
|
oldVal := *usePromCompatibleNaming
|
|
|
|
*usePromCompatibleNaming = true
|
2023-09-08 23:17:16 +02:00
|
|
|
f([]prompbmarshal.Label{{Name: "foo.bar", Value: "baz"}}, "up", `up{foo.bar="baz"}`)
|
2022-12-20 23:32:45 +01:00
|
|
|
*usePromCompatibleNaming = oldVal
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseSeries(data string) []prompbmarshal.TimeSeries {
|
|
|
|
var tss []prompbmarshal.TimeSeries
|
|
|
|
tss = append(tss, prompbmarshal.TimeSeries{
|
|
|
|
Labels: promutils.MustNewLabelsFromString(data).GetLabels(),
|
|
|
|
})
|
|
|
|
return tss
|
|
|
|
}
|