diff --git a/scrape/scrape.go b/scrape/scrape.go index 5c5a71b00..a22dcb9a3 100644 --- a/scrape/scrape.go +++ b/scrape/scrape.go @@ -301,6 +301,10 @@ func (sp *scrapePool) sync(targets []*Target) { sp.loops[hash] = l go l.run(interval, timeout, nil) + } else { + // Need to keep the most updated labels information + // for displaying it in the Service Discovery web page. + sp.targets[hash].SetDiscoveredLabels(t.DiscoveredLabels()) } } diff --git a/scrape/scrape_test.go b/scrape/scrape_test.go index 61284059b..cedb7d5c8 100644 --- a/scrape/scrape_test.go +++ b/scrape/scrape_test.go @@ -96,6 +96,40 @@ func TestDroppedTargetsList(t *testing.T) { } } +// TestDiscoveredLabelsUpdate checks that DiscoveredLabels are updated +// even when new labels don't affect the target `hash`. +func TestDiscoveredLabelsUpdate(t *testing.T) { + + sp := &scrapePool{} + // These are used when syncing so need this to avoid a panic. + sp.config = &config.ScrapeConfig{ + ScrapeInterval: model.Duration(1), + ScrapeTimeout: model.Duration(1), + } + sp.targets = make(map[uint64]*Target) + t1 := &Target{ + discoveredLabels: labels.Labels{ + labels.Label{ + Name: "label", + Value: "name", + }, + }, + } + sp.targets[t1.hash()] = t1 + + t2 := &Target{ + discoveredLabels: labels.Labels{ + labels.Label{ + Name: "labelNew", + Value: "nameNew", + }, + }, + } + sp.sync([]*Target{t2}) + + testutil.Equals(t, t2.DiscoveredLabels(), sp.targets[t1.hash()].DiscoveredLabels()) +} + type testLoop struct { startFunc func(interval, timeout time.Duration, errc chan<- error) stopFunc func() diff --git a/scrape/target.go b/scrape/target.go index 948d86136..6241d5228 100644 --- a/scrape/target.go +++ b/scrape/target.go @@ -115,6 +115,11 @@ func (t *Target) DiscoveredLabels() labels.Labels { return lset } +// SetDiscoveredLabels sets new DiscoveredLabels +func (t *Target) SetDiscoveredLabels(l labels.Labels) { + t.discoveredLabels = l +} + // URL returns a copy of the target's URL. func (t *Target) URL() *url.URL { params := url.Values{}