From 375d5483facf6438a8b3a1df182b04444153bebe Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Wed, 11 Mar 2020 16:30:14 +0200 Subject: [PATCH] lib/promscrape: remove possible races when registering and de-registering scrape workers for `/targets` page --- lib/promscrape/scraper.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/lib/promscrape/scraper.go b/lib/promscrape/scraper.go index 0b872eb2e..920f58c0e 100644 --- a/lib/promscrape/scraper.go +++ b/lib/promscrape/scraper.go @@ -64,8 +64,6 @@ func runScraper(configFile string, pushData func(wr *prompbmarshal.WriteRequest) if err != nil { logger.Fatalf("cannot parse `file_sd_config` from %q: %s", configFile, err) } - tsmGlobal.RegisterAll(swsStatic) - tsmGlobal.RegisterAll(swsFileSD) mustStop := false for !mustStop { @@ -100,13 +98,9 @@ func runScraper(configFile string, pushData func(wr *prompbmarshal.WriteRequest) if err != nil { logger.Errorf("cannot parse `file_sd_config` from %q: %s; continuing with the previous config", configFile, err) } - tsmGlobal.UnregisterAll(swsStatic) - tsmGlobal.UnregisterAll(swsFileSD) cfg = cfgNew swsStatic = swsStaticNew swsFileSD = swsFileSDNew - tsmGlobal.RegisterAll(swsStatic) - tsmGlobal.RegisterAll(swsFileSD) case <-globalStopCh: mustStop = true } @@ -166,9 +160,7 @@ func runFileSDScrapers(sws []ScrapeWork, cfg *Config, pushData func(wr *prompbma goto waitForChans } logger.Infof("restarting scrapers for changed `file_sd_config` targets") - tsmGlobal.UnregisterAll(sws) sws = swsNew - tsmGlobal.RegisterAll(sws) case <-stopCh: mustStop = true } @@ -233,6 +225,7 @@ func equalLabel(a, b *prompbmarshal.Label) bool { // // This function returns after closing stopCh. func runScrapeWorkers(sws []ScrapeWork, pushData func(wr *prompbmarshal.WriteRequest), stopCh <-chan struct{}) { + tsmGlobal.RegisterAll(sws) var wg sync.WaitGroup for i := range sws { cfg := &sws[i] @@ -248,4 +241,5 @@ func runScrapeWorkers(sws []ScrapeWork, pushData func(wr *prompbmarshal.WriteReq }() } wg.Wait() + tsmGlobal.UnregisterAll(sws) }