From 5cfe759348d227a703afa6b68e14f78ba328b742 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Tue, 28 Feb 2023 19:35:31 +0000 Subject: [PATCH] scrape: make TargetsFromGroup work with Builder not []Label Save work converting to `Labels` then to `Builder`. `PopulateLabels()` now takes as Builder as input. Signed-off-by: Bryan Boreham --- cmd/promtool/sd.go | 10 +++++----- scrape/manager_test.go | 2 +- scrape/target.go | 14 ++++++-------- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/cmd/promtool/sd.go b/cmd/promtool/sd.go index 981a67af1..1971b9af3 100644 --- a/cmd/promtool/sd.go +++ b/cmd/promtool/sd.go @@ -109,22 +109,22 @@ outerLoop: func getSDCheckResult(targetGroups []*targetgroup.Group, scrapeConfig *config.ScrapeConfig, noDefaultScrapePort bool) []sdCheckResult { sdCheckResults := []sdCheckResult{} + lb := labels.NewBuilder(labels.EmptyLabels()) for _, targetGroup := range targetGroups { for _, target := range targetGroup.Targets { - labelSlice := make([]labels.Label, 0, len(target)+len(targetGroup.Labels)) + lb.Reset(labels.EmptyLabels()) for name, value := range target { - labelSlice = append(labelSlice, labels.Label{Name: string(name), Value: string(value)}) + lb.Set(string(name), string(value)) } for name, value := range targetGroup.Labels { if _, ok := target[name]; !ok { - labelSlice = append(labelSlice, labels.Label{Name: string(name), Value: string(value)}) + lb.Set(string(name), string(value)) } } - targetLabels := labels.New(labelSlice...) - res, orig, err := scrape.PopulateLabels(targetLabels, scrapeConfig, noDefaultScrapePort) + res, orig, err := scrape.PopulateLabels(lb, scrapeConfig, noDefaultScrapePort) result := sdCheckResult{ DiscoveredLabels: orig, Labels: res, diff --git a/scrape/manager_test.go b/scrape/manager_test.go index 3f6410dba..d048e0867 100644 --- a/scrape/manager_test.go +++ b/scrape/manager_test.go @@ -431,7 +431,7 @@ func TestPopulateLabels(t *testing.T) { for _, c := range cases { in := c.in.Copy() - res, orig, err := PopulateLabels(c.in, c.cfg, c.noDefaultPort) + res, orig, err := PopulateLabels(labels.NewBuilder(c.in), c.cfg, c.noDefaultPort) if c.err != "" { require.EqualError(t, err, c.err) } else { diff --git a/scrape/target.go b/scrape/target.go index 851a7f362..abf602c9f 100644 --- a/scrape/target.go +++ b/scrape/target.go @@ -349,7 +349,7 @@ func (app *timeLimitAppender) Append(ref storage.SeriesRef, lset labels.Labels, // PopulateLabels builds a label set from the given label set and scrape configuration. // It returns a label set before relabeling was applied as the second return value. // Returns the original discovered label set found before relabelling was applied if the target is dropped during relabeling. -func PopulateLabels(lset labels.Labels, cfg *config.ScrapeConfig, noDefaultPort bool) (res, orig labels.Labels, err error) { +func PopulateLabels(lb *labels.Builder, cfg *config.ScrapeConfig, noDefaultPort bool) (res, orig labels.Labels, err error) { // Copy labels into the labelset for the target if they are not set already. scrapeLabels := []labels.Label{ {Name: model.JobLabel, Value: cfg.JobName}, @@ -358,7 +358,6 @@ func PopulateLabels(lset labels.Labels, cfg *config.ScrapeConfig, noDefaultPort {Name: model.MetricsPathLabel, Value: cfg.MetricsPath}, {Name: model.SchemeLabel, Value: cfg.Scheme}, } - lb := labels.NewBuilder(lset) for _, l := range scrapeLabels { if lb.Get(l.Name) == "" { @@ -487,21 +486,20 @@ func TargetsFromGroup(tg *targetgroup.Group, cfg *config.ScrapeConfig, noDefault targets := make([]*Target, 0, len(tg.Targets)) failures := []error{} + lb := labels.NewBuilder(labels.EmptyLabels()) for i, tlset := range tg.Targets { - lbls := make([]labels.Label, 0, len(tlset)+len(tg.Labels)) + lb.Reset(labels.EmptyLabels()) for ln, lv := range tlset { - lbls = append(lbls, labels.Label{Name: string(ln), Value: string(lv)}) + lb.Set(string(ln), string(lv)) } for ln, lv := range tg.Labels { if _, ok := tlset[ln]; !ok { - lbls = append(lbls, labels.Label{Name: string(ln), Value: string(lv)}) + lb.Set(string(ln), string(lv)) } } - lset := labels.New(lbls...) - - lset, origLabels, err := PopulateLabels(lset, cfg, noDefaultPort) + lset, origLabels, err := PopulateLabels(lb, cfg, noDefaultPort) if err != nil { failures = append(failures, errors.Wrapf(err, "instance %d in group %s", i, tg)) }