2020-02-23 11:35:47 +00:00
|
|
|
package promrelabel
|
|
|
|
|
|
|
|
import (
|
2022-08-25 20:22:03 +00:00
|
|
|
"fmt"
|
2022-12-10 10:09:21 +00:00
|
|
|
"reflect"
|
2020-02-23 11:35:47 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
2022-11-30 05:22:12 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
2020-02-23 11:35:47 +00:00
|
|
|
)
|
|
|
|
|
2023-08-14 14:14:40 +00:00
|
|
|
func TestSanitizeMetricName(t *testing.T) {
|
2022-09-28 06:59:36 +00:00
|
|
|
f := func(s, resultExpected string) {
|
|
|
|
t.Helper()
|
|
|
|
for i := 0; i < 5; i++ {
|
2023-08-14 14:14:40 +00:00
|
|
|
result := SanitizeMetricName(s)
|
2022-09-28 06:59:36 +00:00
|
|
|
if result != resultExpected {
|
2023-08-14 14:14:40 +00:00
|
|
|
t.Fatalf("unexpected result for SanitizeMetricName(%q) at iteration %d; got %q; want %q", s, i, result, resultExpected)
|
2022-09-28 06:59:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f("", "")
|
|
|
|
f("a", "a")
|
|
|
|
f("foo.bar/baz:a", "foo_bar_baz:a")
|
|
|
|
f("foo...bar", "foo___bar")
|
|
|
|
}
|
|
|
|
|
2023-08-14 14:14:40 +00:00
|
|
|
func TestSanitizeLabelName(t *testing.T) {
|
|
|
|
f := func(s, resultExpected string) {
|
|
|
|
t.Helper()
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
result := SanitizeLabelName(s)
|
|
|
|
if result != resultExpected {
|
|
|
|
t.Fatalf("unexpected result for SanitizeLabelName(%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 17:41:50 +00:00
|
|
|
func TestLabelsToString(t *testing.T) {
|
|
|
|
f := func(labels []prompbmarshal.Label, sExpected string) {
|
|
|
|
t.Helper()
|
2022-12-10 10:09:21 +00:00
|
|
|
s := LabelsToString(labels)
|
2021-06-04 17:41:50 +00: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 10:09:21 +00: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 19:46:54 +00:00
|
|
|
f := func(config, metric string, isFinalize bool, resultExpected string) {
|
2020-02-23 11:35:47 +00:00
|
|
|
t.Helper()
|
2022-12-10 10:09:21 +00:00
|
|
|
pcs, err := ParseRelabelConfigsData([]byte(config))
|
2021-02-22 14:33:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("cannot parse %q: %s", config, err)
|
|
|
|
}
|
2022-12-10 10:09:21 +00:00
|
|
|
labels := promutils.MustNewLabelsFromString(metric)
|
2022-11-30 05:22:12 +00:00
|
|
|
resultLabels := pcs.Apply(labels.GetLabels(), 0)
|
2022-10-09 11:51:14 +00:00
|
|
|
if isFinalize {
|
|
|
|
resultLabels = FinalizeLabels(resultLabels[:0], resultLabels)
|
|
|
|
}
|
|
|
|
SortLabels(resultLabels)
|
2022-12-10 10:09:21 +00:00
|
|
|
result := LabelsToString(resultLabels)
|
2022-08-21 19:46:54 +00:00
|
|
|
if result != resultExpected {
|
|
|
|
t.Fatalf("unexpected result; got\n%s\nwant\n%s", result, resultExpected)
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// empty_relabel_configs
|
|
|
|
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"}`)
|
|
|
|
|
|
|
|
// replace-miss"
|
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: replace
|
|
|
|
target_label: bar
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{}`, false, `{}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: replace
|
|
|
|
source_labels: ["foo"]
|
|
|
|
target_label: bar
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{}`, false, `{}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: replace
|
|
|
|
source_labels: ["foo"]
|
|
|
|
target_label: "bar"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{xxx="yyy"}`, false, `{xxx="yyy"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: replace
|
|
|
|
source_labels: ["foo"]
|
|
|
|
target_label: "bar"
|
|
|
|
regex: ".+"
|
2022-10-17 11:47:52 +00:00
|
|
|
`, `{xxx="yyy"}`, false, `{xxx="yyy"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
f(`
|
2022-10-17 11:47:52 +00:00
|
|
|
- action: replace
|
|
|
|
source_labels: ["foo"]
|
|
|
|
target_label: "xxx"
|
|
|
|
regex: ".+"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{xxx="yyy"}`, false, `{xxx="yyy"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// replace-if-miss
|
|
|
|
f(`
|
2022-02-24 00:26:15 +00:00
|
|
|
- action: replace
|
|
|
|
if: '{foo="bar"}'
|
|
|
|
source_labels: ["xxx", "foo"]
|
|
|
|
target_label: "bar"
|
|
|
|
replacement: "a-$1-b"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{xxx="yyy"}`, false, `{xxx="yyy"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// replace-hit
|
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: replace
|
|
|
|
source_labels: ["xxx", "foo"]
|
|
|
|
target_label: "bar"
|
|
|
|
replacement: "a-$1-b"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{xxx="yyy"}`, false, `{bar="a-yyy;-b",xxx="yyy"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
f(`
|
2022-10-17 11:47:52 +00:00
|
|
|
- action: replace
|
|
|
|
source_labels: ["xxx", "foo"]
|
|
|
|
target_label: "xxx"
|
|
|
|
`, `{xxx="yyy"}`, false, `{xxx="yyy;"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
f(`
|
2022-10-17 11:47:52 +00:00
|
|
|
- action: replace
|
|
|
|
source_labels: ["foo"]
|
|
|
|
target_label: "xxx"
|
|
|
|
`, `{xxx="yyy"}`, false, `{}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// replace-if-hit
|
|
|
|
f(`
|
2022-02-24 00:26:15 +00:00
|
|
|
- action: replace
|
|
|
|
if: '{xxx=~".y."}'
|
|
|
|
source_labels: ["xxx", "foo"]
|
|
|
|
target_label: "bar"
|
|
|
|
replacement: "a-$1-b"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{xxx="yyy"}`, false, `{bar="a-yyy;-b",xxx="yyy"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// replace-remove-label-value-hit
|
|
|
|
f(`
|
2021-05-03 08:26:38 +00:00
|
|
|
- action: replace
|
|
|
|
source_labels: ["foo"]
|
|
|
|
target_label: "foo"
|
|
|
|
regex: "xxx"
|
|
|
|
replacement: ""
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{foo="xxx",bar="baz"}`, false, `{bar="baz"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// replace-remove-label-value-miss
|
|
|
|
f(`
|
2021-05-03 08:26:38 +00:00
|
|
|
- action: replace
|
|
|
|
source_labels: ["foo"]
|
|
|
|
target_label: "foo"
|
|
|
|
regex: "xxx"
|
|
|
|
replacement: ""
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{foo="yyy",bar="baz"}`, false, `{bar="baz",foo="yyy"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// replace-hit-remove-label
|
|
|
|
f(`
|
2021-05-17 21:22:30 +00:00
|
|
|
- action: replace
|
|
|
|
source_labels: ["xxx", "foo"]
|
|
|
|
regex: "yyy;.+"
|
|
|
|
target_label: "foo"
|
|
|
|
replacement: ""
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{xxx="yyy",foo="bar"}`, false, `{xxx="yyy"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// replace-miss-remove-label
|
|
|
|
f(`
|
2021-05-17 21:22:30 +00:00
|
|
|
- action: replace
|
|
|
|
source_labels: ["xxx", "foo"]
|
|
|
|
regex: "yyy;.+"
|
|
|
|
target_label: "foo"
|
|
|
|
replacement: ""
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{xxx="yyyz",foo="bar"}`, false, `{foo="bar",xxx="yyyz"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// replace-hit-target-label-with-capture-group
|
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: replace
|
|
|
|
source_labels: ["xxx", "foo"]
|
|
|
|
target_label: "bar-$1"
|
|
|
|
replacement: "a-$1-b"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{xxx="yyy"}`, false, `{bar-yyy;="a-yyy;-b",xxx="yyy"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// replace_all-miss
|
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: replace_all
|
|
|
|
source_labels: [foo]
|
|
|
|
target_label: "bar"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{}`, false, `{}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: replace_all
|
|
|
|
source_labels: ["foo"]
|
|
|
|
target_label: "bar"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{}`, false, `{}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: replace_all
|
|
|
|
source_labels: ["foo"]
|
|
|
|
target_label: "bar"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{xxx="yyy"}`, false, `{xxx="yyy"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: replace_all
|
|
|
|
source_labels: ["foo"]
|
|
|
|
target_label: "bar"
|
|
|
|
regex: ".+"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{xxx="yyy"}`, false, `{xxx="yyy"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// replace_all-if-miss
|
|
|
|
f(`
|
2022-02-24 00:26:15 +00:00
|
|
|
- action: replace_all
|
|
|
|
if: 'foo'
|
|
|
|
source_labels: ["xxx"]
|
|
|
|
target_label: "xxx"
|
|
|
|
regex: "-"
|
|
|
|
replacement: "."
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{xxx="a-b-c"}`, false, `{xxx="a-b-c"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// replace_all-hit
|
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: replace_all
|
|
|
|
source_labels: ["xxx"]
|
|
|
|
target_label: "xxx"
|
|
|
|
regex: "-"
|
|
|
|
replacement: "."
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{xxx="a-b-c"}`, false, `{xxx="a.b.c"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// replace_all-if-hit
|
|
|
|
f(`
|
2022-02-24 00:26:15 +00:00
|
|
|
- action: replace_all
|
|
|
|
if: '{non_existing_label=~".*"}'
|
|
|
|
source_labels: ["xxx"]
|
|
|
|
target_label: "xxx"
|
|
|
|
regex: "-"
|
|
|
|
replacement: "."
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{xxx="a-b-c"}`, false, `{xxx="a.b.c"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// replace_all-regex-hit
|
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: replace_all
|
|
|
|
source_labels: ["xxx", "foo"]
|
|
|
|
target_label: "xxx"
|
|
|
|
regex: "(;)"
|
|
|
|
replacement: "-$1-"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{xxx="y;y"}`, false, `{xxx="y-;-y-;-"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// replace-add-multi-labels
|
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- 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 19:46:54 +00:00
|
|
|
`, `{xxx="yyy",instance="a.bc"}`, true, `{bar="a-yyy",instance="a.bc",xxx="yyy",zar="b-a-yyy"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// replace-self
|
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: replace
|
|
|
|
source_labels: ["foo"]
|
|
|
|
target_label: "foo"
|
|
|
|
replacement: "a-$1"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{foo="aaxx"}`, true, `{foo="a-aaxx"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// replace-missing-source
|
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: replace
|
|
|
|
target_label: foo
|
|
|
|
replacement: "foobar"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{}`, true, `{foo="foobar"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// keep_if_contains-non-existing-target-and-source
|
|
|
|
f(`
|
2023-11-29 09:00:48 +00:00
|
|
|
- action: keep_if_contains
|
|
|
|
target_label: foo
|
|
|
|
source_labels: [bar]
|
|
|
|
`, `{x="y"}`, true, `{x="y"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// keep_if_contains-non-existing-target
|
|
|
|
f(`
|
2023-11-29 09:00:48 +00:00
|
|
|
- action: keep_if_contains
|
|
|
|
target_label: foo
|
|
|
|
source_labels: [bar]
|
|
|
|
`, `{bar="aaa"}`, true, `{}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// keep_if_contains-non-existing-source
|
|
|
|
f(`
|
2023-11-29 09:00:48 +00:00
|
|
|
- action: keep_if_contains
|
|
|
|
target_label: foo
|
|
|
|
source_labels: [bar]
|
|
|
|
`, `{foo="aaa"}`, true, `{foo="aaa"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// keep_if_contains-matching-source-target
|
|
|
|
f(`
|
2023-11-29 09:00:48 +00:00
|
|
|
- action: keep_if_contains
|
|
|
|
target_label: foo
|
|
|
|
source_labels: [bar]
|
|
|
|
`, `{bar="aaa",foo="aaa"}`, true, `{bar="aaa",foo="aaa"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// keep_if_contains-matching-sources-target
|
|
|
|
f(`
|
2023-11-29 09:00:48 +00:00
|
|
|
- action: keep_if_contains
|
|
|
|
target_label: foo
|
|
|
|
source_labels: [bar, baz]
|
|
|
|
`, `{bar="aaa",foo="aaa",baz="aaa"}`, true, `{bar="aaa",baz="aaa",foo="aaa"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// keep_if_contains-mismatching-source-target
|
|
|
|
f(`
|
2023-11-29 09:00:48 +00:00
|
|
|
- action: keep_if_contains
|
|
|
|
target_label: foo
|
|
|
|
source_labels: [bar]
|
|
|
|
`, `{bar="aaa",foo="bbb"}`, true, `{}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// keep_if_contains-mismatching-sources-target
|
|
|
|
f(`
|
2023-11-29 09:00:48 +00:00
|
|
|
- action: keep_if_contains
|
|
|
|
target_label: foo
|
|
|
|
source_labels: [bar, baz]
|
|
|
|
`, `{bar="aaa",foo="aaa",baz="bbb"}`, true, `{}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// drop_if_contains-non-existing-target-and-source
|
|
|
|
f(`
|
2023-11-29 09:00:48 +00:00
|
|
|
- action: drop_if_contains
|
|
|
|
target_label: foo
|
|
|
|
source_labels: [bar]
|
|
|
|
`, `{x="y"}`, true, `{}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// drop_if_contains-non-existing-target
|
|
|
|
f(`
|
2023-11-29 09:00:48 +00:00
|
|
|
- action: drop_if_contains
|
|
|
|
target_label: foo
|
|
|
|
source_labels: [bar]
|
|
|
|
`, `{bar="aaa"}`, true, `{bar="aaa"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// drop_if_contains-non-existing-source
|
|
|
|
f(`
|
2023-11-29 09:00:48 +00:00
|
|
|
- action: drop_if_contains
|
|
|
|
target_label: foo
|
|
|
|
source_labels: [bar]
|
|
|
|
`, `{foo="aaa"}`, true, `{}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// drop_if_contains-matching-source-target
|
|
|
|
f(`
|
2023-11-29 09:00:48 +00:00
|
|
|
- action: drop_if_contains
|
|
|
|
target_label: foo
|
|
|
|
source_labels: [bar]
|
|
|
|
`, `{bar="aaa",foo="aaa"}`, true, `{}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// drop_if_contains-matching-sources-target
|
|
|
|
f(`
|
2023-11-29 09:00:48 +00:00
|
|
|
- action: drop_if_contains
|
|
|
|
target_label: foo
|
|
|
|
source_labels: [bar, baz]
|
|
|
|
`, `{bar="aaa",foo="aaa",baz="aaa"}`, true, `{}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// drop_if_contains-mismatching-source-target
|
|
|
|
f(`
|
2023-11-29 09:00:48 +00:00
|
|
|
- action: drop_if_contains
|
|
|
|
target_label: foo
|
|
|
|
source_labels: [bar]
|
|
|
|
`, `{bar="aaa",foo="bbb"}`, true, `{bar="aaa",foo="bbb"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// drop_if_contains-mismatching-sources-target
|
|
|
|
f(`
|
2023-11-29 09:00:48 +00:00
|
|
|
- action: drop_if_contains
|
|
|
|
target_label: foo
|
|
|
|
source_labels: [bar, baz]
|
|
|
|
`, `{bar="aaa",foo="aaa",baz="bbb"}`, true, `{bar="aaa",baz="bbb",foo="aaa"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// keep_if_equal-miss
|
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: keep_if_equal
|
|
|
|
source_labels: ["foo", "bar"]
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{}`, true, `{}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: keep_if_equal
|
|
|
|
source_labels: ["xxx", "bar"]
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{xxx="yyy"}`, true, `{}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// keep_if_equal-hit
|
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: keep_if_equal
|
|
|
|
source_labels: ["xxx", "bar"]
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{xxx="yyy",bar="yyy"}`, true, `{bar="yyy",xxx="yyy"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// drop_if_equal-miss
|
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: drop_if_equal
|
|
|
|
source_labels: ["foo", "bar"]
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{}`, true, `{}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: drop_if_equal
|
|
|
|
source_labels: ["xxx", "bar"]
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{xxx="yyy"}`, true, `{xxx="yyy"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// drop_if_equal-hit
|
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: drop_if_equal
|
|
|
|
source_labels: [xxx, bar]
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{xxx="yyy",bar="yyy"}`, true, `{}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// keepequal-hit
|
|
|
|
f(`
|
2022-12-22 03:55:57 +00:00
|
|
|
- action: keepequal
|
|
|
|
source_labels: [foo]
|
|
|
|
target_label: bar
|
|
|
|
`, `{foo="a",bar="a"}`, true, `{bar="a",foo="a"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// keepequal-miss
|
|
|
|
f(`
|
2022-12-22 03:55:57 +00:00
|
|
|
- action: keepequal
|
|
|
|
source_labels: [foo]
|
|
|
|
target_label: bar
|
|
|
|
`, `{foo="a",bar="x"}`, true, `{}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// dropequal-hit
|
|
|
|
f(`
|
2022-12-22 03:55:57 +00:00
|
|
|
- action: dropequal
|
|
|
|
source_labels: [foo]
|
|
|
|
target_label: bar
|
|
|
|
`, `{foo="a",bar="a"}`, true, `{}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// dropequal-miss
|
|
|
|
f(`
|
2022-12-22 03:55:57 +00:00
|
|
|
- action: dropequal
|
|
|
|
source_labels: [foo]
|
|
|
|
target_label: bar
|
|
|
|
`, `{foo="a",bar="x"}`, true, `{bar="x",foo="a"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// keep-miss
|
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: keep
|
|
|
|
source_labels: [foo]
|
|
|
|
regex: ".+"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{}`, true, `{}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: keep
|
|
|
|
source_labels: [foo]
|
|
|
|
regex: ".+"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{xxx="yyy"}`, true, `{}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// keep-if-miss
|
|
|
|
f(`
|
2022-02-24 00:26:15 +00:00
|
|
|
- action: keep
|
|
|
|
if: '{foo="bar"}'
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{foo="yyy"}`, false, `{}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// keep-if-hit
|
|
|
|
f(`
|
2022-02-24 00:26:15 +00:00
|
|
|
- action: keep
|
2023-07-24 23:10:47 +00:00
|
|
|
if: ['foobar', '{foo="yyy"}', '{a="b"}']
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{foo="yyy"}`, false, `{foo="yyy"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// keep-hit
|
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: keep
|
|
|
|
source_labels: [foo]
|
|
|
|
regex: "yyy"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{foo="yyy"}`, false, `{foo="yyy"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// keep-hit-regexp
|
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: keep
|
|
|
|
source_labels: ["foo"]
|
|
|
|
regex: ".+"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{foo="yyy"}`, false, `{foo="yyy"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// keep_metrics-miss
|
|
|
|
f(`
|
2021-09-09 13:18:19 +00:00
|
|
|
- action: keep_metrics
|
|
|
|
regex:
|
|
|
|
- foo
|
|
|
|
- bar
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `xxx`, true, `{}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// keep_metrics-if-miss
|
|
|
|
f(`
|
2022-02-24 00:26:15 +00:00
|
|
|
- action: keep_metrics
|
|
|
|
if: 'bar'
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `foo`, true, `{}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// keep_metrics-if-hit
|
|
|
|
f(`
|
2022-02-24 00:26:15 +00:00
|
|
|
- action: keep_metrics
|
|
|
|
if: 'foo'
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `foo`, true, `foo`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// keep_metrics-hit
|
|
|
|
f(`
|
2021-09-09 13:18:19 +00:00
|
|
|
- action: keep_metrics
|
|
|
|
regex:
|
|
|
|
- foo
|
|
|
|
- bar
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `foo`, true, `foo`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// drop-miss
|
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: drop
|
|
|
|
source_labels: [foo]
|
|
|
|
regex: ".+"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{}`, false, `{}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: drop
|
|
|
|
source_labels: [foo]
|
|
|
|
regex: ".+"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{xxx="yyy"}`, true, `{xxx="yyy"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// drop-if-miss
|
|
|
|
f(`
|
2022-02-24 00:26:15 +00:00
|
|
|
- action: drop
|
|
|
|
if: '{foo="bar"}'
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{foo="yyy"}`, true, `{foo="yyy"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// drop-if-hit
|
|
|
|
f(`
|
2022-02-24 00:26:15 +00:00
|
|
|
- action: drop
|
|
|
|
if: '{foo="yyy"}'
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{foo="yyy"}`, true, `{}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// drop-hit
|
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: drop
|
|
|
|
source_labels: [foo]
|
|
|
|
regex: yyy
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{foo="yyy"}`, true, `{}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// drop-hit-regexp
|
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: drop
|
|
|
|
source_labels: [foo]
|
|
|
|
regex: ".+"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{foo="yyy"}`, true, `{}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// drop_metrics-miss
|
|
|
|
f(`
|
2021-09-09 13:18:19 +00:00
|
|
|
- action: drop_metrics
|
|
|
|
regex:
|
|
|
|
- foo
|
|
|
|
- bar
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `xxx`, true, `xxx`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// drop_metrics-if-miss
|
|
|
|
f(`
|
2022-02-24 00:26:15 +00:00
|
|
|
- action: drop_metrics
|
|
|
|
if: bar
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `foo`, true, `foo`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// drop_metrics-if-hit
|
|
|
|
f(`
|
2022-02-24 00:26:15 +00:00
|
|
|
- action: drop_metrics
|
|
|
|
if: foo
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `foo`, true, `{}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// drop_metrics-hit
|
|
|
|
f(`
|
2021-09-09 13:18:19 +00:00
|
|
|
- action: drop_metrics
|
|
|
|
regex:
|
|
|
|
- foo
|
|
|
|
- bar
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `foo`, true, `{}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// hashmod-miss
|
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: hashmod
|
|
|
|
source_labels: [foo]
|
|
|
|
target_label: aaa
|
|
|
|
modulus: 123
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{xxx="yyy"}`, false, `{aaa="81",xxx="yyy"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// hashmod-if-miss
|
|
|
|
f(`
|
2022-02-24 00:26:15 +00:00
|
|
|
- action: hashmod
|
|
|
|
if: '{foo="bar"}'
|
|
|
|
source_labels: [foo]
|
|
|
|
target_label: aaa
|
|
|
|
modulus: 123
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{foo="yyy"}`, true, `{foo="yyy"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// hashmod-if-hit
|
|
|
|
f(`
|
2022-02-24 00:26:15 +00:00
|
|
|
- action: hashmod
|
|
|
|
if: '{foo="yyy"}'
|
|
|
|
source_labels: [foo]
|
|
|
|
target_label: aaa
|
|
|
|
modulus: 123
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{foo="yyy"}`, true, `{aaa="73",foo="yyy"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// hashmod-hit
|
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: hashmod
|
|
|
|
source_labels: [foo]
|
|
|
|
target_label: aaa
|
|
|
|
modulus: 123
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{foo="yyy"}`, true, `{aaa="73",foo="yyy"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// labelmap-copy-label-if-miss
|
|
|
|
f(`
|
2022-02-24 00:26:15 +00:00
|
|
|
- action: labelmap
|
|
|
|
if: '{foo="yyy",foobar="aab"}'
|
|
|
|
regex: "foo"
|
|
|
|
replacement: "bar"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{foo="yyy",foobar="aaa"}`, true, `{foo="yyy",foobar="aaa"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// labelmap-copy-label-if-hit
|
|
|
|
f(`
|
2022-02-24 00:26:15 +00:00
|
|
|
- action: labelmap
|
|
|
|
if: '{foo="yyy",foobar="aaa"}'
|
|
|
|
regex: "foo"
|
|
|
|
replacement: "bar"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{foo="yyy",foobar="aaa"}`, true, `{bar="yyy",foo="yyy",foobar="aaa"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// labelmap-copy-label
|
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: labelmap
|
|
|
|
regex: "foo"
|
|
|
|
replacement: "bar"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{foo="yyy",foobar="aaa"}`, true, `{bar="yyy",foo="yyy",foobar="aaa"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// labelmap-remove-prefix-dot-star
|
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: labelmap
|
|
|
|
regex: "foo(.*)"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{xoo="yyy",foobar="aaa"}`, true, `{bar="aaa",foobar="aaa",xoo="yyy"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// labelmap-remove-prefix-dot-plus
|
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: labelmap
|
|
|
|
regex: "foo(.+)"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{foo="yyy",foobar="aaa"}`, true, `{bar="aaa",foo="yyy",foobar="aaa"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// labelmap-regex
|
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: labelmap
|
|
|
|
regex: "foo(.+)"
|
|
|
|
replacement: "$1-x"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{foo="yyy",foobar="aaa"}`, true, `{bar-x="aaa",foo="yyy",foobar="aaa"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// labelmap_all-if-miss
|
|
|
|
f(`
|
2022-02-24 00:26:15 +00:00
|
|
|
- action: labelmap_all
|
|
|
|
if: foobar
|
|
|
|
regex: "\\."
|
|
|
|
replacement: "-"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{foo.bar.baz="yyy",foobar="aaa"}`, true, `{foo.bar.baz="yyy",foobar="aaa"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// labelmap_all-if-hit
|
|
|
|
f(`
|
2022-02-24 00:26:15 +00:00
|
|
|
- action: labelmap_all
|
|
|
|
if: '{foo.bar.baz="yyy"}'
|
|
|
|
regex: "\\."
|
|
|
|
replacement: "-"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{foo.bar.baz="yyy",foobar="aaa"}`, true, `{foo-bar-baz="yyy",foobar="aaa"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// labelmap_all
|
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: labelmap_all
|
|
|
|
regex: "\\."
|
|
|
|
replacement: "-"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{foo.bar.baz="yyy",foobar="aaa"}`, true, `{foo-bar-baz="yyy",foobar="aaa"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// labelmap_all-regexp
|
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: labelmap_all
|
|
|
|
regex: "ba(.)"
|
|
|
|
replacement: "${1}ss"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{foo.bar.baz="yyy",foozar="aaa"}`, true, `{foo.rss.zss="yyy",foozar="aaa"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// labeldrop
|
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: labeldrop
|
|
|
|
regex: dropme
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{aaa="bbb"}`, true, `{aaa="bbb"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// if-miss
|
|
|
|
f(`
|
2022-02-24 00:26:15 +00:00
|
|
|
- action: labeldrop
|
|
|
|
if: foo
|
|
|
|
regex: dropme
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{xxx="yyy",dropme="aaa",foo="bar"}`, false, `{dropme="aaa",foo="bar",xxx="yyy"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// if-hit
|
|
|
|
f(`
|
2022-02-24 00:26:15 +00:00
|
|
|
- action: labeldrop
|
|
|
|
if: '{xxx="yyy"}'
|
|
|
|
regex: dropme
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{xxx="yyy",dropme="aaa",foo="bar"}`, false, `{foo="bar",xxx="yyy"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: labeldrop
|
|
|
|
regex: dropme
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{xxx="yyy",dropme="aaa",foo="bar"}`, false, `{foo="bar",xxx="yyy"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// regex in single quotes
|
|
|
|
f(`
|
2021-07-27 07:50:39 +00:00
|
|
|
- action: labeldrop
|
|
|
|
regex: 'dropme'
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{xxx="yyy",dropme="aaa"}`, false, `{xxx="yyy"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// regex in double quotes
|
|
|
|
f(`
|
2021-07-27 07:50:39 +00:00
|
|
|
- action: labeldrop
|
|
|
|
regex: "dropme"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{xxx="yyy",dropme="aaa"}`, false, `{xxx="yyy"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// labeldrop-prefix
|
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: labeldrop
|
|
|
|
regex: "dropme.*"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{aaa="bbb"}`, true, `{aaa="bbb"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: labeldrop
|
2021-02-24 15:57:50 +00:00
|
|
|
regex: "dropme(.+)"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{xxx="yyy",dropme-please="aaa",foo="bar"}`, false, `{foo="bar",xxx="yyy"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// labeldrop-regexp
|
|
|
|
f(`
|
2021-02-24 15:57:50 +00:00
|
|
|
- action: labeldrop
|
|
|
|
regex: ".*dropme.*"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{aaa="bbb"}`, true, `{aaa="bbb"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
f(`
|
2021-02-24 15:57:50 +00:00
|
|
|
- action: labeldrop
|
|
|
|
regex: ".*dropme.*"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{xxx="yyy",dropme-please="aaa",foo="bar"}`, false, `{foo="bar",xxx="yyy"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// labelkeep
|
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: labelkeep
|
|
|
|
regex: "keepme"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{keepme="aaa"}`, true, `{keepme="aaa"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// if-miss
|
|
|
|
f(`
|
2022-02-24 00:26:15 +00:00
|
|
|
- action: labelkeep
|
|
|
|
if: '{aaaa="awefx"}'
|
|
|
|
regex: keepme
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{keepme="aaa",aaaa="awef",keepme-aaa="234"}`, false, `{aaaa="awef",keepme="aaa",keepme-aaa="234"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// if-hit
|
|
|
|
f(`
|
2022-02-24 00:26:15 +00:00
|
|
|
- action: labelkeep
|
|
|
|
if: '{aaaa="awef"}'
|
|
|
|
regex: keepme
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{keepme="aaa",aaaa="awef",keepme-aaa="234"}`, false, `{keepme="aaa"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: labelkeep
|
|
|
|
regex: keepme
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{keepme="aaa",aaaa="awef",keepme-aaa="234"}`, false, `{keepme="aaa"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// labelkeep-regexp
|
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: labelkeep
|
|
|
|
regex: "keepme.*"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{keepme="aaa"}`, true, `{keepme="aaa"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
f(`
|
2021-02-22 14:33:55 +00:00
|
|
|
- action: labelkeep
|
|
|
|
regex: "keepme.*"
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{keepme="aaa",aaaa="awef",keepme-aaa="234"}`, false, `{keepme="aaa",keepme-aaa="234"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// upper-lower-case
|
|
|
|
f(`
|
2022-06-01 08:02:37 +00:00
|
|
|
- action: uppercase
|
|
|
|
source_labels: ["foo"]
|
|
|
|
target_label: foo
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{foo="bar"}`, true, `{foo="BAR"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
f(`
|
2022-06-01 08:02:37 +00:00
|
|
|
- action: lowercase
|
|
|
|
source_labels: ["foo", "bar"]
|
|
|
|
target_label: baz
|
|
|
|
- action: labeldrop
|
|
|
|
regex: foo|bar
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{foo="BaR",bar="fOO"}`, true, `{baz="bar;foo"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
f(`
|
2022-06-01 08:02:37 +00:00
|
|
|
- action: lowercase
|
|
|
|
source_labels: ["foo"]
|
|
|
|
target_label: baz
|
|
|
|
- action: uppercase
|
|
|
|
source_labels: ["bar"]
|
|
|
|
target_label: baz
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `{qux="quux"}`, true, `{qux="quux"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// graphite-match
|
|
|
|
f(`
|
2022-06-16 17:24:19 +00:00
|
|
|
- action: graphite
|
|
|
|
match: foo.*.baz
|
|
|
|
labels:
|
|
|
|
__name__: aaa
|
|
|
|
job: ${1}-zz
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `foo.bar.baz`, true, `aaa{job="bar-zz"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// graphite-mismatch
|
|
|
|
f(`
|
2022-06-16 17:24:19 +00:00
|
|
|
- action: graphite
|
|
|
|
match: foo.*.baz
|
|
|
|
labels:
|
|
|
|
__name__: aaa
|
|
|
|
job: ${1}-zz
|
2022-08-21 19:46:54 +00:00
|
|
|
`, `foo.bar.bazz`, true, `foo.bar.bazz`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// replacement-with-label-refs
|
|
|
|
// no regex
|
|
|
|
f(`
|
2022-08-21 19:46:54 +00:00
|
|
|
- target_label: abc
|
|
|
|
replacement: "{{__name__}}.{{foo}}"
|
|
|
|
`, `qwe{foo="bar",baz="aaa"}`, true, `qwe{abc="qwe.bar",baz="aaa",foo="bar"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// with regex
|
|
|
|
f(`
|
2022-08-21 19:46:54 +00:00
|
|
|
- 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"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
2022-09-30 05:13:56 +00:00
|
|
|
// Check $ at the end of regex - see https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3131
|
2024-04-02 20:16:24 +00:00
|
|
|
f(`
|
2022-09-30 05:13:56 +00:00
|
|
|
- target_label: xyz
|
|
|
|
regex: "foo\\$$"
|
|
|
|
replacement: bar
|
|
|
|
source_labels: [xyz]
|
|
|
|
`, `metric{xyz="foo$",a="b"}`, true, `metric{a="b",xyz="bar"}`)
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// issue-3251
|
|
|
|
f(`
|
2022-10-29 01:33:34 +00:00
|
|
|
- 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,
|
2024-04-02 20:16:24 +00:00
|
|
|
`{container_label_com_docker_swarm_task_name="myservice:subdomain",instance="subdomain.domain.com"}`)
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFinalizeLabels(t *testing.T) {
|
2022-08-21 19:46:54 +00:00
|
|
|
f := func(metric, resultExpected string) {
|
2020-02-23 11:35:47 +00:00
|
|
|
t.Helper()
|
2022-12-10 10:09:21 +00:00
|
|
|
labels := promutils.MustNewLabelsFromString(metric)
|
2022-11-30 05:22:12 +00:00
|
|
|
resultLabels := FinalizeLabels(nil, labels.GetLabels())
|
2022-12-10 10:09:21 +00:00
|
|
|
result := LabelsToString(resultLabels)
|
2022-08-21 19:46:54 +00:00
|
|
|
if result != resultExpected {
|
|
|
|
t.Fatalf("unexpected result; got\n%s\nwant\n%s", result, resultExpected)
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-21 19:46:54 +00: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 11:35:47 +00:00
|
|
|
}
|
2020-04-14 09:21:10 +00:00
|
|
|
|
2022-08-21 19:46:54 +00:00
|
|
|
func TestFillLabelReferences(t *testing.T) {
|
|
|
|
f := func(replacement, metric, resultExpected string) {
|
|
|
|
t.Helper()
|
2022-12-10 10:09:21 +00:00
|
|
|
labels := promutils.MustNewLabelsFromString(metric)
|
2022-11-30 05:22:12 +00:00
|
|
|
result := fillLabelReferences(nil, replacement, labels.GetLabels())
|
2022-08-21 19:46:54 +00: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 09:21:10 +00:00
|
|
|
}
|
2022-08-24 14:54:26 +00:00
|
|
|
|
2022-08-26 12:23:41 +00:00
|
|
|
func TestRegexMatchStringSuccess(t *testing.T) {
|
2022-08-24 14:54:26 +00:00
|
|
|
f := func(pattern, s string) {
|
|
|
|
t.Helper()
|
2022-08-25 20:22:03 +00:00
|
|
|
prc := newTestRegexRelabelConfig(pattern)
|
2022-08-26 12:23:41 +00:00
|
|
|
if !prc.regex.MatchString(s) {
|
|
|
|
t.Fatalf("unexpected MatchString(%q) result; got false; want true", s)
|
2022-08-24 14:54:26 +00: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 20:22:03 +00:00
|
|
|
prc := newTestRegexRelabelConfig(pattern)
|
2022-08-26 12:23:41 +00:00
|
|
|
if prc.regex.MatchString(s) {
|
|
|
|
t.Fatalf("unexpected MatchString(%q) result; got true; want false", s)
|
2022-08-24 14:54:26 +00: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 20:22:03 +00:00
|
|
|
|
|
|
|
func newTestRegexRelabelConfig(pattern string) *parsedRelabelConfig {
|
|
|
|
rc := &RelabelConfig{
|
|
|
|
Action: "labeldrop",
|
|
|
|
Regex: &MultiLineRegex{
|
|
|
|
S: pattern,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
prc, err := parseRelabelConfig(rc)
|
|
|
|
if err != nil {
|
2023-10-25 19:24:01 +00:00
|
|
|
panic(fmt.Errorf("unexpected error in parseRelabelConfig: %w", err))
|
2022-08-25 20:22:03 +00:00
|
|
|
}
|
|
|
|
return prc
|
|
|
|
}
|
2023-08-11 13:37:48 +00:00
|
|
|
|
|
|
|
func TestParsedRelabelConfigsApplyForMultipleSeries(t *testing.T) {
|
|
|
|
f := func(config string, metrics []string, resultExpected []string) {
|
|
|
|
t.Helper()
|
|
|
|
pcs, err := ParseRelabelConfigsData([]byte(config))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("cannot parse %q: %s", config, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
totalLabels := 0
|
|
|
|
var labels []prompbmarshal.Label
|
|
|
|
for _, metric := range metrics {
|
|
|
|
labels = append(labels, promutils.MustNewLabelsFromString(metric).GetLabels()...)
|
|
|
|
resultLabels := pcs.Apply(labels, totalLabels)
|
|
|
|
SortLabels(resultLabels)
|
|
|
|
totalLabels += len(resultLabels)
|
|
|
|
labels = resultLabels
|
|
|
|
}
|
|
|
|
|
|
|
|
var result []string
|
|
|
|
for i := range labels {
|
|
|
|
result = append(result, LabelsToString(labels[i:i+1]))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(result) != len(resultExpected) {
|
|
|
|
t.Fatalf("unexpected number of results; got\n%q\nwant\n%q", result, resultExpected)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range result {
|
|
|
|
if result[i] != resultExpected[i] {
|
|
|
|
t.Fatalf("unexpected result[%d]; got\n%q\nwant\n%q", i, result[i], resultExpected[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-02 20:16:24 +00:00
|
|
|
// drops one of series
|
|
|
|
f(`
|
2023-08-11 13:37:48 +00:00
|
|
|
- action: drop
|
|
|
|
if: '{__name__!~"smth"}'
|
|
|
|
`, []string{`smth`, `notthis`}, []string{`smth`})
|
2024-04-02 20:16:24 +00:00
|
|
|
f(`
|
2023-08-11 13:37:48 +00:00
|
|
|
- action: drop
|
|
|
|
if: '{__name__!~"smth"}'
|
|
|
|
`, []string{`notthis`, `smth`}, []string{`smth`})
|
|
|
|
}
|