From 0c3abdc26d4cd2e44549b122799c652c9796f8d8 Mon Sep 17 00:00:00 2001 From: Xiaonan Shen Date: Tue, 28 Jun 2022 17:58:52 +0800 Subject: [PATCH] Keep relabeled scrape interval and timeout on reloads (#10916) * Preserve relabeled scrape interval and timeout on reloads Signed-off-by: Xiaonan Shen --- scrape/manager_test.go | 6 ++++-- scrape/scrape.go | 6 +++++- scrape/scrape_test.go | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/scrape/manager_test.go b/scrape/manager_test.go index 6c5091bb6..946091881 100644 --- a/scrape/manager_test.go +++ b/scrape/manager_test.go @@ -405,8 +405,10 @@ scrape_configs: return noopLoop() } sp := &scrapePool{ - appendable: &nopAppendable{}, - activeTargets: map[uint64]*Target{}, + appendable: &nopAppendable{}, + activeTargets: map[uint64]*Target{ + 1: {}, + }, loops: map[uint64]loop{ 1: noopLoop(), }, diff --git a/scrape/scrape.go b/scrape/scrape.go index 2febb4390..9304baecd 100644 --- a/scrape/scrape.go +++ b/scrape/scrape.go @@ -426,8 +426,9 @@ func (sp *scrapePool) reload(cfg *config.ScrapeConfig) error { cache = newScrapeCache() } + t := sp.activeTargets[fp] + interval, timeout, err := t.intervalAndTimeout(interval, timeout) var ( - t = sp.activeTargets[fp] s = &targetScraper{Target: t, client: sp.client, timeout: timeout, bodySizeLimit: bodySizeLimit} newLoop = sp.newLoop(scrapeLoopOptions{ target: t, @@ -442,6 +443,9 @@ func (sp *scrapePool) reload(cfg *config.ScrapeConfig) error { timeout: timeout, }) ) + if err != nil { + newLoop.setForcedError(err) + } wg.Add(1) go func(oldLoop, newLoop loop) { diff --git a/scrape/scrape_test.go b/scrape/scrape_test.go index 43200d440..d887a0c9d 100644 --- a/scrape/scrape_test.go +++ b/scrape/scrape_test.go @@ -325,6 +325,40 @@ func TestScrapePoolReload(t *testing.T) { require.Equal(t, numTargets, len(sp.loops), "Unexpected number of stopped loops after reload") } +func TestScrapePoolReloadPreserveRelabeledIntervalTimeout(t *testing.T) { + reloadCfg := &config.ScrapeConfig{ + ScrapeInterval: model.Duration(3 * time.Second), + ScrapeTimeout: model.Duration(2 * time.Second), + } + newLoop := func(opts scrapeLoopOptions) loop { + l := &testLoop{interval: time.Duration(opts.interval), timeout: time.Duration(opts.timeout)} + l.startFunc = func(interval, timeout time.Duration, errc chan<- error) { + require.Equal(t, 5*time.Second, interval, "Unexpected scrape interval") + require.Equal(t, 3*time.Second, timeout, "Unexpected scrape timeout") + } + return l + } + sp := &scrapePool{ + appendable: &nopAppendable{}, + activeTargets: map[uint64]*Target{ + 1: { + labels: labels.FromStrings(model.ScrapeIntervalLabel, "5s", model.ScrapeTimeoutLabel, "3s"), + }, + }, + loops: map[uint64]loop{ + 1: noopLoop(), + }, + newLoop: newLoop, + logger: nil, + client: http.DefaultClient, + } + + err := sp.reload(reloadCfg) + if err != nil { + t.Fatalf("unable to reload configuration: %s", err) + } +} + func TestScrapePoolTargetLimit(t *testing.T) { var wg sync.WaitGroup // On starting to run, new loops created on reload check whether their preceding