mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
Merge pull request #995 from prometheus/relabel-replace
retrieval: Don't include unmatched source of regex in replacement.
This commit is contained in:
commit
79eb208d0f
|
@ -47,12 +47,13 @@ func relabel(labels clientmodel.LabelSet, cfg *config.RelabelConfig) (clientmode
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
case config.RelabelReplace:
|
case config.RelabelReplace:
|
||||||
|
indexes := cfg.Regex.FindStringSubmatchIndex(val)
|
||||||
// If there is no match no replacement must take place.
|
// If there is no match no replacement must take place.
|
||||||
if !cfg.Regex.MatchString(val) {
|
if indexes == nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
res := cfg.Regex.ReplaceAllString(val, cfg.Replacement)
|
res := cfg.Regex.ExpandString([]byte{}, cfg.Replacement, val, indexes)
|
||||||
if res == "" {
|
if len(res) == 0 {
|
||||||
delete(labels, cfg.TargetLabel)
|
delete(labels, cfg.TargetLabel)
|
||||||
} else {
|
} else {
|
||||||
labels[cfg.TargetLabel] = clientmodel.LabelValue(res)
|
labels[cfg.TargetLabel] = clientmodel.LabelValue(res)
|
||||||
|
|
|
@ -90,6 +90,25 @@ func TestRelabel(t *testing.T) {
|
||||||
},
|
},
|
||||||
output: nil,
|
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{
|
input: clientmodel.LabelSet{
|
||||||
"a": "foo",
|
"a": "foo",
|
||||||
|
|
Loading…
Reference in a new issue