retrieval: Don't include unmatched source of regex in replacement.

ReplaceAllString only replaces the matching part of the regex,
the unmatched bits around it are left in place. This is not the
expected or desired behaviour as the replacement string should
be everything.

This may break users dependant on this behaviour, but
what they're doing is still possible.
This commit is contained in:
Brian Brazil 2015-08-16 23:39:39 +01:00
parent 60967736fa
commit e1d5eb52f2
2 changed files with 23 additions and 3 deletions

View file

@ -47,12 +47,13 @@ func relabel(labels clientmodel.LabelSet, cfg *config.RelabelConfig) (clientmode
return nil, nil
}
case config.RelabelReplace:
indexes := cfg.Regex.FindStringSubmatchIndex(val)
// If there is no match no replacement must take place.
if !cfg.Regex.MatchString(val) {
if indexes == nil {
break
}
res := cfg.Regex.ReplaceAllString(val, cfg.Replacement)
if res == "" {
res := cfg.Regex.ExpandString([]byte{}, cfg.Replacement, val, indexes)
if len(res) == 0 {
delete(labels, cfg.TargetLabel)
} else {
labels[cfg.TargetLabel] = clientmodel.LabelValue(res)

View file

@ -90,6 +90,25 @@ func TestRelabel(t *testing.T) {
},
output: nil,
},
{
input: clientmodel.LabelSet{
"a": "abc",
},
relabel: []*config.RelabelConfig{
{
SourceLabels: clientmodel.LabelNames{"a"},
Regex: &config.Regexp{*regexp.MustCompile("(b)")},
TargetLabel: clientmodel.LabelName("d"),
Separator: ";",
Replacement: "$1",
Action: config.RelabelReplace,
},
},
output: clientmodel.LabelSet{
"a": "abc",
"d": "b",
},
},
{
input: clientmodel.LabelSet{
"a": "foo",