From 6c07453ec1ca1612be466e4c40d30cd0fd314304 Mon Sep 17 00:00:00 2001 From: Brian Brazil Date: Wed, 21 Dec 2016 10:00:33 +0000 Subject: [PATCH] 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. --- relabel/relabel.go | 30 ++++++++++++------------------ retrieval/target.go | 2 +- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/relabel/relabel.go b/relabel/relabel.go index da153811f0..be9fa6d82f 100644 --- a/relabel/relabel.go +++ b/relabel/relabel.go @@ -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)) } diff --git a/retrieval/target.go b/retrieval/target.go index fabf93c198..a9a793f9a8 100644 --- a/retrieval/target.go +++ b/retrieval/target.go @@ -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.