mirror of
https://github.com/prometheus/prometheus.git
synced 2024-11-09 23:24:05 -08:00
Merge pull request #980 from prometheus/map-labels
Retrieval: Add relabel action to map labels names with a regex.
This commit is contained in:
commit
24e91720ad
|
@ -610,6 +610,8 @@ const (
|
|||
RelabelDrop RelabelAction = "drop"
|
||||
// Sets a label to the modulus of a hash of labels.
|
||||
RelabelHashMod RelabelAction = "hashmod"
|
||||
// Copy labels to other labelnames based on a regex.
|
||||
RelabelLabelMap RelabelAction = "labelmap"
|
||||
)
|
||||
|
||||
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
||||
|
|
|
@ -60,6 +60,19 @@ func relabel(labels clientmodel.LabelSet, cfg *config.RelabelConfig) (clientmode
|
|||
case config.RelabelHashMod:
|
||||
mod := sum64(md5.Sum([]byte(val))) % cfg.Modulus
|
||||
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:
|
||||
panic(fmt.Errorf("retrieval.relabel: unknown relabel action type %q", cfg.Action))
|
||||
}
|
||||
|
|
|
@ -173,6 +173,50 @@ func TestRelabel(t *testing.T) {
|
|||
"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 {
|
||||
|
|
Loading…
Reference in a new issue