mirror of https://github.com/prometheus/prometheus
Merge pull request #980 from prometheus/map-labels
Retrieval: Add relabel action to map labels names with a regex.pull/984/head
commit
24e91720ad
|
@ -610,6 +610,8 @@ const (
|
||||||
RelabelDrop RelabelAction = "drop"
|
RelabelDrop RelabelAction = "drop"
|
||||||
// Sets a label to the modulus of a hash of labels.
|
// Sets a label to the modulus of a hash of labels.
|
||||||
RelabelHashMod RelabelAction = "hashmod"
|
RelabelHashMod RelabelAction = "hashmod"
|
||||||
|
// Copy labels to other labelnames based on a regex.
|
||||||
|
RelabelLabelMap RelabelAction = "labelmap"
|
||||||
)
|
)
|
||||||
|
|
||||||
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
||||||
|
|
|
@ -60,6 +60,19 @@ func relabel(labels clientmodel.LabelSet, cfg *config.RelabelConfig) (clientmode
|
||||||
case config.RelabelHashMod:
|
case config.RelabelHashMod:
|
||||||
mod := sum64(md5.Sum([]byte(val))) % cfg.Modulus
|
mod := sum64(md5.Sum([]byte(val))) % cfg.Modulus
|
||||||
labels[cfg.TargetLabel] = clientmodel.LabelValue(fmt.Sprintf("%d", mod))
|
labels[cfg.TargetLabel] = clientmodel.LabelValue(fmt.Sprintf("%d", mod))
|
||||||
|
case config.RelabelLabelMap:
|
||||||
|
out := make(clientmodel.LabelSet, len(labels))
|
||||||
|
// Take a copy to avoid infinite loops.
|
||||||
|
for ln, lv := range labels {
|
||||||
|
out[ln] = lv
|
||||||
|
}
|
||||||
|
for ln, lv := range labels {
|
||||||
|
if cfg.Regex.MatchString(string(ln)) {
|
||||||
|
res := cfg.Regex.ReplaceAllString(string(ln), cfg.Replacement)
|
||||||
|
out[clientmodel.LabelName(res)] = lv
|
||||||
|
}
|
||||||
|
}
|
||||||
|
labels = out
|
||||||
default:
|
default:
|
||||||
panic(fmt.Errorf("retrieval.relabel: unknown relabel action type %q", cfg.Action))
|
panic(fmt.Errorf("retrieval.relabel: unknown relabel action type %q", cfg.Action))
|
||||||
}
|
}
|
||||||
|
|
|
@ -173,6 +173,50 @@ func TestRelabel(t *testing.T) {
|
||||||
"d": "976",
|
"d": "976",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
input: clientmodel.LabelSet{
|
||||||
|
"a": "foo",
|
||||||
|
"b1": "bar",
|
||||||
|
"b2": "baz",
|
||||||
|
},
|
||||||
|
relabel: []*config.RelabelConfig{
|
||||||
|
{
|
||||||
|
Regex: &config.Regexp{*regexp.MustCompile("^(b.*)")},
|
||||||
|
Replacement: "bar_${1}",
|
||||||
|
Action: config.RelabelLabelMap,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
output: clientmodel.LabelSet{
|
||||||
|
"a": "foo",
|
||||||
|
"b1": "bar",
|
||||||
|
"b2": "baz",
|
||||||
|
"bar_b1": "bar",
|
||||||
|
"bar_b2": "baz",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: clientmodel.LabelSet{
|
||||||
|
"a": "foo",
|
||||||
|
"__meta_my_bar": "aaa",
|
||||||
|
"__meta_my_baz": "bbb",
|
||||||
|
"__meta_other": "ccc",
|
||||||
|
},
|
||||||
|
relabel: []*config.RelabelConfig{
|
||||||
|
{
|
||||||
|
Regex: &config.Regexp{*regexp.MustCompile("^__meta_(my.*)")},
|
||||||
|
Replacement: "${1}",
|
||||||
|
Action: config.RelabelLabelMap,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
output: clientmodel.LabelSet{
|
||||||
|
"a": "foo",
|
||||||
|
"__meta_my_bar": "aaa",
|
||||||
|
"__meta_my_baz": "bbb",
|
||||||
|
"__meta_other": "ccc",
|
||||||
|
"my_bar": "aaa",
|
||||||
|
"my_baz": "bbb",
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, test := range tests {
|
for i, test := range tests {
|
||||||
|
|
Loading…
Reference in New Issue