mirror of https://github.com/prometheus/prometheus
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
481 lines
11 KiB
481 lines
11 KiB
8 years ago
|
// 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.
|
||
|
|
||
|
package relabel
|
||
|
|
||
|
import (
|
||
|
"testing"
|
||
|
|
||
|
"github.com/prometheus/common/model"
|
||
4 years ago
|
"github.com/stretchr/testify/require"
|
||
8 years ago
|
|
||
3 years ago
|
"github.com/prometheus/prometheus/model/labels"
|
||
8 years ago
|
)
|
||
|
|
||
|
func TestRelabel(t *testing.T) {
|
||
|
tests := []struct {
|
||
|
input labels.Labels
|
||
6 years ago
|
relabel []*Config
|
||
8 years ago
|
output labels.Labels
|
||
|
}{
|
||
|
{
|
||
|
input: labels.FromMap(map[string]string{
|
||
|
"a": "foo",
|
||
|
"b": "bar",
|
||
|
"c": "baz",
|
||
|
}),
|
||
6 years ago
|
relabel: []*Config{
|
||
8 years ago
|
{
|
||
|
SourceLabels: model.LabelNames{"a"},
|
||
6 years ago
|
Regex: MustNewRegexp("f(.*)"),
|
||
8 years ago
|
TargetLabel: "d",
|
||
|
Separator: ";",
|
||
|
Replacement: "ch${1}-ch${1}",
|
||
6 years ago
|
Action: Replace,
|
||
8 years ago
|
},
|
||
|
},
|
||
|
output: labels.FromMap(map[string]string{
|
||
|
"a": "foo",
|
||
|
"b": "bar",
|
||
|
"c": "baz",
|
||
|
"d": "choo-choo",
|
||
|
}),
|
||
|
},
|
||
|
{
|
||
|
input: labels.FromMap(map[string]string{
|
||
|
"a": "foo",
|
||
|
"b": "bar",
|
||
|
"c": "baz",
|
||
|
}),
|
||
6 years ago
|
relabel: []*Config{
|
||
8 years ago
|
{
|
||
|
SourceLabels: model.LabelNames{"a", "b"},
|
||
6 years ago
|
Regex: MustNewRegexp("f(.*);(.*)r"),
|
||
8 years ago
|
TargetLabel: "a",
|
||
|
Separator: ";",
|
||
|
Replacement: "b${1}${2}m", // boobam
|
||
6 years ago
|
Action: Replace,
|
||
8 years ago
|
},
|
||
|
{
|
||
|
SourceLabels: model.LabelNames{"c", "a"},
|
||
6 years ago
|
Regex: MustNewRegexp("(b).*b(.*)ba(.*)"),
|
||
8 years ago
|
TargetLabel: "d",
|
||
|
Separator: ";",
|
||
|
Replacement: "$1$2$2$3",
|
||
6 years ago
|
Action: Replace,
|
||
8 years ago
|
},
|
||
|
},
|
||
|
output: labels.FromMap(map[string]string{
|
||
|
"a": "boobam",
|
||
|
"b": "bar",
|
||
|
"c": "baz",
|
||
|
"d": "boooom",
|
||
|
}),
|
||
|
},
|
||
|
{
|
||
|
input: labels.FromMap(map[string]string{
|
||
|
"a": "foo",
|
||
|
}),
|
||
6 years ago
|
relabel: []*Config{
|
||
8 years ago
|
{
|
||
|
SourceLabels: model.LabelNames{"a"},
|
||
6 years ago
|
Regex: MustNewRegexp(".*o.*"),
|
||
|
Action: Drop,
|
||
8 years ago
|
}, {
|
||
|
SourceLabels: model.LabelNames{"a"},
|
||
6 years ago
|
Regex: MustNewRegexp("f(.*)"),
|
||
8 years ago
|
TargetLabel: "d",
|
||
|
Separator: ";",
|
||
|
Replacement: "ch$1-ch$1",
|
||
6 years ago
|
Action: Replace,
|
||
8 years ago
|
},
|
||
|
},
|
||
|
output: nil,
|
||
|
},
|
||
|
{
|
||
|
input: labels.FromMap(map[string]string{
|
||
|
"a": "foo",
|
||
|
"b": "bar",
|
||
|
}),
|
||
6 years ago
|
relabel: []*Config{
|
||
8 years ago
|
{
|
||
|
SourceLabels: model.LabelNames{"a"},
|
||
6 years ago
|
Regex: MustNewRegexp(".*o.*"),
|
||
|
Action: Drop,
|
||
8 years ago
|
},
|
||
|
},
|
||
|
output: nil,
|
||
|
},
|
||
|
{
|
||
|
input: labels.FromMap(map[string]string{
|
||
|
"a": "abc",
|
||
|
}),
|
||
6 years ago
|
relabel: []*Config{
|
||
8 years ago
|
{
|
||
|
SourceLabels: model.LabelNames{"a"},
|
||
6 years ago
|
Regex: MustNewRegexp(".*(b).*"),
|
||
8 years ago
|
TargetLabel: "d",
|
||
|
Separator: ";",
|
||
|
Replacement: "$1",
|
||
6 years ago
|
Action: Replace,
|
||
8 years ago
|
},
|
||
|
},
|
||
|
output: labels.FromMap(map[string]string{
|
||
|
"a": "abc",
|
||
|
"d": "b",
|
||
|
}),
|
||
|
},
|
||
|
{
|
||
|
input: labels.FromMap(map[string]string{
|
||
|
"a": "foo",
|
||
|
}),
|
||
6 years ago
|
relabel: []*Config{
|
||
8 years ago
|
{
|
||
|
SourceLabels: model.LabelNames{"a"},
|
||
6 years ago
|
Regex: MustNewRegexp("no-match"),
|
||
|
Action: Drop,
|
||
8 years ago
|
},
|
||
|
},
|
||
|
output: labels.FromMap(map[string]string{
|
||
|
"a": "foo",
|
||
|
}),
|
||
|
},
|
||
|
{
|
||
|
input: labels.FromMap(map[string]string{
|
||
|
"a": "foo",
|
||
|
}),
|
||
6 years ago
|
relabel: []*Config{
|
||
8 years ago
|
{
|
||
|
SourceLabels: model.LabelNames{"a"},
|
||
6 years ago
|
Regex: MustNewRegexp("f|o"),
|
||
|
Action: Drop,
|
||
8 years ago
|
},
|
||
|
},
|
||
|
output: labels.FromMap(map[string]string{
|
||
|
"a": "foo",
|
||
|
}),
|
||
|
},
|
||
|
{
|
||
|
input: labels.FromMap(map[string]string{
|
||
|
"a": "foo",
|
||
|
}),
|
||
6 years ago
|
relabel: []*Config{
|
||
8 years ago
|
{
|
||
|
SourceLabels: model.LabelNames{"a"},
|
||
6 years ago
|
Regex: MustNewRegexp("no-match"),
|
||
|
Action: Keep,
|
||
8 years ago
|
},
|
||
|
},
|
||
|
output: nil,
|
||
|
},
|
||
|
{
|
||
|
input: labels.FromMap(map[string]string{
|
||
|
"a": "foo",
|
||
|
}),
|
||
6 years ago
|
relabel: []*Config{
|
||
8 years ago
|
{
|
||
|
SourceLabels: model.LabelNames{"a"},
|
||
6 years ago
|
Regex: MustNewRegexp("f.*"),
|
||
|
Action: Keep,
|
||
8 years ago
|
},
|
||
|
},
|
||
|
output: labels.FromMap(map[string]string{
|
||
|
"a": "foo",
|
||
|
}),
|
||
|
},
|
||
|
{
|
||
|
// No replacement must be applied if there is no match.
|
||
|
input: labels.FromMap(map[string]string{
|
||
|
"a": "boo",
|
||
|
}),
|
||
6 years ago
|
relabel: []*Config{
|
||
8 years ago
|
{
|
||
|
SourceLabels: model.LabelNames{"a"},
|
||
6 years ago
|
Regex: MustNewRegexp("f"),
|
||
8 years ago
|
TargetLabel: "b",
|
||
|
Replacement: "bar",
|
||
6 years ago
|
Action: Replace,
|
||
8 years ago
|
},
|
||
|
},
|
||
|
output: labels.FromMap(map[string]string{
|
||
|
"a": "boo",
|
||
|
}),
|
||
|
},
|
||
|
{
|
||
|
input: labels.FromMap(map[string]string{
|
||
|
"a": "foo",
|
||
|
"b": "bar",
|
||
|
"c": "baz",
|
||
|
}),
|
||
6 years ago
|
relabel: []*Config{
|
||
8 years ago
|
{
|
||
|
SourceLabels: model.LabelNames{"c"},
|
||
|
TargetLabel: "d",
|
||
|
Separator: ";",
|
||
6 years ago
|
Action: HashMod,
|
||
8 years ago
|
Modulus: 1000,
|
||
|
},
|
||
|
},
|
||
|
output: labels.FromMap(map[string]string{
|
||
|
"a": "foo",
|
||
|
"b": "bar",
|
||
|
"c": "baz",
|
||
|
"d": "976",
|
||
|
}),
|
||
|
},
|
||
4 years ago
|
{
|
||
|
input: labels.FromMap(map[string]string{
|
||
|
"a": "foo\nbar",
|
||
|
}),
|
||
|
relabel: []*Config{
|
||
|
{
|
||
|
SourceLabels: model.LabelNames{"a"},
|
||
|
TargetLabel: "b",
|
||
|
Separator: ";",
|
||
|
Action: HashMod,
|
||
|
Modulus: 1000,
|
||
|
},
|
||
|
},
|
||
|
output: labels.FromMap(map[string]string{
|
||
|
"a": "foo\nbar",
|
||
|
"b": "734",
|
||
|
}),
|
||
|
},
|
||
8 years ago
|
{
|
||
|
input: labels.FromMap(map[string]string{
|
||
|
"a": "foo",
|
||
|
"b1": "bar",
|
||
|
"b2": "baz",
|
||
|
}),
|
||
6 years ago
|
relabel: []*Config{
|
||
8 years ago
|
{
|
||
6 years ago
|
Regex: MustNewRegexp("(b.*)"),
|
||
8 years ago
|
Replacement: "bar_${1}",
|
||
6 years ago
|
Action: LabelMap,
|
||
8 years ago
|
},
|
||
|
},
|
||
|
output: labels.FromMap(map[string]string{
|
||
|
"a": "foo",
|
||
|
"b1": "bar",
|
||
|
"b2": "baz",
|
||
|
"bar_b1": "bar",
|
||
|
"bar_b2": "baz",
|
||
|
}),
|
||
|
},
|
||
|
{
|
||
|
input: labels.FromMap(map[string]string{
|
||
|
"a": "foo",
|
||
|
"__meta_my_bar": "aaa",
|
||
|
"__meta_my_baz": "bbb",
|
||
|
"__meta_other": "ccc",
|
||
|
}),
|
||
6 years ago
|
relabel: []*Config{
|
||
8 years ago
|
{
|
||
6 years ago
|
Regex: MustNewRegexp("__meta_(my.*)"),
|
||
8 years ago
|
Replacement: "${1}",
|
||
6 years ago
|
Action: LabelMap,
|
||
8 years ago
|
},
|
||
|
},
|
||
|
output: labels.FromMap(map[string]string{
|
||
|
"a": "foo",
|
||
|
"__meta_my_bar": "aaa",
|
||
|
"__meta_my_baz": "bbb",
|
||
|
"__meta_other": "ccc",
|
||
|
"my_bar": "aaa",
|
||
|
"my_baz": "bbb",
|
||
|
}),
|
||
|
},
|
||
|
{ // valid case
|
||
|
input: labels.FromMap(map[string]string{
|
||
|
"a": "some-name-value",
|
||
|
}),
|
||
6 years ago
|
relabel: []*Config{
|
||
8 years ago
|
{
|
||
|
SourceLabels: model.LabelNames{"a"},
|
||
6 years ago
|
Regex: MustNewRegexp("some-([^-]+)-([^,]+)"),
|
||
|
Action: Replace,
|
||
8 years ago
|
Replacement: "${2}",
|
||
|
TargetLabel: "${1}",
|
||
|
},
|
||
|
},
|
||
|
output: labels.FromMap(map[string]string{
|
||
|
"a": "some-name-value",
|
||
|
"name": "value",
|
||
|
}),
|
||
|
},
|
||
|
{ // invalid replacement ""
|
||
|
input: labels.FromMap(map[string]string{
|
||
|
"a": "some-name-value",
|
||
|
}),
|
||
6 years ago
|
relabel: []*Config{
|
||
8 years ago
|
{
|
||
|
SourceLabels: model.LabelNames{"a"},
|
||
6 years ago
|
Regex: MustNewRegexp("some-([^-]+)-([^,]+)"),
|
||
|
Action: Replace,
|
||
8 years ago
|
Replacement: "${3}",
|
||
|
TargetLabel: "${1}",
|
||
|
},
|
||
|
},
|
||
|
output: labels.FromMap(map[string]string{
|
||
|
"a": "some-name-value",
|
||
|
}),
|
||
|
},
|
||
|
{ // invalid target_labels
|
||
|
input: labels.FromMap(map[string]string{
|
||
|
"a": "some-name-value",
|
||
|
}),
|
||
6 years ago
|
relabel: []*Config{
|
||
8 years ago
|
{
|
||
|
SourceLabels: model.LabelNames{"a"},
|
||
6 years ago
|
Regex: MustNewRegexp("some-([^-]+)-([^,]+)"),
|
||
|
Action: Replace,
|
||
8 years ago
|
Replacement: "${1}",
|
||
|
TargetLabel: "${3}",
|
||
|
},
|
||
|
{
|
||
|
SourceLabels: model.LabelNames{"a"},
|
||
6 years ago
|
Regex: MustNewRegexp("some-([^-]+)-([^,]+)"),
|
||
|
Action: Replace,
|
||
8 years ago
|
Replacement: "${1}",
|
||
|
TargetLabel: "0${3}",
|
||
|
},
|
||
|
{
|
||
|
SourceLabels: model.LabelNames{"a"},
|
||
6 years ago
|
Regex: MustNewRegexp("some-([^-]+)-([^,]+)"),
|
||
|
Action: Replace,
|
||
8 years ago
|
Replacement: "${1}",
|
||
|
TargetLabel: "-${3}",
|
||
|
},
|
||
|
},
|
||
|
output: labels.FromMap(map[string]string{
|
||
|
"a": "some-name-value",
|
||
|
}),
|
||
|
},
|
||
|
{ // more complex real-life like usecase
|
||
|
input: labels.FromMap(map[string]string{
|
||
|
"__meta_sd_tags": "path:/secret,job:some-job,label:foo=bar",
|
||
|
}),
|
||
6 years ago
|
relabel: []*Config{
|
||
8 years ago
|
{
|
||
|
SourceLabels: model.LabelNames{"__meta_sd_tags"},
|
||
6 years ago
|
Regex: MustNewRegexp("(?:.+,|^)path:(/[^,]+).*"),
|
||
|
Action: Replace,
|
||
8 years ago
|
Replacement: "${1}",
|
||
|
TargetLabel: "__metrics_path__",
|
||
|
},
|
||
|
{
|
||
|
SourceLabels: model.LabelNames{"__meta_sd_tags"},
|
||
6 years ago
|
Regex: MustNewRegexp("(?:.+,|^)job:([^,]+).*"),
|
||
|
Action: Replace,
|
||
8 years ago
|
Replacement: "${1}",
|
||
|
TargetLabel: "job",
|
||
|
},
|
||
|
{
|
||
|
SourceLabels: model.LabelNames{"__meta_sd_tags"},
|
||
6 years ago
|
Regex: MustNewRegexp("(?:.+,|^)label:([^=]+)=([^,]+).*"),
|
||
|
Action: Replace,
|
||
8 years ago
|
Replacement: "${2}",
|
||
|
TargetLabel: "${1}",
|
||
|
},
|
||
|
},
|
||
|
output: labels.FromMap(map[string]string{
|
||
|
"__meta_sd_tags": "path:/secret,job:some-job,label:foo=bar",
|
||
|
"__metrics_path__": "/secret",
|
||
|
"job": "some-job",
|
||
|
"foo": "bar",
|
||
|
}),
|
||
|
},
|
||
|
{
|
||
|
input: labels.FromMap(map[string]string{
|
||
|
"a": "foo",
|
||
|
"b1": "bar",
|
||
|
"b2": "baz",
|
||
|
}),
|
||
6 years ago
|
relabel: []*Config{
|
||
8 years ago
|
{
|
||
6 years ago
|
Regex: MustNewRegexp("(b.*)"),
|
||
|
Action: LabelKeep,
|
||
8 years ago
|
},
|
||
|
},
|
||
|
output: labels.FromMap(map[string]string{
|
||
|
"b1": "bar",
|
||
|
"b2": "baz",
|
||
|
}),
|
||
|
},
|
||
|
{
|
||
|
input: labels.FromMap(map[string]string{
|
||
|
"a": "foo",
|
||
|
"b1": "bar",
|
||
|
"b2": "baz",
|
||
|
}),
|
||
6 years ago
|
relabel: []*Config{
|
||
8 years ago
|
{
|
||
6 years ago
|
Regex: MustNewRegexp("(b.*)"),
|
||
|
Action: LabelDrop,
|
||
8 years ago
|
},
|
||
|
},
|
||
|
output: labels.FromMap(map[string]string{
|
||
|
"a": "foo",
|
||
|
}),
|
||
|
},
|
||
|
}
|
||
|
|
||
7 years ago
|
for _, test := range tests {
|
||
4 years ago
|
// Setting default fields, mimicking the behaviour in Prometheus.
|
||
|
for _, cfg := range test.relabel {
|
||
|
if cfg.Action == "" {
|
||
|
cfg.Action = DefaultRelabelConfig.Action
|
||
|
}
|
||
|
if cfg.Separator == "" {
|
||
|
cfg.Separator = DefaultRelabelConfig.Separator
|
||
|
}
|
||
|
if cfg.Regex.original == "" {
|
||
|
cfg.Regex = DefaultRelabelConfig.Regex
|
||
|
}
|
||
|
if cfg.Replacement == "" {
|
||
|
cfg.Replacement = DefaultRelabelConfig.Replacement
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
res := Process(test.input, test.relabel...)
|
||
4 years ago
|
require.Equal(t, test.output, res)
|
||
8 years ago
|
}
|
||
|
}
|
||
6 years ago
|
|
||
|
func TestTargetLabelValidity(t *testing.T) {
|
||
|
tests := []struct {
|
||
|
str string
|
||
|
valid bool
|
||
|
}{
|
||
|
{"-label", false},
|
||
|
{"label", true},
|
||
|
{"label${1}", true},
|
||
|
{"${1}label", true},
|
||
|
{"${1}", true},
|
||
|
{"${1}label", true},
|
||
|
{"${", false},
|
||
|
{"$", false},
|
||
|
{"${}", false},
|
||
|
{"foo${", false},
|
||
|
{"$1", true},
|
||
|
{"asd$2asd", true},
|
||
|
{"-foo${1}bar-", false},
|
||
|
{"_${1}_", true},
|
||
|
{"foo${bar}foo", true},
|
||
|
}
|
||
|
for _, test := range tests {
|
||
4 years ago
|
require.Equal(t, test.valid, relabelTarget.Match([]byte(test.str)),
|
||
6 years ago
|
"Expected %q to be %v", test.str, test.valid)
|
||
|
}
|
||
|
}
|