2015-04-28 22:08:58 +00:00
|
|
|
package retrieval
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
2015-05-07 08:55:03 +00:00
|
|
|
"regexp"
|
2015-04-28 22:08:58 +00: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 15:03:12 +00:00
|
|
|
relabel []*config.RelabelConfig
|
2015-04-28 22:08:58 +00:00
|
|
|
output clientmodel.LabelSet
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
input: clientmodel.LabelSet{
|
|
|
|
"a": "foo",
|
|
|
|
"b": "bar",
|
|
|
|
"c": "baz",
|
|
|
|
},
|
2015-06-04 15:03:12 +00:00
|
|
|
relabel: []*config.RelabelConfig{
|
2015-04-28 22:08:58 +00:00
|
|
|
{
|
2015-05-07 08:55:03 +00: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 22:08:58 +00: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 15:03:12 +00:00
|
|
|
relabel: []*config.RelabelConfig{
|
2015-04-28 22:08:58 +00:00
|
|
|
{
|
2015-05-07 08:55:03 +00: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 22:08:58 +00:00
|
|
|
},
|
|
|
|
{
|
2015-05-07 08:55:03 +00: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 22:08:58 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
output: clientmodel.LabelSet{
|
|
|
|
"a": "boobam",
|
|
|
|
"b": "bar",
|
|
|
|
"c": "baz",
|
|
|
|
"d": "boooom",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: clientmodel.LabelSet{
|
|
|
|
"a": "foo",
|
|
|
|
},
|
2015-06-04 15:03:12 +00:00
|
|
|
relabel: []*config.RelabelConfig{
|
2015-04-28 22:08:58 +00:00
|
|
|
{
|
2015-05-07 08:55:03 +00:00
|
|
|
SourceLabels: clientmodel.LabelNames{"a"},
|
|
|
|
Regex: &config.Regexp{*regexp.MustCompile("o$")},
|
|
|
|
Action: config.RelabelDrop,
|
2015-04-28 22:08:58 +00:00
|
|
|
}, {
|
2015-05-07 08:55:03 +00: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 22:08:58 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
output: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: clientmodel.LabelSet{
|
|
|
|
"a": "foo",
|
|
|
|
},
|
2015-06-04 15:03:12 +00:00
|
|
|
relabel: []*config.RelabelConfig{
|
2015-04-28 22:08:58 +00:00
|
|
|
{
|
2015-05-07 08:55:03 +00:00
|
|
|
SourceLabels: clientmodel.LabelNames{"a"},
|
|
|
|
Regex: &config.Regexp{*regexp.MustCompile("no-match")},
|
|
|
|
Action: config.RelabelDrop,
|
2015-04-28 22:08:58 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
output: clientmodel.LabelSet{
|
|
|
|
"a": "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: clientmodel.LabelSet{
|
|
|
|
"a": "foo",
|
|
|
|
},
|
2015-06-04 15:03:12 +00:00
|
|
|
relabel: []*config.RelabelConfig{
|
2015-04-28 22:08:58 +00:00
|
|
|
{
|
2015-05-07 08:55:03 +00:00
|
|
|
SourceLabels: clientmodel.LabelNames{"a"},
|
|
|
|
Regex: &config.Regexp{*regexp.MustCompile("no-match")},
|
|
|
|
Action: config.RelabelKeep,
|
2015-04-28 22:08:58 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
output: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: clientmodel.LabelSet{
|
|
|
|
"a": "foo",
|
|
|
|
},
|
2015-06-04 15:03:12 +00:00
|
|
|
relabel: []*config.RelabelConfig{
|
2015-04-28 22:08:58 +00:00
|
|
|
{
|
2015-05-07 08:55:03 +00:00
|
|
|
SourceLabels: clientmodel.LabelNames{"a"},
|
|
|
|
Regex: &config.Regexp{*regexp.MustCompile("^f")},
|
|
|
|
Action: config.RelabelKeep,
|
2015-04-28 22:08:58 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
output: clientmodel.LabelSet{
|
|
|
|
"a": "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// No replacement must be applied if there is no match.
|
|
|
|
input: clientmodel.LabelSet{
|
|
|
|
"a": "boo",
|
|
|
|
},
|
2015-06-04 15:03:12 +00:00
|
|
|
relabel: []*config.RelabelConfig{
|
2015-04-28 22:08:58 +00:00
|
|
|
{
|
2015-05-07 08:55:03 +00:00
|
|
|
SourceLabels: clientmodel.LabelNames{"a"},
|
|
|
|
Regex: &config.Regexp{*regexp.MustCompile("^f")},
|
|
|
|
TargetLabel: clientmodel.LabelName("b"),
|
|
|
|
Replacement: "bar",
|
|
|
|
Action: config.RelabelReplace,
|
2015-04-28 22:08:58 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
output: clientmodel.LabelSet{
|
|
|
|
"a": "boo",
|
|
|
|
},
|
|
|
|
},
|
2015-06-24 07:07:17 +00: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",
|
|
|
|
"d": "58",
|
|
|
|
},
|
|
|
|
},
|
2015-04-28 22:08:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2015-06-04 15:03:12 +00:00
|
|
|
res, err := Relabel(test.input, test.relabel...)
|
2015-04-28 22:08:58 +00: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|