From e1774b6f83a7554c91fa52bc420982dd0ac5a1e1 Mon Sep 17 00:00:00 2001 From: Julien Pivotto Date: Fri, 14 May 2021 22:32:15 +0200 Subject: [PATCH] Fix the computation of prometheus_sd_discovered_targets prometheus_sd_discovered_targets is wrongly calculated when there are multiple SD configurations in place. One discovery manager can have multiple groups coming from multiple service discoveries. When multiple service discovery configs are used, we do not compute the metric correctly, and instead just set the metric to one of the service discoveries. Signed-off-by: Julien Pivotto --- discovery/manager.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/discovery/manager.go b/discovery/manager.go index e50d2ee1a2..a4208532ee 100644 --- a/discovery/manager.go +++ b/discovery/manager.go @@ -279,15 +279,17 @@ func (m *Manager) allGroups() map[string][]*targetgroup.Group { defer m.mtx.RUnlock() tSets := map[string][]*targetgroup.Group{} + n := map[string]int{} for pkey, tsets := range m.targets { - var n int for _, tg := range tsets { // Even if the target group 'tg' is empty we still need to send it to the 'Scrape manager' // to signal that it needs to stop all scrape loops for this target set. tSets[pkey.setName] = append(tSets[pkey.setName], tg) - n += len(tg.Targets) + n[pkey.setName] += len(tg.Targets) } - discoveredTargets.WithLabelValues(m.name, pkey.setName).Set(float64(n)) + } + for setName, v := range n { + discoveredTargets.WithLabelValues(m.name, setName).Set(float64(v)) } return tSets }