From b7bb278e955d624f26d99b92dc5e29e477b92b08 Mon Sep 17 00:00:00 2001 From: yeya24 Date: Tue, 30 Jul 2019 00:08:54 +0800 Subject: [PATCH] make targets active parallel (#5740) Signed-off-by: yeya24 --- scrape/manager.go | 17 +++++++++++++++-- scrape/scrape.go | 2 +- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/scrape/manager.go b/scrape/manager.go index e0cbb89cf..fed442c18 100644 --- a/scrape/manager.go +++ b/scrape/manager.go @@ -213,7 +213,6 @@ func (m *Manager) TargetsAll() map[string][]*Target { targets := make(map[string][]*Target, len(m.scrapePools)) for tset, sp := range m.scrapePools { targets[tset] = append(sp.ActiveTargets(), sp.DroppedTargets()...) - } return targets } @@ -223,10 +222,24 @@ func (m *Manager) TargetsActive() map[string][]*Target { m.mtxScrape.Lock() defer m.mtxScrape.Unlock() + var ( + wg sync.WaitGroup + mtx sync.Mutex + ) + targets := make(map[string][]*Target, len(m.scrapePools)) + wg.Add(len(m.scrapePools)) for tset, sp := range m.scrapePools { - targets[tset] = sp.ActiveTargets() + // Running in parallel limits the blocking time of scrapePool to scrape + // interval when there's an update from SD. + go func(tset string, sp *scrapePool) { + mtx.Lock() + targets[tset] = sp.ActiveTargets() + mtx.Unlock() + wg.Done() + }(tset, sp) } + wg.Wait() return targets } diff --git a/scrape/scrape.go b/scrape/scrape.go index eca3229c9..0b2b1441d 100644 --- a/scrape/scrape.go +++ b/scrape/scrape.go @@ -66,7 +66,7 @@ var ( targetScrapePools = prometheus.NewCounter( prometheus.CounterOpts{ Name: "prometheus_target_scrape_pools_total", - Help: "Total number of scrape pool creation atttempts.", + Help: "Total number of scrape pool creation attempts.", }, ) targetScrapePoolsFailed = prometheus.NewCounter(