Skip to content

Commit

Permalink
Add labeldrop and labelkeep actions. (prometheus#2279)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tcolgate authored and brian-brazil committed Dec 14, 2016
1 parent 45570e5 commit 4d9134e
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 1 deletion.
6 changes: 5 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,10 @@ const (
RelabelHashMod RelabelAction = "hashmod"
// RelabelLabelMap copies labels to other labelnames based on a regex.
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.
Expand All @@ -1109,7 +1113,7 @@ func (a *RelabelAction) UnmarshalYAML(unmarshal func(interface{}) error) error {
return err
}
switch act := RelabelAction(strings.ToLower(s)); act {
case RelabelReplace, RelabelKeep, RelabelDrop, RelabelHashMod, RelabelLabelMap:
case RelabelReplace, RelabelKeep, RelabelDrop, RelabelHashMod, RelabelLabelMap, RelabelLabelDrop, RelabelLabelKeep:
*a = act
return nil
}
Expand Down
12 changes: 12 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,18 @@ var expectedConf = &Config{
Replacement: DefaultRelabelConfig.Replacement,
Action: RelabelLabelMap,
},
{
Regex: MustNewRegexp("d"),
Separator: ";",
Replacement: DefaultRelabelConfig.Replacement,
Action: RelabelLabelDrop,
},
{
Regex: MustNewRegexp("k"),
Separator: ";",
Replacement: DefaultRelabelConfig.Replacement,
Action: RelabelLabelKeep,
},
},
MetricRelabelConfigs: []*RelabelConfig{
{
Expand Down
4 changes: 4 additions & 0 deletions config/testdata/conf.good.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ scrape_configs:
action: keep
- action: labelmap
regex: 1
- action: labeldrop
regex: d
- action: labelkeep
regex: k

metric_relabel_configs:
- source_labels: [__name__]
Expand Down
16 changes: 16 additions & 0 deletions relabel/relabel.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,22 @@ func relabel(labels model.LabelSet, cfg *config.RelabelConfig) model.LabelSet {
}
}
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:
panic(fmt.Errorf("retrieval.relabel: unknown relabel action type %q", cfg.Action))
}
Expand Down
33 changes: 33 additions & 0 deletions relabel/relabel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,39 @@ func TestRelabel(t *testing.T) {
"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 {
Expand Down

0 comments on commit 4d9134e

Please sign in to comment.