mirror of
https://github.com/prometheus/prometheus.git
synced 2024-11-09 23:24:05 -08:00
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:
parent
60967736fa
commit
e1d5eb52f2
|
@ -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)
|
||||
|
|
|
@ -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",
|
||||
|
|
Loading…
Reference in a new issue