2020-02-23 11:35:47 +00:00
|
|
|
package promrelabel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2021-09-09 13:18:19 +00:00
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
2020-02-23 11:35:47 +00:00
|
|
|
)
|
|
|
|
|
2022-08-08 00:15:23 +00:00
|
|
|
func TestMultiLineRegexUnmarshalMarshal(t *testing.T) {
|
|
|
|
f := func(data, resultExpected string) {
|
|
|
|
t.Helper()
|
|
|
|
var mlr MultiLineRegex
|
|
|
|
if err := yaml.UnmarshalStrict([]byte(data), &mlr); err != nil {
|
|
|
|
t.Fatalf("cannot unmarshal %q: %s", data, err)
|
|
|
|
}
|
|
|
|
result, err := yaml.Marshal(&mlr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("cannot marshal %q: %s", data, err)
|
|
|
|
}
|
|
|
|
if string(result) != resultExpected {
|
|
|
|
t.Fatalf("unexpected marshaled data; got\n%q\nwant\n%q", result, resultExpected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f(``, `""`+"\n")
|
|
|
|
f(`foo`, "foo\n")
|
|
|
|
f(`a|b||c`, "- a\n- b\n- \"\"\n- c\n")
|
|
|
|
f(`(a|b)`, "(a|b)\n")
|
|
|
|
f(`a|b[c|d]`, "a|b[c|d]\n")
|
|
|
|
f("- a\n- b", "- a\n- b\n")
|
|
|
|
f("- a\n- (b)", "a|(b)\n")
|
|
|
|
}
|
|
|
|
|
2021-09-09 13:18:19 +00:00
|
|
|
func TestRelabelConfigMarshalUnmarshal(t *testing.T) {
|
|
|
|
f := func(data, resultExpected string) {
|
|
|
|
t.Helper()
|
|
|
|
var rcs []RelabelConfig
|
|
|
|
if err := yaml.UnmarshalStrict([]byte(data), &rcs); err != nil {
|
|
|
|
t.Fatalf("cannot unmarshal %q: %s", data, err)
|
|
|
|
}
|
|
|
|
result, err := yaml.Marshal(&rcs)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("cannot marshal %q: %s", data, err)
|
|
|
|
}
|
|
|
|
if string(result) != resultExpected {
|
|
|
|
t.Fatalf("unexpected marshaled data; got\n%q\nwant\n%q", result, resultExpected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f(``, "[]\n")
|
|
|
|
f(`
|
|
|
|
- action: keep
|
|
|
|
regex: foobar
|
2022-12-10 10:09:21 +00:00
|
|
|
`, "- action: keep\n regex: foobar\n")
|
2021-09-09 13:18:19 +00:00
|
|
|
f(`
|
|
|
|
- regex:
|
|
|
|
- 'fo.+'
|
|
|
|
- '.*ba[r-z]a'
|
2022-08-08 10:47:29 +00:00
|
|
|
`, "- regex: fo.+|.*ba[r-z]a\n")
|
2021-09-09 13:18:19 +00:00
|
|
|
f(`- regex: foo|bar`, "- regex:\n - foo\n - bar\n")
|
2021-09-21 20:00:15 +00:00
|
|
|
f(`- regex: True`, `- regex: "true"`+"\n")
|
|
|
|
f(`- regex: true`, `- regex: "true"`+"\n")
|
|
|
|
f(`- regex: 123`, `- regex: "123"`+"\n")
|
|
|
|
f(`- regex: 1.23`, `- regex: "1.23"`+"\n")
|
|
|
|
f(`- regex: [null]`, `- regex: "null"`+"\n")
|
|
|
|
f(`
|
|
|
|
- regex:
|
|
|
|
- -1.23
|
|
|
|
- False
|
|
|
|
- null
|
|
|
|
- nan
|
|
|
|
`, "- regex:\n - \"-1.23\"\n - \"false\"\n - \"null\"\n - nan\n")
|
2022-06-16 17:24:19 +00:00
|
|
|
f(`
|
|
|
|
- action: graphite
|
|
|
|
match: 'foo.*.*.aaa'
|
|
|
|
labels:
|
|
|
|
instance: '$1-abc'
|
|
|
|
job: '${2}'
|
|
|
|
`, "- action: graphite\n match: foo.*.*.aaa\n labels:\n instance: $1-abc\n job: ${2}\n")
|
2021-09-09 13:18:19 +00:00
|
|
|
}
|
|
|
|
|
2020-02-23 11:35:47 +00:00
|
|
|
func TestLoadRelabelConfigsSuccess(t *testing.T) {
|
|
|
|
path := "testdata/relabel_configs_valid.yml"
|
2022-12-10 10:09:21 +00:00
|
|
|
pcs, err := LoadRelabelConfigs(path)
|
2020-02-23 11:35:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("cannot load relabel configs from %q: %s", path, err)
|
|
|
|
}
|
2022-12-22 03:55:57 +00:00
|
|
|
nExpected := 18
|
2022-06-16 17:24:19 +00:00
|
|
|
if n := pcs.Len(); n != nExpected {
|
|
|
|
t.Fatalf("unexpected number of relabel configs loaded from %q; got %d; want %d", path, n, nExpected)
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadRelabelConfigsFailure(t *testing.T) {
|
|
|
|
f := func(path string) {
|
|
|
|
t.Helper()
|
2022-12-10 10:09:21 +00:00
|
|
|
rcs, err := LoadRelabelConfigs(path)
|
2020-02-23 11:35:47 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expecting non-nil error")
|
|
|
|
}
|
2021-02-22 14:33:55 +00:00
|
|
|
if rcs.Len() != 0 {
|
2020-02-23 11:35:47 +00:00
|
|
|
t.Fatalf("unexpected non-empty rcs: %#v", rcs)
|
|
|
|
}
|
|
|
|
}
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// non-existing-file
|
|
|
|
f("testdata/non-exsiting-file")
|
|
|
|
|
|
|
|
// invalid-file
|
|
|
|
f("testdata/invalid_config.yml")
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 17:24:19 +00:00
|
|
|
func TestParsedConfigsString(t *testing.T) {
|
|
|
|
f := func(rcs []RelabelConfig, sExpected string) {
|
|
|
|
t.Helper()
|
2022-12-10 10:09:21 +00:00
|
|
|
pcs, err := ParseRelabelConfigs(rcs)
|
2022-06-16 17:24:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
s := pcs.String()
|
|
|
|
if s != sExpected {
|
|
|
|
t.Fatalf("unexpected string representation for ParsedConfigs;\ngot\n%s\nwant\n%s", s, sExpected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
TargetLabel: "foo",
|
|
|
|
SourceLabels: []string{"aaa"},
|
|
|
|
},
|
2022-12-10 10:09:21 +00:00
|
|
|
}, "- source_labels: [aaa]\n target_label: foo\n")
|
2022-06-16 17:24:19 +00:00
|
|
|
var ie IfExpression
|
|
|
|
if err := ie.Parse("{foo=~'bar'}"); err != nil {
|
|
|
|
t.Fatalf("unexpected error when parsing if expression: %s", err)
|
|
|
|
}
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "graphite",
|
|
|
|
Match: "foo.*.bar",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"job": "$1-zz",
|
|
|
|
},
|
|
|
|
If: &ie,
|
|
|
|
},
|
2022-12-10 10:09:21 +00:00
|
|
|
}, "- if: '{foo=~''bar''}'\n action: graphite\n match: foo.*.bar\n labels:\n job: $1-zz\n")
|
|
|
|
replacement := "foo"
|
2022-06-16 17:24:19 +00:00
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "replace",
|
|
|
|
SourceLabels: []string{"foo", "bar"},
|
|
|
|
TargetLabel: "x",
|
|
|
|
If: &ie,
|
|
|
|
},
|
2022-12-10 10:09:21 +00:00
|
|
|
{
|
|
|
|
TargetLabel: "x",
|
|
|
|
Replacement: &replacement,
|
|
|
|
},
|
|
|
|
}, "- if: '{foo=~''bar''}'\n action: replace\n source_labels: [foo, bar]\n target_label: x\n- target_label: x\n replacement: foo\n")
|
2022-06-16 17:24:19 +00:00
|
|
|
}
|
|
|
|
|
2020-02-23 11:35:47 +00:00
|
|
|
func TestParseRelabelConfigsSuccess(t *testing.T) {
|
2021-02-22 14:33:55 +00:00
|
|
|
f := func(rcs []RelabelConfig, pcsExpected *ParsedConfigs) {
|
2020-02-23 11:35:47 +00:00
|
|
|
t.Helper()
|
2022-12-10 10:09:21 +00:00
|
|
|
pcs, err := ParseRelabelConfigs(rcs)
|
2020-02-23 11:35:47 +00:00
|
|
|
if err != nil {
|
2021-12-20 15:39:43 +00:00
|
|
|
t.Fatalf("unexpected error: %s", err)
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
2022-09-30 07:43:31 +00:00
|
|
|
if pcs != nil {
|
|
|
|
for _, prc := range pcs.prcs {
|
2022-12-10 10:09:21 +00:00
|
|
|
prc.ruleOriginal = ""
|
2022-09-30 07:43:31 +00:00
|
|
|
prc.stringReplacer = nil
|
2022-09-30 09:25:02 +00:00
|
|
|
prc.submatchReplacer = nil
|
2022-09-30 07:43:31 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-22 14:33:55 +00:00
|
|
|
if !reflect.DeepEqual(pcs, pcsExpected) {
|
|
|
|
t.Fatalf("unexpected pcs; got\n%#v\nwant\n%#v", pcs, pcsExpected)
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
f(nil, nil)
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
SourceLabels: []string{"foo", "bar"},
|
|
|
|
TargetLabel: "xxx",
|
|
|
|
},
|
2021-02-22 14:33:55 +00:00
|
|
|
}, &ParsedConfigs{
|
|
|
|
prcs: []*parsedRelabelConfig{
|
|
|
|
{
|
2022-08-26 12:23:41 +00:00
|
|
|
SourceLabels: []string{"foo", "bar"},
|
|
|
|
Separator: ";",
|
|
|
|
TargetLabel: "xxx",
|
|
|
|
RegexAnchored: defaultRegexForRelabelConfig,
|
|
|
|
Replacement: "$1",
|
|
|
|
Action: "replace",
|
2020-06-18 23:20:29 +00:00
|
|
|
|
2022-08-26 12:23:41 +00:00
|
|
|
regex: defaultPromRegex,
|
2021-02-22 14:33:55 +00:00
|
|
|
regexOriginal: defaultOriginalRegexForRelabelConfig,
|
|
|
|
hasCaptureGroupInReplacement: true,
|
|
|
|
},
|
2020-02-23 11:35:47 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseRelabelConfigsFailure(t *testing.T) {
|
|
|
|
f := func(rcs []RelabelConfig) {
|
|
|
|
t.Helper()
|
2022-12-10 10:09:21 +00:00
|
|
|
pcs, err := ParseRelabelConfigs(rcs)
|
2020-02-23 11:35:47 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expecting non-nil error")
|
|
|
|
}
|
2021-02-22 14:33:55 +00:00
|
|
|
if pcs.Len() > 0 {
|
|
|
|
t.Fatalf("unexpected non-empty pcs: %#v", pcs)
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// invalid regex
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
SourceLabels: []string{"aaa"},
|
|
|
|
TargetLabel: "xxx",
|
|
|
|
Regex: &MultiLineRegex{
|
|
|
|
S: "foo[bar",
|
2020-02-23 11:35:47 +00:00
|
|
|
},
|
2024-04-02 20:16:24 +00:00
|
|
|
},
|
2020-02-23 11:35:47 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// replace-missing-target-label
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "replace",
|
|
|
|
SourceLabels: []string{"foo"},
|
|
|
|
},
|
2020-02-23 11:35:47 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// replace_all-missing-source-labels
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "replace_all",
|
|
|
|
TargetLabel: "xxx",
|
|
|
|
},
|
2020-02-23 11:35:47 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// replace_all-missing-target-label
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "replace_all",
|
|
|
|
SourceLabels: []string{"foo"},
|
|
|
|
},
|
2020-02-23 11:35:47 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// keep-missing-source-labels
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "keep",
|
|
|
|
},
|
2020-02-23 11:35:47 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// keep_if_contains-missing-target-label
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "keep_if_contains",
|
|
|
|
SourceLabels: []string{"foo"},
|
|
|
|
},
|
2023-11-29 09:00:48 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// keep_if_contains-missing-source-labels
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "keep_if_contains",
|
|
|
|
TargetLabel: "foo",
|
|
|
|
},
|
2023-11-29 09:00:48 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// keep_if_contains-unused-regex
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "keep_if_contains",
|
|
|
|
TargetLabel: "foo",
|
|
|
|
SourceLabels: []string{"bar"},
|
|
|
|
Regex: &MultiLineRegex{
|
|
|
|
S: "bar",
|
2023-11-29 09:00:48 +00:00
|
|
|
},
|
2024-04-02 20:16:24 +00:00
|
|
|
},
|
2023-11-29 09:00:48 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// drop_if_contains-missing-target-label
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "drop_if_contains",
|
|
|
|
SourceLabels: []string{"foo"},
|
|
|
|
},
|
2023-11-29 09:00:48 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// drop_if_contains-missing-source-labels
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "drop_if_contains",
|
|
|
|
TargetLabel: "foo",
|
|
|
|
},
|
2023-11-29 09:00:48 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// drop_if_contains-unused-regex
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "drop_if_contains",
|
|
|
|
TargetLabel: "foo",
|
|
|
|
SourceLabels: []string{"bar"},
|
|
|
|
Regex: &MultiLineRegex{
|
|
|
|
S: "bar",
|
2023-11-29 09:00:48 +00:00
|
|
|
},
|
2024-04-02 20:16:24 +00:00
|
|
|
},
|
2023-11-29 09:00:48 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// keep_if_equal-missing-source-labels
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "keep_if_equal",
|
|
|
|
},
|
2020-06-23 14:17:58 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// keep_if_equal-single-source-label
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "keep_if_equal",
|
|
|
|
SourceLabels: []string{"foo"},
|
|
|
|
},
|
2020-06-23 14:17:58 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// keep_if_equal-unused-target-label
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "keep_if_equal",
|
|
|
|
SourceLabels: []string{"foo", "bar"},
|
|
|
|
TargetLabel: "foo",
|
|
|
|
},
|
2022-12-22 03:55:57 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// keep_if_equal-unused-regex
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "keep_if_equal",
|
|
|
|
SourceLabels: []string{"foo", "bar"},
|
|
|
|
Regex: &MultiLineRegex{
|
|
|
|
S: "bar",
|
2022-12-22 03:55:57 +00:00
|
|
|
},
|
2024-04-02 20:16:24 +00:00
|
|
|
},
|
2022-12-22 03:55:57 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// drop_if_equal-missing-source-labels
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "drop_if_equal",
|
|
|
|
},
|
2020-06-23 14:17:58 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// drop_if_equal-single-source-label
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "drop_if_equal",
|
|
|
|
SourceLabels: []string{"foo"},
|
|
|
|
},
|
2020-06-23 14:17:58 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// drop_if_equal-unused-target-label
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "drop_if_equal",
|
|
|
|
SourceLabels: []string{"foo", "bar"},
|
|
|
|
TargetLabel: "foo",
|
|
|
|
},
|
2022-12-22 03:55:57 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// drop_if_equal-unused-regex
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "drop_if_equal",
|
|
|
|
SourceLabels: []string{"foo", "bar"},
|
|
|
|
Regex: &MultiLineRegex{
|
|
|
|
S: "bar",
|
2022-12-22 03:55:57 +00:00
|
|
|
},
|
2024-04-02 20:16:24 +00:00
|
|
|
},
|
2022-12-22 03:55:57 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// keepequal-missing-source-labels
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "keepequal",
|
|
|
|
},
|
2022-12-22 03:55:57 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// keepequal-missing-target-label
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "keepequal",
|
|
|
|
SourceLabels: []string{"foo"},
|
|
|
|
},
|
2022-12-22 03:55:57 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// keepequal-unused-regex
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "keepequal",
|
|
|
|
SourceLabels: []string{"foo"},
|
|
|
|
TargetLabel: "foo",
|
|
|
|
Regex: &MultiLineRegex{
|
|
|
|
S: "bar",
|
2022-12-22 03:55:57 +00:00
|
|
|
},
|
2024-04-02 20:16:24 +00:00
|
|
|
},
|
2022-12-22 03:55:57 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// dropequal-missing-source-labels
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "dropequal",
|
|
|
|
},
|
2022-12-22 03:55:57 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// dropequal-missing-target-label
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "dropequal",
|
|
|
|
SourceLabels: []string{"foo"},
|
|
|
|
},
|
2022-12-22 03:55:57 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// dropequal-unused-regex
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "dropequal",
|
|
|
|
SourceLabels: []string{"foo"},
|
|
|
|
TargetLabel: "foo",
|
|
|
|
Regex: &MultiLineRegex{
|
|
|
|
S: "bar",
|
2022-12-22 03:55:57 +00:00
|
|
|
},
|
2024-04-02 20:16:24 +00:00
|
|
|
},
|
2022-12-22 03:55:57 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// drop-missing-source-labels
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "drop",
|
|
|
|
},
|
2020-02-23 11:35:47 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// hashmod-missing-source-labels
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "hashmod",
|
|
|
|
TargetLabel: "aaa",
|
|
|
|
Modulus: 123,
|
|
|
|
},
|
2020-02-23 11:35:47 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// hashmod-missing-target-label
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "hashmod",
|
|
|
|
SourceLabels: []string{"aaa"},
|
|
|
|
Modulus: 123,
|
|
|
|
},
|
2020-02-23 11:35:47 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// hashmod-missing-modulus
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "hashmod",
|
|
|
|
SourceLabels: []string{"aaa"},
|
|
|
|
TargetLabel: "xxx",
|
|
|
|
},
|
2020-02-23 11:35:47 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// invalid-action
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "invalid-action",
|
|
|
|
},
|
2020-02-23 11:35:47 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// drop_metrics-missing-regex
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "drop_metrics",
|
|
|
|
},
|
2021-09-09 13:18:19 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// drop_metrics-non-empty-source-labels
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "drop_metrics",
|
|
|
|
SourceLabels: []string{"foo"},
|
|
|
|
Regex: &MultiLineRegex{
|
|
|
|
S: "bar",
|
2021-09-09 13:18:19 +00:00
|
|
|
},
|
2024-04-02 20:16:24 +00:00
|
|
|
},
|
2021-09-09 13:18:19 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// keep_metrics-missing-regex
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "keep_metrics",
|
|
|
|
},
|
2021-09-09 13:18:19 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// keep_metrics-non-empty-source-labels
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "keep_metrics",
|
|
|
|
SourceLabels: []string{"foo"},
|
|
|
|
Regex: &MultiLineRegex{
|
|
|
|
S: "bar",
|
2021-09-09 13:18:19 +00:00
|
|
|
},
|
2024-04-02 20:16:24 +00:00
|
|
|
},
|
2021-09-09 13:18:19 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// uppercase-missing-sourceLabels
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "uppercase",
|
|
|
|
TargetLabel: "foobar",
|
|
|
|
},
|
2022-06-16 17:24:19 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// lowercase-missing-targetLabel
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "lowercase",
|
|
|
|
SourceLabels: []string{"foobar"},
|
|
|
|
},
|
2022-06-16 17:24:19 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// graphite-missing-match
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "graphite",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
2022-06-16 17:24:19 +00:00
|
|
|
},
|
2024-04-02 20:16:24 +00:00
|
|
|
},
|
2022-06-16 17:24:19 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// graphite-missing-labels
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "graphite",
|
|
|
|
Match: "foo.*.bar",
|
|
|
|
},
|
2022-06-16 17:24:19 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// graphite-superflouous-sourceLabels
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "graphite",
|
|
|
|
Match: "foo.*.bar",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
2022-06-16 17:24:19 +00:00
|
|
|
},
|
2024-04-02 20:16:24 +00:00
|
|
|
SourceLabels: []string{"foo"},
|
|
|
|
},
|
2022-06-16 17:24:19 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// graphite-superflouous-targetLabel
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "graphite",
|
|
|
|
Match: "foo.*.bar",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
2022-06-16 17:24:19 +00:00
|
|
|
},
|
2024-04-02 20:16:24 +00:00
|
|
|
TargetLabel: "foo",
|
|
|
|
},
|
2022-06-16 17:24:19 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// graphite-superflouous-replacement
|
2022-06-16 17:24:19 +00:00
|
|
|
replacement := "foo"
|
2024-04-02 20:16:24 +00:00
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "graphite",
|
|
|
|
Match: "foo.*.bar",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
2022-06-16 17:24:19 +00:00
|
|
|
},
|
2024-04-02 20:16:24 +00:00
|
|
|
Replacement: &replacement,
|
|
|
|
},
|
2022-06-16 17:24:19 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// graphite-superflouous-regex
|
2022-06-16 17:24:19 +00:00
|
|
|
var re MultiLineRegex
|
2024-04-02 20:16:24 +00:00
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "graphite",
|
|
|
|
Match: "foo.*.bar",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
2022-06-16 17:24:19 +00:00
|
|
|
},
|
2024-04-02 20:16:24 +00:00
|
|
|
Regex: &re,
|
|
|
|
},
|
2022-06-16 17:24:19 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// non-graphite-superflouos-match
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "uppercase",
|
|
|
|
SourceLabels: []string{"foo"},
|
|
|
|
TargetLabel: "foo",
|
|
|
|
Match: "aaa",
|
|
|
|
},
|
2022-06-16 17:24:19 +00:00
|
|
|
})
|
2024-04-02 20:16:24 +00:00
|
|
|
|
|
|
|
// non-graphite-superflouos-labels
|
|
|
|
f([]RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: "uppercase",
|
|
|
|
SourceLabels: []string{"foo"},
|
|
|
|
TargetLabel: "foo",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "Bar",
|
2022-06-16 17:24:19 +00:00
|
|
|
},
|
2024-04-02 20:16:24 +00:00
|
|
|
},
|
2022-06-16 17:24:19 +00:00
|
|
|
})
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
2022-08-26 08:57:12 +00:00
|
|
|
|
|
|
|
func TestIsDefaultRegex(t *testing.T) {
|
|
|
|
f := func(s string, resultExpected bool) {
|
|
|
|
t.Helper()
|
|
|
|
result := isDefaultRegex(s)
|
|
|
|
if result != resultExpected {
|
|
|
|
t.Fatalf("unexpected result for isDefaultRegex(%q); got %v; want %v", s, result, resultExpected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f("", false)
|
|
|
|
f("foo", false)
|
|
|
|
f(".+", false)
|
|
|
|
f("a.*", false)
|
|
|
|
f(".*", true)
|
|
|
|
f("(.*)", true)
|
|
|
|
f("^.*$", true)
|
|
|
|
f("(?:.*)", true)
|
|
|
|
}
|