Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce allocation in discovery by ~30% #2350

Merged
merged 2 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ Main (unreleased)

- Update `async-profiler` binaries for `pyroscope.java` to 3.0-fa937db (@aleks-p)

- Reduced memory allocation in discovery components by up to 30% (@thampiotr)

### Bugfixes

- Fixed issue with automemlimit logging bad messages and trying to access cgroup on non-linux builds (@dehaansa)
Expand Down
41 changes: 26 additions & 15 deletions internal/component/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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) {}
44 changes: 44 additions & 0 deletions internal/component/discovery/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down
Loading