Skip to content

Commit

Permalink
Only clone the metric in the one place relabelling needs it. (prometh…
Browse files Browse the repository at this point in the history
…eus#2292)

This cuts ~17% off memory allocations related to ingesting data
in a basic setup.
  • Loading branch information
brian-brazil authored Dec 21, 2016
1 parent 2e3b42a commit 6c07453
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
26 changes: 10 additions & 16 deletions relabel/relabel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
for ln, _ := range labels {
if cfg.Regex.MatchString(string(ln)) {
delete(labels, ln)
}
}
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
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))
}
Expand Down
2 changes: 1 addition & 1 deletion retrieval/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 6c07453

Please sign in to comment.