mirror of
https://github.com/prometheus/prometheus.git
synced 2025-01-12 06:17:27 -08:00
Add labeldrop and labelkeep actions. (#2279)
Introduce two new relabel actions. labeldrop, and labelkeep. These can be used to filter the set of labels by matching regex - labeldrop: drops all labels that match the regex - labelkeep: drops all labels that do not match the regex
This commit is contained in:
parent
45570e5972
commit
4d9134e6d8
|
@ -1100,6 +1100,10 @@ const (
|
||||||
RelabelHashMod RelabelAction = "hashmod"
|
RelabelHashMod RelabelAction = "hashmod"
|
||||||
// RelabelLabelMap copies labels to other labelnames based on a regex.
|
// RelabelLabelMap copies labels to other labelnames based on a regex.
|
||||||
RelabelLabelMap RelabelAction = "labelmap"
|
RelabelLabelMap RelabelAction = "labelmap"
|
||||||
|
// RelabelLabelDrop drops any label matching the regex.
|
||||||
|
RelabelLabelDrop RelabelAction = "labeldrop"
|
||||||
|
// RelabelLabelKeep drops any label not matching the regex.
|
||||||
|
RelabelLabelKeep RelabelAction = "labelkeep"
|
||||||
)
|
)
|
||||||
|
|
||||||
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
||||||
|
@ -1109,7 +1113,7 @@ func (a *RelabelAction) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
switch act := RelabelAction(strings.ToLower(s)); act {
|
switch act := RelabelAction(strings.ToLower(s)); act {
|
||||||
case RelabelReplace, RelabelKeep, RelabelDrop, RelabelHashMod, RelabelLabelMap:
|
case RelabelReplace, RelabelKeep, RelabelDrop, RelabelHashMod, RelabelLabelMap, RelabelLabelDrop, RelabelLabelKeep:
|
||||||
*a = act
|
*a = act
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -193,6 +193,18 @@ var expectedConf = &Config{
|
||||||
Replacement: DefaultRelabelConfig.Replacement,
|
Replacement: DefaultRelabelConfig.Replacement,
|
||||||
Action: RelabelLabelMap,
|
Action: RelabelLabelMap,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Regex: MustNewRegexp("d"),
|
||||||
|
Separator: ";",
|
||||||
|
Replacement: DefaultRelabelConfig.Replacement,
|
||||||
|
Action: RelabelLabelDrop,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Regex: MustNewRegexp("k"),
|
||||||
|
Separator: ";",
|
||||||
|
Replacement: DefaultRelabelConfig.Replacement,
|
||||||
|
Action: RelabelLabelKeep,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
MetricRelabelConfigs: []*RelabelConfig{
|
MetricRelabelConfigs: []*RelabelConfig{
|
||||||
{
|
{
|
||||||
|
|
4
config/testdata/conf.good.yml
vendored
4
config/testdata/conf.good.yml
vendored
|
@ -95,6 +95,10 @@ scrape_configs:
|
||||||
action: keep
|
action: keep
|
||||||
- action: labelmap
|
- action: labelmap
|
||||||
regex: 1
|
regex: 1
|
||||||
|
- action: labeldrop
|
||||||
|
regex: d
|
||||||
|
- action: labelkeep
|
||||||
|
regex: k
|
||||||
|
|
||||||
metric_relabel_configs:
|
metric_relabel_configs:
|
||||||
- source_labels: [__name__]
|
- source_labels: [__name__]
|
||||||
|
|
|
@ -88,6 +88,22 @@ func relabel(labels model.LabelSet, cfg *config.RelabelConfig) model.LabelSet {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
labels = out
|
labels = out
|
||||||
|
case config.RelabelLabelDrop:
|
||||||
|
out := make(model.LabelSet, len(labels))
|
||||||
|
for ln, lv := range labels {
|
||||||
|
if !cfg.Regex.MatchString(string(ln)) {
|
||||||
|
out[ln] = lv
|
||||||
|
}
|
||||||
|
}
|
||||||
|
labels = out
|
||||||
|
case config.RelabelLabelKeep:
|
||||||
|
out := make(model.LabelSet, len(labels))
|
||||||
|
for ln, lv := range labels {
|
||||||
|
if cfg.Regex.MatchString(string(ln)) {
|
||||||
|
out[ln] = 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))
|
||||||
}
|
}
|
||||||
|
|
|
@ -377,6 +377,39 @@ func TestRelabel(t *testing.T) {
|
||||||
"foo": "bar",
|
"foo": "bar",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
input: model.LabelSet{
|
||||||
|
"a": "foo",
|
||||||
|
"b1": "bar",
|
||||||
|
"b2": "baz",
|
||||||
|
},
|
||||||
|
relabel: []*config.RelabelConfig{
|
||||||
|
{
|
||||||
|
Regex: config.MustNewRegexp("(b.*)"),
|
||||||
|
Action: config.RelabelLabelKeep,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
output: model.LabelSet{
|
||||||
|
"b1": "bar",
|
||||||
|
"b2": "baz",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: model.LabelSet{
|
||||||
|
"a": "foo",
|
||||||
|
"b1": "bar",
|
||||||
|
"b2": "baz",
|
||||||
|
},
|
||||||
|
relabel: []*config.RelabelConfig{
|
||||||
|
{
|
||||||
|
Regex: config.MustNewRegexp("(b.*)"),
|
||||||
|
Action: config.RelabelLabelDrop,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
output: model.LabelSet{
|
||||||
|
"a": "foo",
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, test := range tests {
|
for i, test := range tests {
|
||||||
|
|
Loading…
Reference in a new issue