2020-02-23 12:35:47 +01:00
|
|
|
package promrelabel
|
|
|
|
|
|
|
|
import (
|
2022-08-25 22:22:03 +02:00
|
|
|
"fmt"
|
2022-12-10 11:09:21 +01:00
|
|
|
"reflect"
|
2020-02-23 12:35:47 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
2022-11-30 06:22:12 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
2020-02-23 12:35:47 +01:00
|
|
|
)
|
|
|
|
|
2022-09-28 08:59:36 +02:00
|
|
|
func TestSanitizeName(t *testing.T) {
|
|
|
|
f := func(s, resultExpected string) {
|
|
|
|
t.Helper()
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
result := SanitizeName(s)
|
|
|
|
if result != resultExpected {
|
|
|
|
t.Fatalf("unexpected result for SanitizeName(%q) at iteration %d; got %q; want %q", s, i, result, resultExpected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f("", "")
|
|
|
|
f("a", "a")
|
|
|
|
f("foo.bar/baz:a", "foo_bar_baz:a")
|
|
|
|
f("foo...bar", "foo___bar")
|
|
|
|
}
|
|
|
|
|
2021-06-04 19:41:50 +02:00
|
|
|
func TestLabelsToString(t *testing.T) {
|
|
|
|
f := func(labels []prompbmarshal.Label, sExpected string) {
|
|
|
|
t.Helper()
|
2022-12-10 11:09:21 +01:00
|
|
|
s := LabelsToString(labels)
|
2021-06-04 19:41:50 +02:00
|
|
|
if s != sExpected {
|
|
|
|
t.Fatalf("unexpected result;\ngot\n%s\nwant\n%s", s, sExpected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f(nil, "{}")
|
|
|
|
f([]prompbmarshal.Label{
|
|
|
|
{
|
|
|
|
Name: "__name__",
|
|
|
|
Value: "foo",
|
|
|
|
},
|
|
|
|
}, "foo")
|
|
|
|
f([]prompbmarshal.Label{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
Value: "bar",
|
|
|
|
},
|
|
|
|
}, `{foo="bar"}`)
|
|
|
|
f([]prompbmarshal.Label{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
Value: "bar",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "a",
|
|
|
|
Value: "bc",
|
|
|
|
},
|
|
|
|
}, `{a="bc",foo="bar"}`)
|
|
|
|
f([]prompbmarshal.Label{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
Value: "bar",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "__name__",
|
|
|
|
Value: "xxx",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "a",
|
|
|
|
Value: "bc",
|
|
|
|
},
|
|
|
|
}, `xxx{a="bc",foo="bar"}`)
|
|
|
|
}
|
|
|
|
|
2022-12-10 11:09:21 +01:00
|
|
|
func TestParsedRelabelConfigsApplyDebug(t *testing.T) {
|
|
|
|
f := func(config, metric string, dssExpected []DebugStep) {
|
|
|
|
t.Helper()
|
|
|
|
pcs, err := ParseRelabelConfigsData([]byte(config))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("cannot parse %q: %s", config, err)
|
|
|
|
}
|
|
|
|
labels := promutils.MustNewLabelsFromString(metric)
|
|
|
|
_, dss := pcs.ApplyDebug(labels.GetLabels())
|
|
|
|
if !reflect.DeepEqual(dss, dssExpected) {
|
|
|
|
t.Fatalf("unexpected result; got\n%s\nwant\n%s", dss, dssExpected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// empty relabel config
|
|
|
|
f(``, `foo`, nil)
|
|
|
|
// add label
|
|
|
|
f(`
|
|
|
|
- target_label: abc
|
|
|
|
replacement: xyz
|
|
|
|
`, `foo{bar="baz"}`, []DebugStep{
|
|
|
|
{
|
|
|
|
Rule: "target_label: abc\nreplacement: xyz\n",
|
|
|
|
In: `foo{bar="baz"}`,
|
|
|
|
Out: `foo{abc="xyz",bar="baz"}`,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
// drop label
|
|
|
|
f(`
|
|
|
|
- target_label: bar
|
|
|
|
replacement: ''
|
|
|
|
`, `foo{bar="baz"}`, []DebugStep{
|
|
|
|
{
|
|
|
|
Rule: "target_label: bar\nreplacement: \"\"\n",
|
|
|
|
In: `foo{bar="baz"}`,
|
|
|
|
Out: `foo{bar=""}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Rule: "remove empty labels",
|
|
|
|
In: `foo{bar=""}`,
|
|
|
|
Out: `foo`,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
// drop metric
|
|
|
|
f(`
|
|
|
|
- action: drop
|
|
|
|
source_labels: [bar]
|
|
|
|
regex: baz
|
|
|
|
`, `foo{bar="baz",abc="def"}`, []DebugStep{
|
|
|
|
{
|
|
|
|
Rule: "action: drop\nsource_labels: [bar]\nregex: baz\n",
|
|
|
|
In: `foo{abc="def",bar="baz"}`,
|
|
|
|
Out: `{}`,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
// Multiple steps
|
|
|
|
f(`
|
|
|
|
- action: labeldrop
|
|
|
|
regex: "foo.*"
|
|
|
|
- target_label: foobar
|
|
|
|
replacement: "abc"
|
|
|
|
`, `m{foo="x",foobc="123",a="b"}`, []DebugStep{
|
|
|
|
{
|
|
|
|
Rule: "action: labeldrop\nregex: foo.*\n",
|
|
|
|
In: `m{a="b",foo="x",foobc="123"}`,
|
|
|
|
Out: `m{a="b"}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Rule: "target_label: foobar\nreplacement: abc\n",
|
|
|
|
In: `m{a="b"}`,
|
|
|
|
Out: `m{a="b",foobar="abc"}`,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParsedRelabelConfigsApply(t *testing.T) {
|
2022-08-21 21:46:54 +02:00
|
|
|
f := func(config, metric string, isFinalize bool, resultExpected string) {
|
2020-02-23 12:35:47 +01:00
|
|
|
t.Helper()
|
2022-12-10 11:09:21 +01:00
|
|
|
pcs, err := ParseRelabelConfigsData([]byte(config))
|
2021-02-22 15:33:55 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("cannot parse %q: %s", config, err)
|
|
|
|
}
|
2022-12-10 11:09:21 +01:00
|
|
|
labels := promutils.MustNewLabelsFromString(metric)
|
2022-11-30 06:22:12 +01:00
|
|
|
resultLabels := pcs.Apply(labels.GetLabels(), 0)
|
2022-10-09 13:51:14 +02:00
|
|
|
if isFinalize {
|
|
|
|
resultLabels = FinalizeLabels(resultLabels[:0], resultLabels)
|
|
|
|
}
|
|
|
|
SortLabels(resultLabels)
|
2022-12-10 11:09:21 +01:00
|
|
|
result := LabelsToString(resultLabels)
|
2022-08-21 21:46:54 +02:00
|
|
|
if result != resultExpected {
|
|
|
|
t.Fatalf("unexpected result; got\n%s\nwant\n%s", result, resultExpected)
|
2020-02-23 12:35:47 +01:00
|
|
|
}
|
|
|
|
}
|
2020-05-03 15:41:33 +02:00
|
|
|
t.Run("empty_relabel_configs", func(t *testing.T) {
|
2022-08-21 21:46:54 +02:00
|
|
|
f("", `{}`, false, `{}`)
|
|
|
|
f("", `{}`, true, `{}`)
|
|
|
|
f("", `{foo="bar"}`, false, `{foo="bar"}`)
|
|
|
|
f("", `xxx{foo="bar",__aaa="yyy"}`, false, `xxx{__aaa="yyy",foo="bar"}`)
|
|
|
|
f("", `xxx{foo="bar",__aaa="yyy"}`, true, `xxx{foo="bar"}`)
|
2020-02-23 12:35:47 +01:00
|
|
|
})
|
|
|
|
t.Run("replace-miss", func(t *testing.T) {
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: replace
|
|
|
|
target_label: bar
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{}`, false, `{}`)
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: replace
|
|
|
|
source_labels: ["foo"]
|
|
|
|
target_label: bar
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{}`, false, `{}`)
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: replace
|
|
|
|
source_labels: ["foo"]
|
|
|
|
target_label: "bar"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{xxx="yyy"}`, false, `{xxx="yyy"}`)
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: replace
|
|
|
|
source_labels: ["foo"]
|
|
|
|
target_label: "bar"
|
|
|
|
regex: ".+"
|
2022-10-17 13:47:52 +02:00
|
|
|
`, `{xxx="yyy"}`, false, `{xxx="yyy"}`)
|
|
|
|
f(`
|
|
|
|
- action: replace
|
|
|
|
source_labels: ["foo"]
|
|
|
|
target_label: "xxx"
|
|
|
|
regex: ".+"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{xxx="yyy"}`, false, `{xxx="yyy"}`)
|
2022-02-24 01:26:15 +01:00
|
|
|
})
|
|
|
|
t.Run("replace-if-miss", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: replace
|
|
|
|
if: '{foo="bar"}'
|
|
|
|
source_labels: ["xxx", "foo"]
|
|
|
|
target_label: "bar"
|
|
|
|
replacement: "a-$1-b"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{xxx="yyy"}`, false, `{xxx="yyy"}`)
|
2020-02-23 12:35:47 +01:00
|
|
|
})
|
|
|
|
t.Run("replace-hit", func(t *testing.T) {
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: replace
|
|
|
|
source_labels: ["xxx", "foo"]
|
|
|
|
target_label: "bar"
|
|
|
|
replacement: "a-$1-b"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{xxx="yyy"}`, false, `{bar="a-yyy;-b",xxx="yyy"}`)
|
2022-10-17 13:47:52 +02:00
|
|
|
f(`
|
|
|
|
- action: replace
|
|
|
|
source_labels: ["xxx", "foo"]
|
|
|
|
target_label: "xxx"
|
|
|
|
`, `{xxx="yyy"}`, false, `{xxx="yyy;"}`)
|
|
|
|
f(`
|
|
|
|
- action: replace
|
|
|
|
source_labels: ["foo"]
|
|
|
|
target_label: "xxx"
|
|
|
|
`, `{xxx="yyy"}`, false, `{}`)
|
2022-02-24 01:26:15 +01:00
|
|
|
})
|
|
|
|
t.Run("replace-if-hit", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: replace
|
|
|
|
if: '{xxx=~".y."}'
|
|
|
|
source_labels: ["xxx", "foo"]
|
|
|
|
target_label: "bar"
|
|
|
|
replacement: "a-$1-b"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{xxx="yyy"}`, false, `{bar="a-yyy;-b",xxx="yyy"}`)
|
2020-02-23 12:35:47 +01:00
|
|
|
})
|
2021-05-03 10:26:38 +02:00
|
|
|
t.Run("replace-remove-label-value-hit", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: replace
|
|
|
|
source_labels: ["foo"]
|
|
|
|
target_label: "foo"
|
|
|
|
regex: "xxx"
|
|
|
|
replacement: ""
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{foo="xxx",bar="baz"}`, false, `{bar="baz"}`)
|
2021-05-03 10:26:38 +02:00
|
|
|
})
|
|
|
|
t.Run("replace-remove-label-value-miss", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: replace
|
|
|
|
source_labels: ["foo"]
|
|
|
|
target_label: "foo"
|
|
|
|
regex: "xxx"
|
|
|
|
replacement: ""
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{foo="yyy",bar="baz"}`, false, `{bar="baz",foo="yyy"}`)
|
2021-05-03 10:26:38 +02:00
|
|
|
})
|
2021-05-17 23:22:30 +02:00
|
|
|
t.Run("replace-hit-remove-label", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: replace
|
|
|
|
source_labels: ["xxx", "foo"]
|
|
|
|
regex: "yyy;.+"
|
|
|
|
target_label: "foo"
|
|
|
|
replacement: ""
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{xxx="yyy",foo="bar"}`, false, `{xxx="yyy"}`)
|
2021-05-17 23:22:30 +02:00
|
|
|
})
|
|
|
|
t.Run("replace-miss-remove-label", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: replace
|
|
|
|
source_labels: ["xxx", "foo"]
|
|
|
|
regex: "yyy;.+"
|
|
|
|
target_label: "foo"
|
|
|
|
replacement: ""
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{xxx="yyyz",foo="bar"}`, false, `{foo="bar",xxx="yyyz"}`)
|
2021-05-17 23:22:30 +02:00
|
|
|
})
|
2020-06-19 01:20:29 +02:00
|
|
|
t.Run("replace-hit-target-label-with-capture-group", func(t *testing.T) {
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: replace
|
|
|
|
source_labels: ["xxx", "foo"]
|
|
|
|
target_label: "bar-$1"
|
|
|
|
replacement: "a-$1-b"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{xxx="yyy"}`, false, `{bar-yyy;="a-yyy;-b",xxx="yyy"}`)
|
2020-06-19 01:20:29 +02:00
|
|
|
})
|
2020-02-23 12:35:47 +01:00
|
|
|
t.Run("replace_all-miss", func(t *testing.T) {
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: replace_all
|
|
|
|
source_labels: [foo]
|
|
|
|
target_label: "bar"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{}`, false, `{}`)
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: replace_all
|
|
|
|
source_labels: ["foo"]
|
|
|
|
target_label: "bar"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{}`, false, `{}`)
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: replace_all
|
|
|
|
source_labels: ["foo"]
|
|
|
|
target_label: "bar"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{xxx="yyy"}`, false, `{xxx="yyy"}`)
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: replace_all
|
|
|
|
source_labels: ["foo"]
|
|
|
|
target_label: "bar"
|
|
|
|
regex: ".+"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{xxx="yyy"}`, false, `{xxx="yyy"}`)
|
2020-02-23 12:35:47 +01:00
|
|
|
})
|
2022-02-24 01:26:15 +01:00
|
|
|
t.Run("replace_all-if-miss", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: replace_all
|
|
|
|
if: 'foo'
|
|
|
|
source_labels: ["xxx"]
|
|
|
|
target_label: "xxx"
|
|
|
|
regex: "-"
|
|
|
|
replacement: "."
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{xxx="a-b-c"}`, false, `{xxx="a-b-c"}`)
|
2022-02-24 01:26:15 +01:00
|
|
|
})
|
2020-02-23 12:35:47 +01:00
|
|
|
t.Run("replace_all-hit", func(t *testing.T) {
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: replace_all
|
|
|
|
source_labels: ["xxx"]
|
|
|
|
target_label: "xxx"
|
|
|
|
regex: "-"
|
|
|
|
replacement: "."
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{xxx="a-b-c"}`, false, `{xxx="a.b.c"}`)
|
2022-02-24 01:26:15 +01:00
|
|
|
})
|
|
|
|
t.Run("replace_all-if-hit", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: replace_all
|
|
|
|
if: '{non_existing_label=~".*"}'
|
|
|
|
source_labels: ["xxx"]
|
|
|
|
target_label: "xxx"
|
|
|
|
regex: "-"
|
|
|
|
replacement: "."
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{xxx="a-b-c"}`, false, `{xxx="a.b.c"}`)
|
2021-02-21 23:50:57 +01:00
|
|
|
})
|
|
|
|
t.Run("replace_all-regex-hit", func(t *testing.T) {
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: replace_all
|
|
|
|
source_labels: ["xxx", "foo"]
|
|
|
|
target_label: "xxx"
|
|
|
|
regex: "(;)"
|
|
|
|
replacement: "-$1-"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{xxx="y;y"}`, false, `{xxx="y-;-y-;-"}`)
|
2020-02-23 12:35:47 +01:00
|
|
|
})
|
|
|
|
t.Run("replace-add-multi-labels", func(t *testing.T) {
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: replace
|
|
|
|
source_labels: ["xxx"]
|
|
|
|
target_label: "bar"
|
|
|
|
replacement: "a-$1"
|
|
|
|
- action: replace
|
|
|
|
source_labels: ["bar"]
|
|
|
|
target_label: "zar"
|
|
|
|
replacement: "b-$1"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{xxx="yyy",instance="a.bc"}`, true, `{bar="a-yyy",instance="a.bc",xxx="yyy",zar="b-a-yyy"}`)
|
2020-02-23 12:35:47 +01:00
|
|
|
})
|
|
|
|
t.Run("replace-self", func(t *testing.T) {
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: replace
|
|
|
|
source_labels: ["foo"]
|
|
|
|
target_label: "foo"
|
|
|
|
replacement: "a-$1"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{foo="aaxx"}`, true, `{foo="a-aaxx"}`)
|
2020-02-23 12:35:47 +01:00
|
|
|
})
|
|
|
|
t.Run("replace-missing-source", func(t *testing.T) {
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: replace
|
|
|
|
target_label: foo
|
|
|
|
replacement: "foobar"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{}`, true, `{foo="foobar"}`)
|
2020-02-23 12:35:47 +01:00
|
|
|
})
|
2020-06-23 16:17:58 +02:00
|
|
|
t.Run("keep_if_equal-miss", func(t *testing.T) {
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: keep_if_equal
|
|
|
|
source_labels: ["foo", "bar"]
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{}`, true, `{}`)
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: keep_if_equal
|
|
|
|
source_labels: ["xxx", "bar"]
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{xxx="yyy"}`, true, `{}`)
|
2020-06-23 16:17:58 +02:00
|
|
|
})
|
|
|
|
t.Run("keep_if_equal-hit", func(t *testing.T) {
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: keep_if_equal
|
|
|
|
source_labels: ["xxx", "bar"]
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{xxx="yyy",bar="yyy"}`, true, `{bar="yyy",xxx="yyy"}`)
|
2020-06-23 16:17:58 +02:00
|
|
|
})
|
|
|
|
t.Run("drop_if_equal-miss", func(t *testing.T) {
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: drop_if_equal
|
|
|
|
source_labels: ["foo", "bar"]
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{}`, true, `{}`)
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: drop_if_equal
|
|
|
|
source_labels: ["xxx", "bar"]
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{xxx="yyy"}`, true, `{xxx="yyy"}`)
|
2020-06-23 16:17:58 +02:00
|
|
|
})
|
|
|
|
t.Run("drop_if_equal-hit", func(t *testing.T) {
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: drop_if_equal
|
|
|
|
source_labels: [xxx, bar]
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{xxx="yyy",bar="yyy"}`, true, `{}`)
|
2020-06-23 16:17:58 +02:00
|
|
|
})
|
2020-02-23 12:35:47 +01:00
|
|
|
t.Run("keep-miss", func(t *testing.T) {
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: keep
|
|
|
|
source_labels: [foo]
|
|
|
|
regex: ".+"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{}`, true, `{}`)
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: keep
|
|
|
|
source_labels: [foo]
|
|
|
|
regex: ".+"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{xxx="yyy"}`, true, `{}`)
|
2020-02-23 12:35:47 +01:00
|
|
|
})
|
2022-02-24 01:26:15 +01:00
|
|
|
t.Run("keep-if-miss", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: keep
|
|
|
|
if: '{foo="bar"}'
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{foo="yyy"}`, false, `{}`)
|
2022-02-24 01:26:15 +01:00
|
|
|
})
|
|
|
|
t.Run("keep-if-hit", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: keep
|
|
|
|
if: '{foo="yyy"}'
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{foo="yyy"}`, false, `{foo="yyy"}`)
|
2022-02-24 01:26:15 +01:00
|
|
|
})
|
2020-02-23 12:35:47 +01:00
|
|
|
t.Run("keep-hit", func(t *testing.T) {
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: keep
|
|
|
|
source_labels: [foo]
|
|
|
|
regex: "yyy"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{foo="yyy"}`, false, `{foo="yyy"}`)
|
2021-02-21 23:50:57 +01:00
|
|
|
})
|
|
|
|
t.Run("keep-hit-regexp", func(t *testing.T) {
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: keep
|
|
|
|
source_labels: ["foo"]
|
|
|
|
regex: ".+"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{foo="yyy"}`, false, `{foo="yyy"}`)
|
2020-02-23 12:35:47 +01:00
|
|
|
})
|
2021-09-09 15:18:19 +02:00
|
|
|
t.Run("keep_metrics-miss", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: keep_metrics
|
|
|
|
regex:
|
|
|
|
- foo
|
|
|
|
- bar
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `xxx`, true, `{}`)
|
2021-09-09 15:18:19 +02:00
|
|
|
})
|
2022-02-24 01:26:15 +01:00
|
|
|
t.Run("keep_metrics-if-miss", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: keep_metrics
|
|
|
|
if: 'bar'
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `foo`, true, `{}`)
|
2022-02-24 01:26:15 +01:00
|
|
|
})
|
|
|
|
t.Run("keep_metrics-if-hit", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: keep_metrics
|
|
|
|
if: 'foo'
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `foo`, true, `foo`)
|
2022-02-24 01:26:15 +01:00
|
|
|
})
|
2021-09-09 15:18:19 +02:00
|
|
|
t.Run("keep_metrics-hit", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: keep_metrics
|
|
|
|
regex:
|
|
|
|
- foo
|
|
|
|
- bar
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `foo`, true, `foo`)
|
2021-09-09 15:18:19 +02:00
|
|
|
})
|
2020-02-23 12:35:47 +01:00
|
|
|
t.Run("drop-miss", func(t *testing.T) {
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: drop
|
|
|
|
source_labels: [foo]
|
|
|
|
regex: ".+"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{}`, false, `{}`)
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: drop
|
|
|
|
source_labels: [foo]
|
|
|
|
regex: ".+"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{xxx="yyy"}`, true, `{xxx="yyy"}`)
|
2020-02-23 12:35:47 +01:00
|
|
|
})
|
2022-02-24 01:26:15 +01:00
|
|
|
t.Run("drop-if-miss", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: drop
|
|
|
|
if: '{foo="bar"}'
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{foo="yyy"}`, true, `{foo="yyy"}`)
|
2022-02-24 01:26:15 +01:00
|
|
|
})
|
|
|
|
t.Run("drop-if-hit", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: drop
|
|
|
|
if: '{foo="yyy"}'
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{foo="yyy"}`, true, `{}`)
|
2022-02-24 01:26:15 +01:00
|
|
|
})
|
2020-02-23 12:35:47 +01:00
|
|
|
t.Run("drop-hit", func(t *testing.T) {
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: drop
|
|
|
|
source_labels: [foo]
|
|
|
|
regex: yyy
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{foo="yyy"}`, true, `{}`)
|
2021-02-21 23:50:57 +01:00
|
|
|
})
|
|
|
|
t.Run("drop-hit-regexp", func(t *testing.T) {
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: drop
|
|
|
|
source_labels: [foo]
|
|
|
|
regex: ".+"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{foo="yyy"}`, true, `{}`)
|
2020-02-23 12:35:47 +01:00
|
|
|
})
|
2021-09-09 15:18:19 +02:00
|
|
|
t.Run("drop_metrics-miss", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: drop_metrics
|
|
|
|
regex:
|
|
|
|
- foo
|
|
|
|
- bar
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `xxx`, true, `xxx`)
|
2021-09-09 15:18:19 +02:00
|
|
|
})
|
2022-02-24 01:26:15 +01:00
|
|
|
t.Run("drop_metrics-if-miss", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: drop_metrics
|
|
|
|
if: bar
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `foo`, true, `foo`)
|
2022-02-24 01:26:15 +01:00
|
|
|
})
|
|
|
|
t.Run("drop_metrics-if-hit", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: drop_metrics
|
|
|
|
if: foo
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `foo`, true, `{}`)
|
2022-02-24 01:26:15 +01:00
|
|
|
})
|
2021-09-09 15:18:19 +02:00
|
|
|
t.Run("drop_metrics-hit", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: drop_metrics
|
|
|
|
regex:
|
|
|
|
- foo
|
|
|
|
- bar
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `foo`, true, `{}`)
|
2021-09-09 15:18:19 +02:00
|
|
|
})
|
2020-02-23 12:35:47 +01:00
|
|
|
t.Run("hashmod-miss", func(t *testing.T) {
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: hashmod
|
|
|
|
source_labels: [foo]
|
|
|
|
target_label: aaa
|
|
|
|
modulus: 123
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{xxx="yyy"}`, false, `{aaa="81",xxx="yyy"}`)
|
2020-02-23 12:35:47 +01:00
|
|
|
})
|
2022-02-24 01:26:15 +01:00
|
|
|
t.Run("hashmod-if-miss", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: hashmod
|
|
|
|
if: '{foo="bar"}'
|
|
|
|
source_labels: [foo]
|
|
|
|
target_label: aaa
|
|
|
|
modulus: 123
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{foo="yyy"}`, true, `{foo="yyy"}`)
|
2022-02-24 01:26:15 +01:00
|
|
|
})
|
|
|
|
t.Run("hashmod-if-hit", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: hashmod
|
|
|
|
if: '{foo="yyy"}'
|
|
|
|
source_labels: [foo]
|
|
|
|
target_label: aaa
|
|
|
|
modulus: 123
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{foo="yyy"}`, true, `{aaa="73",foo="yyy"}`)
|
2022-02-24 01:26:15 +01:00
|
|
|
})
|
2020-02-23 12:35:47 +01:00
|
|
|
t.Run("hashmod-hit", func(t *testing.T) {
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: hashmod
|
|
|
|
source_labels: [foo]
|
|
|
|
target_label: aaa
|
|
|
|
modulus: 123
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{foo="yyy"}`, true, `{aaa="73",foo="yyy"}`)
|
2020-02-23 12:35:47 +01:00
|
|
|
})
|
2022-02-24 01:26:15 +01:00
|
|
|
t.Run("labelmap-copy-label-if-miss", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: labelmap
|
|
|
|
if: '{foo="yyy",foobar="aab"}'
|
|
|
|
regex: "foo"
|
|
|
|
replacement: "bar"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{foo="yyy",foobar="aaa"}`, true, `{foo="yyy",foobar="aaa"}`)
|
2022-02-24 01:26:15 +01:00
|
|
|
})
|
|
|
|
t.Run("labelmap-copy-label-if-hit", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: labelmap
|
|
|
|
if: '{foo="yyy",foobar="aaa"}'
|
|
|
|
regex: "foo"
|
|
|
|
replacement: "bar"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{foo="yyy",foobar="aaa"}`, true, `{bar="yyy",foo="yyy",foobar="aaa"}`)
|
2022-02-24 01:26:15 +01:00
|
|
|
})
|
2021-02-22 15:33:55 +01:00
|
|
|
t.Run("labelmap-copy-label", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: labelmap
|
|
|
|
regex: "foo"
|
|
|
|
replacement: "bar"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{foo="yyy",foobar="aaa"}`, true, `{bar="yyy",foo="yyy",foobar="aaa"}`)
|
2021-02-22 15:33:55 +01:00
|
|
|
})
|
|
|
|
t.Run("labelmap-remove-prefix-dot-star", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: labelmap
|
|
|
|
regex: "foo(.*)"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{xoo="yyy",foobar="aaa"}`, true, `{bar="aaa",foobar="aaa",xoo="yyy"}`)
|
2021-02-22 15:33:55 +01:00
|
|
|
})
|
|
|
|
t.Run("labelmap-remove-prefix-dot-plus", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: labelmap
|
|
|
|
regex: "foo(.+)"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{foo="yyy",foobar="aaa"}`, true, `{bar="aaa",foo="yyy",foobar="aaa"}`)
|
2020-02-23 12:35:47 +01:00
|
|
|
})
|
2021-02-22 15:33:55 +01:00
|
|
|
t.Run("labelmap-regex", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: labelmap
|
|
|
|
regex: "foo(.+)"
|
|
|
|
replacement: "$1-x"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{foo="yyy",foobar="aaa"}`, true, `{bar-x="aaa",foo="yyy",foobar="aaa"}`)
|
2021-02-22 15:33:55 +01:00
|
|
|
})
|
2022-02-24 01:26:15 +01:00
|
|
|
t.Run("labelmap_all-if-miss", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: labelmap_all
|
|
|
|
if: foobar
|
|
|
|
regex: "\\."
|
|
|
|
replacement: "-"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{foo.bar.baz="yyy",foobar="aaa"}`, true, `{foo.bar.baz="yyy",foobar="aaa"}`)
|
2022-02-24 01:26:15 +01:00
|
|
|
})
|
|
|
|
t.Run("labelmap_all-if-hit", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: labelmap_all
|
|
|
|
if: '{foo.bar.baz="yyy"}'
|
|
|
|
regex: "\\."
|
|
|
|
replacement: "-"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{foo.bar.baz="yyy",foobar="aaa"}`, true, `{foo-bar-baz="yyy",foobar="aaa"}`)
|
2022-02-24 01:26:15 +01:00
|
|
|
})
|
2021-02-22 15:33:55 +01:00
|
|
|
t.Run("labelmap_all", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: labelmap_all
|
|
|
|
regex: "\\."
|
|
|
|
replacement: "-"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{foo.bar.baz="yyy",foobar="aaa"}`, true, `{foo-bar-baz="yyy",foobar="aaa"}`)
|
2020-02-23 12:35:47 +01:00
|
|
|
})
|
2021-02-21 23:50:57 +01:00
|
|
|
t.Run("labelmap_all-regexp", func(t *testing.T) {
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: labelmap_all
|
|
|
|
regex: "ba(.)"
|
|
|
|
replacement: "${1}ss"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{foo.bar.baz="yyy",foozar="aaa"}`, true, `{foo.rss.zss="yyy",foozar="aaa"}`)
|
2021-02-21 23:50:57 +01:00
|
|
|
})
|
2020-02-23 12:35:47 +01:00
|
|
|
t.Run("labeldrop", func(t *testing.T) {
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: labeldrop
|
|
|
|
regex: dropme
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{aaa="bbb"}`, true, `{aaa="bbb"}`)
|
2022-02-24 01:26:15 +01:00
|
|
|
// if-miss
|
|
|
|
f(`
|
|
|
|
- action: labeldrop
|
|
|
|
if: foo
|
|
|
|
regex: dropme
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{xxx="yyy",dropme="aaa",foo="bar"}`, false, `{dropme="aaa",foo="bar",xxx="yyy"}`)
|
2022-02-24 01:26:15 +01:00
|
|
|
// if-hit
|
|
|
|
f(`
|
|
|
|
- action: labeldrop
|
|
|
|
if: '{xxx="yyy"}'
|
|
|
|
regex: dropme
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{xxx="yyy",dropme="aaa",foo="bar"}`, false, `{foo="bar",xxx="yyy"}`)
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: labeldrop
|
|
|
|
regex: dropme
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{xxx="yyy",dropme="aaa",foo="bar"}`, false, `{foo="bar",xxx="yyy"}`)
|
2021-07-27 09:50:39 +02:00
|
|
|
// regex in single quotes
|
|
|
|
f(`
|
|
|
|
- action: labeldrop
|
|
|
|
regex: 'dropme'
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{xxx="yyy",dropme="aaa"}`, false, `{xxx="yyy"}`)
|
2021-07-27 09:50:39 +02:00
|
|
|
// regex in double quotes
|
|
|
|
f(`
|
|
|
|
- action: labeldrop
|
|
|
|
regex: "dropme"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{xxx="yyy",dropme="aaa"}`, false, `{xxx="yyy"}`)
|
2021-02-21 23:50:57 +01:00
|
|
|
})
|
2021-02-24 16:57:50 +01:00
|
|
|
t.Run("labeldrop-prefix", func(t *testing.T) {
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: labeldrop
|
|
|
|
regex: "dropme.*"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{aaa="bbb"}`, true, `{aaa="bbb"}`)
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: labeldrop
|
2021-02-24 16:57:50 +01:00
|
|
|
regex: "dropme(.+)"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{xxx="yyy",dropme-please="aaa",foo="bar"}`, false, `{foo="bar",xxx="yyy"}`)
|
2021-02-24 16:57:50 +01:00
|
|
|
})
|
|
|
|
t.Run("labeldrop-regexp", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: labeldrop
|
|
|
|
regex: ".*dropme.*"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{aaa="bbb"}`, true, `{aaa="bbb"}`)
|
2021-02-24 16:57:50 +01:00
|
|
|
f(`
|
|
|
|
- action: labeldrop
|
|
|
|
regex: ".*dropme.*"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{xxx="yyy",dropme-please="aaa",foo="bar"}`, false, `{foo="bar",xxx="yyy"}`)
|
2020-02-23 12:35:47 +01:00
|
|
|
})
|
|
|
|
t.Run("labelkeep", func(t *testing.T) {
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: labelkeep
|
|
|
|
regex: "keepme"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{keepme="aaa"}`, true, `{keepme="aaa"}`)
|
2022-02-24 01:26:15 +01:00
|
|
|
// if-miss
|
|
|
|
f(`
|
|
|
|
- action: labelkeep
|
|
|
|
if: '{aaaa="awefx"}'
|
|
|
|
regex: keepme
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{keepme="aaa",aaaa="awef",keepme-aaa="234"}`, false, `{aaaa="awef",keepme="aaa",keepme-aaa="234"}`)
|
2022-02-24 01:26:15 +01:00
|
|
|
// if-hit
|
|
|
|
f(`
|
|
|
|
- action: labelkeep
|
|
|
|
if: '{aaaa="awef"}'
|
|
|
|
regex: keepme
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{keepme="aaa",aaaa="awef",keepme-aaa="234"}`, false, `{keepme="aaa"}`)
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: labelkeep
|
|
|
|
regex: keepme
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{keepme="aaa",aaaa="awef",keepme-aaa="234"}`, false, `{keepme="aaa"}`)
|
2021-02-21 23:50:57 +01:00
|
|
|
})
|
|
|
|
t.Run("labelkeep-regexp", func(t *testing.T) {
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: labelkeep
|
|
|
|
regex: "keepme.*"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{keepme="aaa"}`, true, `{keepme="aaa"}`)
|
2021-02-22 15:33:55 +01:00
|
|
|
f(`
|
|
|
|
- action: labelkeep
|
|
|
|
regex: "keepme.*"
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{keepme="aaa",aaaa="awef",keepme-aaa="234"}`, false, `{keepme="aaa",keepme-aaa="234"}`)
|
2020-02-23 12:35:47 +01:00
|
|
|
})
|
2022-06-01 10:02:37 +02:00
|
|
|
t.Run("upper-lower-case", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: uppercase
|
|
|
|
source_labels: ["foo"]
|
|
|
|
target_label: foo
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{foo="bar"}`, true, `{foo="BAR"}`)
|
2022-06-01 10:02:37 +02:00
|
|
|
f(`
|
|
|
|
- action: lowercase
|
|
|
|
source_labels: ["foo", "bar"]
|
|
|
|
target_label: baz
|
|
|
|
- action: labeldrop
|
|
|
|
regex: foo|bar
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{foo="BaR",bar="fOO"}`, true, `{baz="bar;foo"}`)
|
2022-06-16 19:24:19 +02:00
|
|
|
f(`
|
2022-06-01 10:02:37 +02:00
|
|
|
- action: lowercase
|
|
|
|
source_labels: ["foo"]
|
|
|
|
target_label: baz
|
|
|
|
- action: uppercase
|
|
|
|
source_labels: ["bar"]
|
|
|
|
target_label: baz
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `{qux="quux"}`, true, `{qux="quux"}`)
|
2022-06-16 19:24:19 +02:00
|
|
|
})
|
|
|
|
t.Run("graphite-match", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: graphite
|
|
|
|
match: foo.*.baz
|
|
|
|
labels:
|
|
|
|
__name__: aaa
|
|
|
|
job: ${1}-zz
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `foo.bar.baz`, true, `aaa{job="bar-zz"}`)
|
2022-06-16 19:24:19 +02:00
|
|
|
})
|
|
|
|
t.Run("graphite-mismatch", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- action: graphite
|
|
|
|
match: foo.*.baz
|
|
|
|
labels:
|
|
|
|
__name__: aaa
|
|
|
|
job: ${1}-zz
|
2022-08-21 21:46:54 +02:00
|
|
|
`, `foo.bar.bazz`, true, `foo.bar.bazz`)
|
|
|
|
})
|
|
|
|
t.Run("replacement-with-label-refs", func(t *testing.T) {
|
|
|
|
// no regex
|
|
|
|
f(`
|
|
|
|
- target_label: abc
|
|
|
|
replacement: "{{__name__}}.{{foo}}"
|
|
|
|
`, `qwe{foo="bar",baz="aaa"}`, true, `qwe{abc="qwe.bar",baz="aaa",foo="bar"}`)
|
|
|
|
// with regex
|
|
|
|
f(`
|
|
|
|
- target_label: abc
|
|
|
|
replacement: "{{__name__}}.{{foo}}.$1"
|
|
|
|
source_labels: [baz]
|
|
|
|
regex: "a(.+)"
|
|
|
|
`, `qwe{foo="bar",baz="aaa"}`, true, `qwe{abc="qwe.bar.aa",baz="aaa",foo="bar"}`)
|
2022-06-01 10:02:37 +02:00
|
|
|
})
|
2022-09-30 07:13:56 +02:00
|
|
|
// Check $ at the end of regex - see https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3131
|
|
|
|
t.Run("replacement-with-$-at-the-end-of-regex", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- target_label: xyz
|
|
|
|
regex: "foo\\$$"
|
|
|
|
replacement: bar
|
|
|
|
source_labels: [xyz]
|
|
|
|
`, `metric{xyz="foo$",a="b"}`, true, `metric{a="b",xyz="bar"}`)
|
|
|
|
})
|
2022-10-29 03:33:34 +02:00
|
|
|
t.Run("issue-3251", func(t *testing.T) {
|
|
|
|
f(`
|
|
|
|
- source_labels: [instance, container_label_com_docker_swarm_task_name]
|
|
|
|
separator: ';'
|
|
|
|
# regex: '(.*?)\..*;(.*?)\..*'
|
|
|
|
regex: '([^.]+).[^;]+;([^.]+).+'
|
|
|
|
replacement: '$2:$1'
|
|
|
|
target_label: container_label_com_docker_swarm_task_name
|
|
|
|
action: replace
|
|
|
|
`, `{instance="subdomain.domain.com",container_label_com_docker_swarm_task_name="myservice.h408nlaxmv8oqkn1pjjtd71to.nv987lz99rb27lkjjnfiay0g4"}`, true,
|
2022-10-29 04:17:10 +02:00
|
|
|
`{container_label_com_docker_swarm_task_name="myservice:subdomain",instance="subdomain.domain.com"}`)
|
2022-10-29 03:33:34 +02:00
|
|
|
})
|
2020-02-23 12:35:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFinalizeLabels(t *testing.T) {
|
2022-08-21 21:46:54 +02:00
|
|
|
f := func(metric, resultExpected string) {
|
2020-02-23 12:35:47 +01:00
|
|
|
t.Helper()
|
2022-12-10 11:09:21 +01:00
|
|
|
labels := promutils.MustNewLabelsFromString(metric)
|
2022-11-30 06:22:12 +01:00
|
|
|
resultLabels := FinalizeLabels(nil, labels.GetLabels())
|
2022-12-10 11:09:21 +01:00
|
|
|
result := LabelsToString(resultLabels)
|
2022-08-21 21:46:54 +02:00
|
|
|
if result != resultExpected {
|
|
|
|
t.Fatalf("unexpected result; got\n%s\nwant\n%s", result, resultExpected)
|
2020-02-23 12:35:47 +01:00
|
|
|
}
|
|
|
|
}
|
2022-08-21 21:46:54 +02:00
|
|
|
f(`{}`, `{}`)
|
|
|
|
f(`{foo="bar",__aaa="ass",instance="foo.com"}`, `{foo="bar",instance="foo.com"}`)
|
|
|
|
f(`{foo="bar",instance="ass",__address__="foo.com"}`, `{foo="bar",instance="ass"}`)
|
|
|
|
f(`{foo="bar",abc="def",__address__="foo.com"}`, `{abc="def",foo="bar"}`)
|
2020-02-23 12:35:47 +01:00
|
|
|
}
|
2020-04-14 11:21:10 +02:00
|
|
|
|
2022-08-21 21:46:54 +02:00
|
|
|
func TestFillLabelReferences(t *testing.T) {
|
|
|
|
f := func(replacement, metric, resultExpected string) {
|
|
|
|
t.Helper()
|
2022-12-10 11:09:21 +01:00
|
|
|
labels := promutils.MustNewLabelsFromString(metric)
|
2022-11-30 06:22:12 +01:00
|
|
|
result := fillLabelReferences(nil, replacement, labels.GetLabels())
|
2022-08-21 21:46:54 +02:00
|
|
|
if string(result) != resultExpected {
|
|
|
|
t.Fatalf("unexpected result; got\n%q\nwant\n%q", result, resultExpected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f(``, `foo{bar="baz"}`, ``)
|
|
|
|
f(`abc`, `foo{bar="baz"}`, `abc`)
|
|
|
|
f(`foo{{bar`, `foo{bar="baz"}`, `foo{{bar`)
|
|
|
|
f(`foo-$1`, `foo{bar="baz"}`, `foo-$1`)
|
|
|
|
f(`foo{{bar}}`, `foo{bar="baz"}`, `foobaz`)
|
|
|
|
f(`{{bar}}`, `foo{bar="baz"}`, `baz`)
|
|
|
|
f(`{{bar}}-aa`, `foo{bar="baz"}`, `baz-aa`)
|
|
|
|
f(`{{bar}}-aa{{__name__}}.{{bar}}{{non-existing-label}}`, `foo{bar="baz"}`, `baz-aafoo.baz`)
|
2020-04-14 11:21:10 +02:00
|
|
|
}
|
2022-08-24 16:54:26 +02:00
|
|
|
|
2022-08-26 14:23:41 +02:00
|
|
|
func TestRegexMatchStringSuccess(t *testing.T) {
|
2022-08-24 16:54:26 +02:00
|
|
|
f := func(pattern, s string) {
|
|
|
|
t.Helper()
|
2022-08-25 22:22:03 +02:00
|
|
|
prc := newTestRegexRelabelConfig(pattern)
|
2022-08-26 14:23:41 +02:00
|
|
|
if !prc.regex.MatchString(s) {
|
|
|
|
t.Fatalf("unexpected MatchString(%q) result; got false; want true", s)
|
2022-08-24 16:54:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
f("", "")
|
|
|
|
f("foo", "foo")
|
|
|
|
f(".*", "")
|
|
|
|
f(".*", "foo")
|
|
|
|
f("foo.*", "foobar")
|
|
|
|
f("foo.+", "foobar")
|
|
|
|
f("f.+o", "foo")
|
|
|
|
f("foo|bar", "bar")
|
|
|
|
f("^(foo|bar)$", "foo")
|
|
|
|
f("foo.+", "foobar")
|
|
|
|
f("^foo$", "foo")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRegexpMatchStringFailure(t *testing.T) {
|
|
|
|
f := func(pattern, s string) {
|
|
|
|
t.Helper()
|
2022-08-25 22:22:03 +02:00
|
|
|
prc := newTestRegexRelabelConfig(pattern)
|
2022-08-26 14:23:41 +02:00
|
|
|
if prc.regex.MatchString(s) {
|
|
|
|
t.Fatalf("unexpected MatchString(%q) result; got true; want false", s)
|
2022-08-24 16:54:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
f("", "foo")
|
|
|
|
f("foo", "")
|
|
|
|
f("foo.*", "foa")
|
|
|
|
f("foo.+", "foo")
|
|
|
|
f("f.+o", "foor")
|
|
|
|
f("foo|bar", "barz")
|
|
|
|
f("^(foo|bar)$", "xfoo")
|
|
|
|
f("foo.+", "foo")
|
|
|
|
f("^foo$", "foobar")
|
|
|
|
}
|
2022-08-25 22:22:03 +02:00
|
|
|
|
|
|
|
func newTestRegexRelabelConfig(pattern string) *parsedRelabelConfig {
|
|
|
|
rc := &RelabelConfig{
|
|
|
|
Action: "labeldrop",
|
|
|
|
Regex: &MultiLineRegex{
|
|
|
|
S: pattern,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
prc, err := parseRelabelConfig(rc)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("unexpected error in parseRelabelConfig: %s", err))
|
|
|
|
}
|
|
|
|
return prc
|
|
|
|
}
|