lib/promscrape: remove possible races when registering and de-registering scrape workers for /targets page

This commit is contained in:
Aliaksandr Valialkin 2020-03-11 16:30:14 +02:00
parent b46af9678e
commit 375d5483fa

View file

@ -64,8 +64,6 @@ func runScraper(configFile string, pushData func(wr *prompbmarshal.WriteRequest)
if err != nil { if err != nil {
logger.Fatalf("cannot parse `file_sd_config` from %q: %s", configFile, err) logger.Fatalf("cannot parse `file_sd_config` from %q: %s", configFile, err)
} }
tsmGlobal.RegisterAll(swsStatic)
tsmGlobal.RegisterAll(swsFileSD)
mustStop := false mustStop := false
for !mustStop { for !mustStop {
@ -100,13 +98,9 @@ func runScraper(configFile string, pushData func(wr *prompbmarshal.WriteRequest)
if err != nil { if err != nil {
logger.Errorf("cannot parse `file_sd_config` from %q: %s; continuing with the previous config", configFile, err) 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 cfg = cfgNew
swsStatic = swsStaticNew swsStatic = swsStaticNew
swsFileSD = swsFileSDNew swsFileSD = swsFileSDNew
tsmGlobal.RegisterAll(swsStatic)
tsmGlobal.RegisterAll(swsFileSD)
case <-globalStopCh: case <-globalStopCh:
mustStop = true mustStop = true
} }
@ -166,9 +160,7 @@ func runFileSDScrapers(sws []ScrapeWork, cfg *Config, pushData func(wr *prompbma
goto waitForChans goto waitForChans
} }
logger.Infof("restarting scrapers for changed `file_sd_config` targets") logger.Infof("restarting scrapers for changed `file_sd_config` targets")
tsmGlobal.UnregisterAll(sws)
sws = swsNew sws = swsNew
tsmGlobal.RegisterAll(sws)
case <-stopCh: case <-stopCh:
mustStop = true mustStop = true
} }
@ -233,6 +225,7 @@ func equalLabel(a, b *prompbmarshal.Label) bool {
// //
// This function returns after closing stopCh. // This function returns after closing stopCh.
func runScrapeWorkers(sws []ScrapeWork, pushData func(wr *prompbmarshal.WriteRequest), stopCh <-chan struct{}) { func runScrapeWorkers(sws []ScrapeWork, pushData func(wr *prompbmarshal.WriteRequest), stopCh <-chan struct{}) {
tsmGlobal.RegisterAll(sws)
var wg sync.WaitGroup var wg sync.WaitGroup
for i := range sws { for i := range sws {
cfg := &sws[i] cfg := &sws[i]
@ -248,4 +241,5 @@ func runScrapeWorkers(sws []ScrapeWork, pushData func(wr *prompbmarshal.WriteReq
}() }()
} }
wg.Wait() wg.Wait()
tsmGlobal.UnregisterAll(sws)
} }