2015-04-28 15:08:58 -07:00
|
|
|
package retrieval
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
2015-05-07 01:55:03 -07:00
|
|
|
"regexp"
|
2015-04-28 15:08:58 -07:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRelabel(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
input clientmodel.LabelSet
|
2015-06-04 08:03:12 -07:00
|
|
|
relabel []*config.RelabelConfig
|
2015-04-28 15:08:58 -07:00
|
|
|
output clientmodel.LabelSet
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
input: clientmodel.LabelSet{
|
|
|
|
"a": "foo",
|
|
|
|
"b": "bar",
|
|
|
|
"c": "baz",
|
|
|
|
},
|
2015-06-04 08:03:12 -07:00
|
|
|
relabel: []*config.RelabelConfig{
|
2015-04-28 15:08:58 -07:00
|
|
|
{
|
2015-05-07 01:55:03 -07:00
|
|
|
SourceLabels: clientmodel.LabelNames{"a"},
|
|
|
|
Regex: &config.Regexp{*regexp.MustCompile("f(.*)")},
|
|
|
|
TargetLabel: clientmodel.LabelName("d"),
|
|
|
|
Separator: ";",
|
|
|
|
Replacement: "ch${1}-ch${1}",
|
|
|
|
Action: config.RelabelReplace,
|
2015-04-28 15:08:58 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
output: clientmodel.LabelSet{
|
|
|
|
"a": "foo",
|
|
|
|
"b": "bar",
|
|
|
|
"c": "baz",
|
|
|
|
"d": "choo-choo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: clientmodel.LabelSet{
|
|
|
|
"a": "foo",
|
|
|
|
"b": "bar",
|
|
|
|
"c": "baz",
|
|
|
|
},
|
2015-06-04 08:03:12 -07:00
|
|
|
relabel: []*config.RelabelConfig{
|
2015-04-28 15:08:58 -07:00
|
|
|
{
|
2015-05-07 01:55:03 -07:00
|
|
|
SourceLabels: clientmodel.LabelNames{"a", "b"},
|
|
|
|
Regex: &config.Regexp{*regexp.MustCompile("^f(.*);(.*)r$")},
|
|
|
|
TargetLabel: clientmodel.LabelName("a"),
|
|
|
|
Separator: ";",
|
|
|
|
Replacement: "b${1}${2}m", // boobam
|
|
|
|
Action: config.RelabelReplace,
|
2015-04-28 15:08:58 -07:00
|
|
|
},
|
|
|
|
{
|
2015-05-07 01:55:03 -07:00
|
|
|
SourceLabels: clientmodel.LabelNames{"c", "a"},
|
|
|
|
Regex: &config.Regexp{*regexp.MustCompile("(b).*b(.*)ba(.*)")},
|
|
|
|
TargetLabel: clientmodel.LabelName("d"),
|
|
|
|
Separator: ";",
|
|
|
|
Replacement: "$1$2$2$3",
|
|
|
|
Action: config.RelabelReplace,
|
2015-04-28 15:08:58 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
output: clientmodel.LabelSet{
|
|
|
|
"a": "boobam",
|
|
|
|
"b": "bar",
|
|
|
|
"c": "baz",
|
|
|
|
"d": "boooom",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: clientmodel.LabelSet{
|
|
|
|
"a": "foo",
|
|
|
|
},
|
2015-06-04 08:03:12 -07:00
|
|
|
relabel: []*config.RelabelConfig{
|
2015-04-28 15:08:58 -07:00
|
|
|
{
|
2015-05-07 01:55:03 -07:00
|
|
|
SourceLabels: clientmodel.LabelNames{"a"},
|
|
|
|
Regex: &config.Regexp{*regexp.MustCompile("o$")},
|
|
|
|
Action: config.RelabelDrop,
|
2015-04-28 15:08:58 -07:00
|
|
|
}, {
|
2015-05-07 01:55:03 -07:00
|
|
|
SourceLabels: clientmodel.LabelNames{"a"},
|
|
|
|
Regex: &config.Regexp{*regexp.MustCompile("f(.*)")},
|
|
|
|
TargetLabel: clientmodel.LabelName("d"),
|
|
|
|
Separator: ";",
|
|
|
|
Replacement: "ch$1-ch$1",
|
|
|
|
Action: config.RelabelReplace,
|
2015-04-28 15:08:58 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
output: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: clientmodel.LabelSet{
|
|
|
|
"a": "foo",
|
|
|
|
},
|
2015-06-04 08:03:12 -07:00
|
|
|
relabel: []*config.RelabelConfig{
|
2015-04-28 15:08:58 -07:00
|
|
|
{
|
2015-05-07 01:55:03 -07:00
|
|
|
SourceLabels: clientmodel.LabelNames{"a"},
|
|
|
|
Regex: &config.Regexp{*regexp.MustCompile("no-match")},
|
|
|
|
Action: config.RelabelDrop,
|
2015-04-28 15:08:58 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
output: clientmodel.LabelSet{
|
|
|
|
"a": "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: clientmodel.LabelSet{
|
|
|
|
"a": "foo",
|
|
|
|
},
|
2015-06-04 08:03:12 -07:00
|
|
|
relabel: []*config.RelabelConfig{
|
2015-04-28 15:08:58 -07:00
|
|
|
{
|
2015-05-07 01:55:03 -07:00
|
|
|
SourceLabels: clientmodel.LabelNames{"a"},
|
|
|
|
Regex: &config.Regexp{*regexp.MustCompile("no-match")},
|
|
|
|
Action: config.RelabelKeep,
|
2015-04-28 15:08:58 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
output: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: clientmodel.LabelSet{
|
|
|
|
"a": "foo",
|
|
|
|
},
|
2015-06-04 08:03:12 -07:00
|
|
|
relabel: []*config.RelabelConfig{
|
2015-04-28 15:08:58 -07:00
|
|
|
{
|
2015-05-07 01:55:03 -07:00
|
|
|
SourceLabels: clientmodel.LabelNames{"a"},
|
|
|
|
Regex: &config.Regexp{*regexp.MustCompile("^f")},
|
|
|
|
Action: config.RelabelKeep,
|
2015-04-28 15:08:58 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
output: clientmodel.LabelSet{
|
|
|
|
"a": "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// No replacement must be applied if there is no match.
|
|
|
|
input: clientmodel.LabelSet{
|
|
|
|
"a": "boo",
|
|
|
|
},
|
2015-06-04 08:03:12 -07:00
|
|
|
relabel: []*config.RelabelConfig{
|
2015-04-28 15:08:58 -07:00
|
|
|
{
|
2015-05-07 01:55:03 -07:00
|
|
|
SourceLabels: clientmodel.LabelNames{"a"},
|
|
|
|
Regex: &config.Regexp{*regexp.MustCompile("^f")},
|
|
|
|
TargetLabel: clientmodel.LabelName("b"),
|
|
|
|
Replacement: "bar",
|
|
|
|
Action: config.RelabelReplace,
|
2015-04-28 15:08:58 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
output: clientmodel.LabelSet{
|
|
|
|
"a": "boo",
|
|
|
|
},
|
|
|
|
},
|
2015-06-24 00:07:17 -07:00
|
|
|
{
|
|
|
|
input: clientmodel.LabelSet{
|
|
|
|
"a": "foo",
|
|
|
|
"b": "bar",
|
|
|
|
"c": "baz",
|
|
|
|
},
|
|
|
|
relabel: []*config.RelabelConfig{
|
|
|
|
{
|
|
|
|
SourceLabels: clientmodel.LabelNames{"c"},
|
|
|
|
TargetLabel: clientmodel.LabelName("d"),
|
|
|
|
Separator: ";",
|
|
|
|
Action: config.RelabelHashMod,
|
|
|
|
Modulus: 1000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
output: clientmodel.LabelSet{
|
|
|
|
"a": "foo",
|
|
|
|
"b": "bar",
|
|
|
|
"c": "baz",
|
2015-08-12 07:44:42 -07:00
|
|
|
"d": "976",
|
2015-06-24 00:07:17 -07:00
|
|
|
},
|
|
|
|
},
|
2015-08-12 02:21:20 -07:00
|
|
|
{
|
|
|
|
input: clientmodel.LabelSet{
|
|
|
|
"a": "foo",
|
|
|
|
"b1": "bar",
|
|
|
|
"b2": "baz",
|
|
|
|
},
|
|
|
|
relabel: []*config.RelabelConfig{
|
|
|
|
{
|
|
|
|
Regex: &config.Regexp{*regexp.MustCompile("^(b.*)")},
|
|
|
|
Replacement: "bar_${1}",
|
|
|
|
Action: config.RelabelLabelMap,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
output: clientmodel.LabelSet{
|
|
|
|
"a": "foo",
|
|
|
|
"b1": "bar",
|
|
|
|
"b2": "baz",
|
|
|
|
"bar_b1": "bar",
|
|
|
|
"bar_b2": "baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: clientmodel.LabelSet{
|
|
|
|
"a": "foo",
|
|
|
|
"__meta_my_bar": "aaa",
|
|
|
|
"__meta_my_baz": "bbb",
|
|
|
|
"__meta_other": "ccc",
|
|
|
|
},
|
|
|
|
relabel: []*config.RelabelConfig{
|
|
|
|
{
|
|
|
|
Regex: &config.Regexp{*regexp.MustCompile("^__meta_(my.*)")},
|
|
|
|
Replacement: "${1}",
|
|
|
|
Action: config.RelabelLabelMap,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
output: clientmodel.LabelSet{
|
|
|
|
"a": "foo",
|
|
|
|
"__meta_my_bar": "aaa",
|
|
|
|
"__meta_my_baz": "bbb",
|
|
|
|
"__meta_other": "ccc",
|
|
|
|
"my_bar": "aaa",
|
|
|
|
"my_baz": "bbb",
|
|
|
|
},
|
|
|
|
},
|
2015-04-28 15:08:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2015-06-04 08:03:12 -07:00
|
|
|
res, err := Relabel(test.input, test.relabel...)
|
2015-04-28 15:08:58 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test %d: error relabeling: %s", i+1, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(res, test.output) {
|
|
|
|
t.Errorf("Test %d: relabel output mismatch: expected %#v, got %#v", i+1, test.output, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|