Only clone the metric in the one place relabelling needs it. (#2292)

This cuts ~17% off memory allocations related to ingesting data
in a basic setup.
This commit is contained in:
Brian Brazil 2016-12-21 10:00:33 +00:00 committed by GitHub
parent 2e3b42ad6c
commit 6c07453ec1
2 changed files with 13 additions and 19 deletions

View File

@ -26,17 +26,15 @@ import (
// Process returns a relabeled copy of the given label set. The relabel configurations
// are applied in order of input.
// If a label set is dropped, nil is returned.
// May return the input labelSet modified.
func Process(labels model.LabelSet, cfgs ...*config.RelabelConfig) model.LabelSet {
out := model.LabelSet{}
for ln, lv := range labels {
out[ln] = lv
}
for _, cfg := range cfgs {
if out = relabel(out, cfg); out == nil {
labels = relabel(labels, cfg)
if labels == nil {
return nil
}
}
return out
return labels
}
func relabel(labels model.LabelSet, cfg *config.RelabelConfig) model.LabelSet {
@ -89,21 +87,17 @@ 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 {
for ln, _ := range labels {
if cfg.Regex.MatchString(string(ln)) {
out[ln] = lv
delete(labels, ln)
}
}
case config.RelabelLabelKeep:
for ln, _ := range labels {
if !cfg.Regex.MatchString(string(ln)) {
delete(labels, ln)
}
}
labels = out
default:
panic(fmt.Errorf("retrieval.relabel: unknown relabel action type %q", cfg.Action))
}

View File

@ -304,7 +304,7 @@ func populateLabels(lset model.LabelSet, cfg *config.ScrapeConfig) (res, orig mo
}
}
preRelabelLabels := lset
preRelabelLabels := lset.Clone()
lset = relabel.Process(lset, cfg.RelabelConfigs...)
// Check if the target was dropped.