2015-08-24 17:19:21 +00:00
|
|
|
// Copyright 2015 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-08-09 10:36:23 +00:00
|
|
|
package relabel
|
2015-04-28 22:08:58 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2015-08-20 15:18:46 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2015-04-28 22:08:58 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRelabel(t *testing.T) {
|
|
|
|
tests := []struct {
|
2015-08-20 15:18:46 +00:00
|
|
|
input model.LabelSet
|
2015-06-04 15:03:12 +00:00
|
|
|
relabel []*config.RelabelConfig
|
2015-08-20 15:18:46 +00:00
|
|
|
output model.LabelSet
|
2015-04-28 22:08:58 +00:00
|
|
|
}{
|
|
|
|
{
|
2015-08-20 15:18:46 +00:00
|
|
|
input: model.LabelSet{
|
2015-04-28 22:08:58 +00:00
|
|
|
"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-08-20 15:18:46 +00:00
|
|
|
SourceLabels: model.LabelNames{"a"},
|
2015-09-01 13:05:14 +00:00
|
|
|
Regex: config.MustNewRegexp("f(.*)"),
|
2015-08-20 15:18:46 +00:00
|
|
|
TargetLabel: model.LabelName("d"),
|
2015-05-07 08:55:03 +00:00
|
|
|
Separator: ";",
|
|
|
|
Replacement: "ch${1}-ch${1}",
|
|
|
|
Action: config.RelabelReplace,
|
2015-04-28 22:08:58 +00:00
|
|
|
},
|
|
|
|
},
|
2015-08-20 15:18:46 +00:00
|
|
|
output: model.LabelSet{
|
2015-04-28 22:08:58 +00:00
|
|
|
"a": "foo",
|
|
|
|
"b": "bar",
|
|
|
|
"c": "baz",
|
|
|
|
"d": "choo-choo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-08-20 15:18:46 +00:00
|
|
|
input: model.LabelSet{
|
2015-04-28 22:08:58 +00:00
|
|
|
"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-08-20 15:18:46 +00:00
|
|
|
SourceLabels: model.LabelNames{"a", "b"},
|
2015-09-01 13:05:14 +00:00
|
|
|
Regex: config.MustNewRegexp("f(.*);(.*)r"),
|
2015-08-20 15:18:46 +00:00
|
|
|
TargetLabel: model.LabelName("a"),
|
2015-05-07 08:55:03 +00:00
|
|
|
Separator: ";",
|
|
|
|
Replacement: "b${1}${2}m", // boobam
|
|
|
|
Action: config.RelabelReplace,
|
2015-04-28 22:08:58 +00:00
|
|
|
},
|
|
|
|
{
|
2015-08-20 15:18:46 +00:00
|
|
|
SourceLabels: model.LabelNames{"c", "a"},
|
2015-09-01 13:05:14 +00:00
|
|
|
Regex: config.MustNewRegexp("(b).*b(.*)ba(.*)"),
|
2015-08-20 15:18:46 +00:00
|
|
|
TargetLabel: model.LabelName("d"),
|
2015-05-07 08:55:03 +00:00
|
|
|
Separator: ";",
|
|
|
|
Replacement: "$1$2$2$3",
|
|
|
|
Action: config.RelabelReplace,
|
2015-04-28 22:08:58 +00:00
|
|
|
},
|
|
|
|
},
|
2015-08-20 15:18:46 +00:00
|
|
|
output: model.LabelSet{
|
2015-04-28 22:08:58 +00:00
|
|
|
"a": "boobam",
|
|
|
|
"b": "bar",
|
|
|
|
"c": "baz",
|
|
|
|
"d": "boooom",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-08-20 15:18:46 +00:00
|
|
|
input: model.LabelSet{
|
2015-04-28 22:08:58 +00:00
|
|
|
"a": "foo",
|
|
|
|
},
|
2015-06-04 15:03:12 +00:00
|
|
|
relabel: []*config.RelabelConfig{
|
2015-04-28 22:08:58 +00:00
|
|
|
{
|
2015-08-20 15:18:46 +00:00
|
|
|
SourceLabels: model.LabelNames{"a"},
|
2015-09-01 13:05:14 +00:00
|
|
|
Regex: config.MustNewRegexp(".*o.*"),
|
2015-05-07 08:55:03 +00:00
|
|
|
Action: config.RelabelDrop,
|
2015-04-28 22:08:58 +00:00
|
|
|
}, {
|
2015-08-20 15:18:46 +00:00
|
|
|
SourceLabels: model.LabelNames{"a"},
|
2015-09-01 13:05:14 +00:00
|
|
|
Regex: config.MustNewRegexp("f(.*)"),
|
2015-08-20 15:18:46 +00:00
|
|
|
TargetLabel: model.LabelName("d"),
|
2015-05-07 08:55:03 +00:00
|
|
|
Separator: ";",
|
|
|
|
Replacement: "ch$1-ch$1",
|
|
|
|
Action: config.RelabelReplace,
|
2015-04-28 22:08:58 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
output: nil,
|
|
|
|
},
|
2016-06-07 16:13:30 +00:00
|
|
|
{
|
|
|
|
input: model.LabelSet{
|
|
|
|
"a": "foo",
|
|
|
|
"b": "bar",
|
|
|
|
},
|
|
|
|
relabel: []*config.RelabelConfig{
|
|
|
|
{
|
|
|
|
SourceLabels: model.LabelNames{"a"},
|
|
|
|
Regex: config.MustNewRegexp(".*o.*"),
|
|
|
|
Action: config.RelabelDrop,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
output: nil,
|
|
|
|
},
|
2015-08-16 22:39:39 +00:00
|
|
|
{
|
2015-08-20 15:18:46 +00:00
|
|
|
input: model.LabelSet{
|
2015-08-16 22:39:39 +00:00
|
|
|
"a": "abc",
|
|
|
|
},
|
|
|
|
relabel: []*config.RelabelConfig{
|
|
|
|
{
|
2015-08-20 15:18:46 +00:00
|
|
|
SourceLabels: model.LabelNames{"a"},
|
2015-09-01 13:05:14 +00:00
|
|
|
Regex: config.MustNewRegexp(".*(b).*"),
|
2015-08-20 15:18:46 +00:00
|
|
|
TargetLabel: model.LabelName("d"),
|
2015-08-16 22:39:39 +00:00
|
|
|
Separator: ";",
|
|
|
|
Replacement: "$1",
|
|
|
|
Action: config.RelabelReplace,
|
|
|
|
},
|
|
|
|
},
|
2015-08-20 15:18:46 +00:00
|
|
|
output: model.LabelSet{
|
2015-08-16 22:39:39 +00:00
|
|
|
"a": "abc",
|
|
|
|
"d": "b",
|
|
|
|
},
|
|
|
|
},
|
2015-04-28 22:08:58 +00:00
|
|
|
{
|
2015-08-20 15:18:46 +00:00
|
|
|
input: model.LabelSet{
|
2015-04-28 22:08:58 +00:00
|
|
|
"a": "foo",
|
|
|
|
},
|
2015-06-04 15:03:12 +00:00
|
|
|
relabel: []*config.RelabelConfig{
|
2015-04-28 22:08:58 +00:00
|
|
|
{
|
2015-08-20 15:18:46 +00:00
|
|
|
SourceLabels: model.LabelNames{"a"},
|
2015-09-01 13:05:14 +00:00
|
|
|
Regex: config.MustNewRegexp("no-match"),
|
2015-05-07 08:55:03 +00:00
|
|
|
Action: config.RelabelDrop,
|
2015-04-28 22:08:58 +00:00
|
|
|
},
|
|
|
|
},
|
2015-08-20 15:18:46 +00:00
|
|
|
output: model.LabelSet{
|
2015-04-28 22:08:58 +00:00
|
|
|
"a": "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-08-20 15:18:46 +00:00
|
|
|
input: model.LabelSet{
|
2015-04-28 22:08:58 +00:00
|
|
|
"a": "foo",
|
|
|
|
},
|
2015-06-04 15:03:12 +00:00
|
|
|
relabel: []*config.RelabelConfig{
|
2015-04-28 22:08:58 +00:00
|
|
|
{
|
2015-08-20 15:18:46 +00:00
|
|
|
SourceLabels: model.LabelNames{"a"},
|
2015-09-01 13:05:14 +00:00
|
|
|
Regex: config.MustNewRegexp("f|o"),
|
|
|
|
Action: config.RelabelDrop,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
output: model.LabelSet{
|
|
|
|
"a": "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: model.LabelSet{
|
|
|
|
"a": "foo",
|
|
|
|
},
|
|
|
|
relabel: []*config.RelabelConfig{
|
|
|
|
{
|
|
|
|
SourceLabels: model.LabelNames{"a"},
|
|
|
|
Regex: config.MustNewRegexp("no-match"),
|
2015-05-07 08:55:03 +00:00
|
|
|
Action: config.RelabelKeep,
|
2015-04-28 22:08:58 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
output: nil,
|
|
|
|
},
|
|
|
|
{
|
2015-08-20 15:18:46 +00:00
|
|
|
input: model.LabelSet{
|
2015-04-28 22:08:58 +00:00
|
|
|
"a": "foo",
|
|
|
|
},
|
2015-06-04 15:03:12 +00:00
|
|
|
relabel: []*config.RelabelConfig{
|
2015-04-28 22:08:58 +00:00
|
|
|
{
|
2015-08-20 15:18:46 +00:00
|
|
|
SourceLabels: model.LabelNames{"a"},
|
2015-09-01 13:05:14 +00:00
|
|
|
Regex: config.MustNewRegexp("f.*"),
|
2015-05-07 08:55:03 +00:00
|
|
|
Action: config.RelabelKeep,
|
2015-04-28 22:08:58 +00:00
|
|
|
},
|
|
|
|
},
|
2015-08-20 15:18:46 +00:00
|
|
|
output: model.LabelSet{
|
2015-04-28 22:08:58 +00:00
|
|
|
"a": "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// No replacement must be applied if there is no match.
|
2015-08-20 15:18:46 +00:00
|
|
|
input: model.LabelSet{
|
2015-04-28 22:08:58 +00:00
|
|
|
"a": "boo",
|
|
|
|
},
|
2015-06-04 15:03:12 +00:00
|
|
|
relabel: []*config.RelabelConfig{
|
2015-04-28 22:08:58 +00:00
|
|
|
{
|
2015-08-20 15:18:46 +00:00
|
|
|
SourceLabels: model.LabelNames{"a"},
|
2015-09-01 13:05:14 +00:00
|
|
|
Regex: config.MustNewRegexp("f"),
|
2015-08-20 15:18:46 +00:00
|
|
|
TargetLabel: model.LabelName("b"),
|
2015-05-07 08:55:03 +00:00
|
|
|
Replacement: "bar",
|
|
|
|
Action: config.RelabelReplace,
|
2015-04-28 22:08:58 +00:00
|
|
|
},
|
|
|
|
},
|
2015-08-20 15:18:46 +00:00
|
|
|
output: model.LabelSet{
|
2015-04-28 22:08:58 +00:00
|
|
|
"a": "boo",
|
|
|
|
},
|
|
|
|
},
|
2015-06-24 07:07:17 +00:00
|
|
|
{
|
2015-08-20 15:18:46 +00:00
|
|
|
input: model.LabelSet{
|
2015-06-24 07:07:17 +00:00
|
|
|
"a": "foo",
|
|
|
|
"b": "bar",
|
|
|
|
"c": "baz",
|
|
|
|
},
|
|
|
|
relabel: []*config.RelabelConfig{
|
|
|
|
{
|
2015-08-20 15:18:46 +00:00
|
|
|
SourceLabels: model.LabelNames{"c"},
|
|
|
|
TargetLabel: model.LabelName("d"),
|
2015-06-24 07:07:17 +00:00
|
|
|
Separator: ";",
|
|
|
|
Action: config.RelabelHashMod,
|
|
|
|
Modulus: 1000,
|
|
|
|
},
|
|
|
|
},
|
2015-08-20 15:18:46 +00:00
|
|
|
output: model.LabelSet{
|
2015-06-24 07:07:17 +00:00
|
|
|
"a": "foo",
|
|
|
|
"b": "bar",
|
|
|
|
"c": "baz",
|
2015-08-12 14:44:42 +00:00
|
|
|
"d": "976",
|
2015-06-24 07:07:17 +00:00
|
|
|
},
|
|
|
|
},
|
2015-08-12 09:21:20 +00:00
|
|
|
{
|
2015-08-20 15:18:46 +00:00
|
|
|
input: model.LabelSet{
|
2015-08-12 09:21:20 +00:00
|
|
|
"a": "foo",
|
|
|
|
"b1": "bar",
|
|
|
|
"b2": "baz",
|
|
|
|
},
|
|
|
|
relabel: []*config.RelabelConfig{
|
|
|
|
{
|
2015-09-01 13:05:14 +00:00
|
|
|
Regex: config.MustNewRegexp("(b.*)"),
|
2015-08-12 09:21:20 +00:00
|
|
|
Replacement: "bar_${1}",
|
|
|
|
Action: config.RelabelLabelMap,
|
|
|
|
},
|
|
|
|
},
|
2015-08-20 15:18:46 +00:00
|
|
|
output: model.LabelSet{
|
2015-08-12 09:21:20 +00:00
|
|
|
"a": "foo",
|
|
|
|
"b1": "bar",
|
|
|
|
"b2": "baz",
|
|
|
|
"bar_b1": "bar",
|
|
|
|
"bar_b2": "baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-08-20 15:18:46 +00:00
|
|
|
input: model.LabelSet{
|
2015-08-12 09:21:20 +00:00
|
|
|
"a": "foo",
|
|
|
|
"__meta_my_bar": "aaa",
|
|
|
|
"__meta_my_baz": "bbb",
|
|
|
|
"__meta_other": "ccc",
|
|
|
|
},
|
|
|
|
relabel: []*config.RelabelConfig{
|
|
|
|
{
|
2015-09-01 13:05:14 +00:00
|
|
|
Regex: config.MustNewRegexp("__meta_(my.*)"),
|
2015-08-12 09:21:20 +00:00
|
|
|
Replacement: "${1}",
|
|
|
|
Action: config.RelabelLabelMap,
|
|
|
|
},
|
|
|
|
},
|
2015-08-20 15:18:46 +00:00
|
|
|
output: model.LabelSet{
|
2015-08-12 09:21:20 +00:00
|
|
|
"a": "foo",
|
|
|
|
"__meta_my_bar": "aaa",
|
|
|
|
"__meta_my_baz": "bbb",
|
|
|
|
"__meta_other": "ccc",
|
|
|
|
"my_bar": "aaa",
|
|
|
|
"my_baz": "bbb",
|
|
|
|
},
|
|
|
|
},
|
2015-04-28 22:08:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2016-08-09 10:36:23 +00:00
|
|
|
res := Process(test.input, test.relabel...)
|
2015-04-28 22:08:58 +00:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(res, test.output) {
|
|
|
|
t.Errorf("Test %d: relabel output mismatch: expected %#v, got %#v", i+1, test.output, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|