From b0c98e01c8197b8ba09992100fa297d34c0a9606 Mon Sep 17 00:00:00 2001 From: Julien Pivotto Date: Wed, 20 Oct 2021 23:44:45 +0200 Subject: [PATCH] Include scrape labels in the hash (#9551) Signed-off-by: Julien Pivotto --- scrape/target.go | 11 +---------- scrape/target_test.go | 26 -------------------------- 2 files changed, 1 insertion(+), 36 deletions(-) diff --git a/scrape/target.go b/scrape/target.go index ada1bcdc5..35d691751 100644 --- a/scrape/target.go +++ b/scrape/target.go @@ -144,17 +144,8 @@ func (t *Target) SetMetadataStore(s MetricMetadataStore) { func (t *Target) hash() uint64 { h := fnv.New64a() - // We must build a label set without the scrape interval and timeout - // labels because those aren't defining attributes of a target - // and can be changed without qualifying its parent as a new target, - // therefore they should not effect its unique hash. - l := t.labels.Map() - delete(l, model.ScrapeIntervalLabel) - delete(l, model.ScrapeTimeoutLabel) - lset := labels.FromMap(l) - //nolint: errcheck - h.Write([]byte(fmt.Sprintf("%016d", lset.Hash()))) + h.Write([]byte(fmt.Sprintf("%016d", t.labels.Hash()))) //nolint: errcheck h.Write([]byte(t.URL().String())) diff --git a/scrape/target_test.go b/scrape/target_test.go index a578d2760..542326d7c 100644 --- a/scrape/target_test.go +++ b/scrape/target_test.go @@ -382,29 +382,3 @@ func TestTargetsFromGroup(t *testing.T) { t.Fatalf("Expected error %s, got %s", expectedError, failures[0]) } } - -func TestTargetHash(t *testing.T) { - target1 := &Target{ - labels: labels.Labels{ - {Name: model.AddressLabel, Value: "localhost"}, - {Name: model.SchemeLabel, Value: "http"}, - {Name: model.MetricsPathLabel, Value: "/metrics"}, - {Name: model.ScrapeIntervalLabel, Value: "15s"}, - {Name: model.ScrapeTimeoutLabel, Value: "500ms"}, - }, - } - hash1 := target1.hash() - - target2 := &Target{ - labels: labels.Labels{ - {Name: model.AddressLabel, Value: "localhost"}, - {Name: model.SchemeLabel, Value: "http"}, - {Name: model.MetricsPathLabel, Value: "/metrics"}, - {Name: model.ScrapeIntervalLabel, Value: "14s"}, - {Name: model.ScrapeTimeoutLabel, Value: "600ms"}, - }, - } - hash2 := target2.hash() - - require.Equal(t, hash1, hash2, "Scrape interval and duration labels should not effect hash.") -}