From 187fd89c709b0aaeba2e75c996208a07d2f45d3f Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Wed, 11 Mar 2020 03:19:56 +0200 Subject: [PATCH] lib/promscrape: consistently update `/targets` page after SIGHUP --- lib/promscrape/scraper.go | 8 ++++++++ lib/promscrape/targetstatus.go | 26 ++++++++++++++++++++------ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/lib/promscrape/scraper.go b/lib/promscrape/scraper.go index c69e92286..0b872eb2e 100644 --- a/lib/promscrape/scraper.go +++ b/lib/promscrape/scraper.go @@ -64,6 +64,8 @@ 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 { @@ -98,9 +100,13 @@ 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 } @@ -160,7 +166,9 @@ 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 } diff --git a/lib/promscrape/targetstatus.go b/lib/promscrape/targetstatus.go index 74e382633..390ab355e 100644 --- a/lib/promscrape/targetstatus.go +++ b/lib/promscrape/targetstatus.go @@ -35,6 +35,25 @@ func (tsm *targetStatusMap) Reset() { tsm.mu.Unlock() } +func (tsm *targetStatusMap) RegisterAll(sws []ScrapeWork) { + tsm.mu.Lock() + for i := range sws { + sw := &sws[i] + tsm.m[sw.ScrapeURL] = targetStatus{ + sw: sw, + } + } + tsm.mu.Unlock() +} + +func (tsm *targetStatusMap) UnregisterAll(sws []ScrapeWork) { + tsm.mu.Lock() + for i := range sws { + delete(tsm.m, sws[i].ScrapeURL) + } + tsm.mu.Unlock() +} + func (tsm *targetStatusMap) Update(sw *ScrapeWork, up bool, scrapeTime, scrapeDuration int64, err error) { tsm.mu.Lock() tsm.m[sw.ScrapeURL] = targetStatus{ @@ -50,12 +69,7 @@ func (tsm *targetStatusMap) Update(sw *ScrapeWork, up bool, scrapeTime, scrapeDu func (tsm *targetStatusMap) WriteHumanReadable(w io.Writer) { byJob := make(map[string][]targetStatus) tsm.mu.Lock() - for k, st := range tsm.m { - if st.getDurationFromLastScrape() > 10*st.sw.ScrapeInterval { - // Remove obsolete targets - delete(tsm.m, k) - continue - } + for _, st := range tsm.m { job := "" label := promrelabel.GetLabelByName(st.sw.Labels, "job") if label != nil {