Skip to content

Commit

Permalink
Reduce allocation in discovery by ~30% (#2350)
Browse files Browse the repository at this point in the history
* Reduce allocation in discovery by 30%

* changelog
  • Loading branch information
thampiotr authored Jan 8, 2025
1 parent 0afec17 commit 2b78534
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 15 deletions.
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

0 comments on commit 2b78534

Please sign in to comment.