relabel: blank replacement deletes label post-regexp

If `cfg.TargetLabel` is a template like `$1`, it won't match any labels,
so no point calling `lb.Del` with it.

Similarly if `target` is not a valid label name, it won't match any
labels, so don't call with that either.

The intention seems to have been that a blank _value_ would delete the
target, so change that code to use `target` instead of `cfg.TargetLabel`.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
Bryan Boreham 2023-12-18 16:38:59 +00:00
parent 000182e4b8
commit 0289dd6157
2 changed files with 20 additions and 2 deletions

View File

@ -274,12 +274,11 @@ func relabel(cfg *Config, lb *labels.Builder) (keep bool) {
}
target := model.LabelName(cfg.Regex.ExpandString([]byte{}, cfg.TargetLabel, val, indexes))
if !target.IsValid() {
lb.Del(cfg.TargetLabel)
break
}
res := cfg.Regex.ExpandString([]byte{}, cfg.Replacement, val, indexes)
if len(res) == 0 {
lb.Del(cfg.TargetLabel)
lb.Del(string(target))
break
}
lb.Set(string(target), string(res))

View File

@ -214,6 +214,25 @@ func TestRelabel(t *testing.T) {
"a": "boo",
}),
},
{
// Blank replacement should delete the label.
input: labels.FromMap(map[string]string{
"a": "foo",
"f": "baz",
}),
relabel: []*Config{
{
SourceLabels: model.LabelNames{"a"},
Regex: MustNewRegexp("(f).*"),
TargetLabel: "$1",
Replacement: "$2",
Action: Replace,
},
},
output: labels.FromMap(map[string]string{
"a": "foo",
}),
},
{
input: labels.FromMap(map[string]string{
"a": "foo",