From 57b5bdadb3e6fc4b670988013e11f98687951baa Mon Sep 17 00:00:00 2001 From: Paulin Todev Date: Thu, 19 Dec 2024 14:10:44 +0200 Subject: [PATCH] Remove Pyroscope scrape loops for no longer active targets (#7082) --- CHANGELOG.md | 2 ++ internal/component/pyroscope/scrape/manager.go | 8 ++++++++ internal/component/pyroscope/scrape/manager_test.go | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2731aa559354..23174c39340a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ Main (unreleased) - `loki.source.podlogs`: Fixed a bug which prevented clustering from working and caused duplicate logs to be sent. The bug only happened when no `selector` or `namespace_selector` blocks were specified in the Agent configuration. (@ptodev) +- `pyroscope.scrape` no longer tries to scrape endpoints which are not active targets anymore. (@wildum @mattdurham @dehaansa @ptodev) + ### Enhancements - Upgrade `github.com/goccy/go-json` to v0.10.4, which reduces the memory consumption of an Agent instance by 20MB. diff --git a/internal/component/pyroscope/scrape/manager.go b/internal/component/pyroscope/scrape/manager.go index 7eddeb31cd59..91872c740853 100644 --- a/internal/component/pyroscope/scrape/manager.go +++ b/internal/component/pyroscope/scrape/manager.go @@ -103,6 +103,14 @@ func (m *Manager) reload() { wg.Done() }(m.targetsGroups[setName], groups) } + + for tgName, sp := range m.targetsGroups { + if _, ok := m.targetSets[tgName]; !ok { + sp.stop() + delete(m.targetsGroups, tgName) + } + } + wg.Wait() } diff --git a/internal/component/pyroscope/scrape/manager_test.go b/internal/component/pyroscope/scrape/manager_test.go index 8b56cf2cbc38..8301785d46f0 100644 --- a/internal/component/pyroscope/scrape/manager_test.go +++ b/internal/component/pyroscope/scrape/manager_test.go @@ -11,9 +11,12 @@ import ( "github.com/prometheus/prometheus/discovery/targetgroup" "github.com/prometheus/prometheus/model/labels" "github.com/stretchr/testify/require" + "go.uber.org/goleak" ) func TestManager(t *testing.T) { + defer goleak.VerifyNone(t, goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start")) + reloadInterval = time.Millisecond m := NewManager(pyroscope.AppendableFunc(func(ctx context.Context, labels labels.Labels, samples []*pyroscope.RawSample) error { @@ -62,6 +65,8 @@ func TestManager(t *testing.T) { return len(m.TargetsActive()["group2"]) == 10 }, time.Second, 10*time.Millisecond) + require.Equal(t, 1, len(m.targetsGroups)) + for _, ts := range m.targetsGroups { require.Equal(t, 1*time.Second, ts.config.ScrapeInterval) }