diff --git a/internal/component/discovery/discovery.go b/internal/component/discovery/discovery.go index 5501db0489..03670814e1 100644 --- a/internal/component/discovery/discovery.go +++ b/internal/component/discovery/discovery.go @@ -221,21 +221,7 @@ func (c *Component) runDiscovery(ctx context.Context, d DiscovererWithMetrics) { // function to convert and send targets in format scraper expects send := func() { - allTargets := []Target{} - for _, group := range cache { - for _, target := range group.Targets { - labels := map[string]string{} - // first add the group labels, and then the - // target labels, so that target labels take precedence. - for k, v := range group.Labels { - labels[string(k)] = string(v) - } - for k, v := range target { - labels[string(k)] = string(v) - } - allTargets = append(allTargets, labels) - } - } + allTargets := toAlloyTargets(cache) componentID := livedebugging.ComponentID(c.opts.ID) if c.debugDataPublisher.IsActive(componentID) { c.debugDataPublisher.Publish(componentID, fmt.Sprintf("%s", allTargets)) @@ -274,4 +260,29 @@ func (c *Component) runDiscovery(ctx context.Context, d DiscovererWithMetrics) { } } +func toAlloyTargets(cache map[string]*targetgroup.Group) []Target { + targetsCount := 0 + for _, group := range cache { + targetsCount += len(group.Targets) + } + allTargets := make([]Target, 0, targetsCount) + + for _, group := range cache { + for _, target := range group.Targets { + tLabels := make(map[string]string, len(group.Labels)+len(target)) + + // first add the group labels, and then the + // target labels, so that target labels take precedence. + for k, v := range group.Labels { + tLabels[string(k)] = string(v) + } + for k, v := range target { + tLabels[string(k)] = string(v) + } + allTargets = append(allTargets, tLabels) + } + } + return allTargets +} + func (c *Component) LiveDebugging(_ int) {} diff --git a/internal/component/discovery/discovery_test.go b/internal/component/discovery/discovery_test.go index 6d57362ceb..71927dba95 100644 --- a/internal/component/discovery/discovery_test.go +++ b/internal/component/discovery/discovery_test.go @@ -8,6 +8,7 @@ import ( "testing" "time" + "github.com/Masterminds/goutils" "github.com/go-kit/log" "github.com/prometheus/common/model" "github.com/prometheus/prometheus/discovery/targetgroup" @@ -202,6 +203,49 @@ func TestDiscoveryUpdates(t *testing.T) { } } +/* +on darwin/arm64/Apple M2: +Benchmark_ToAlloyTargets-8 150 7549967 ns/op 12768249 B/op 40433 allocs/op +Benchmark_ToAlloyTargets-8 169 7257841 ns/op 12767441 B/op 40430 allocs/op +Benchmark_ToAlloyTargets-8 171 7026276 ns/op 12767394 B/op 40430 allocs/op +Benchmark_ToAlloyTargets-8 170 7060700 ns/op 12767377 B/op 40430 allocs/op +Benchmark_ToAlloyTargets-8 170 7034392 ns/op 12767427 B/op 40430 allocs/op +*/ +func Benchmark_ToAlloyTargets(b *testing.B) { + sharedLabels := 5 + labelsPerTarget := 5 + labelsLength := 10 + targetsCount := 20_000 + + genLabelSet := func(size int) model.LabelSet { + ls := model.LabelSet{} + for i := 0; i < size; i++ { + name, _ := goutils.RandomAlphaNumeric(labelsLength) + value, _ := goutils.RandomAlphaNumeric(labelsLength) + ls[model.LabelName(name)] = model.LabelValue(value) + } + return ls + } + + var targets = []model.LabelSet{} + for i := 0; i < targetsCount; i++ { + targets = append(targets, genLabelSet(labelsPerTarget)) + } + + cache := map[string]*targetgroup.Group{} + cache["test"] = &targetgroup.Group{ + Targets: targets, + Labels: genLabelSet(sharedLabels), + Source: "test", + } + + b.ResetTimer() + + for i := 0; i < b.N; i++ { + toAlloyTargets(cache) + } +} + func updateDiscoverer(comp *Component, discoverer *fakeDiscoverer) { comp.discMut.Lock() defer comp.discMut.Unlock()