From e32ad9e9237fab875e29c9d85d76bc86c06212d6 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Mon, 1 Mar 2021 12:29:09 +0200 Subject: [PATCH] lib/promscrape: use target arg in ScrapeWork cache --- lib/promscrape/config.go | 6 ++++-- lib/promscrape/config_test.go | 10 ++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/promscrape/config.go b/lib/promscrape/config.go index dc32daf2a..1d08a0441 100644 --- a/lib/promscrape/config.go +++ b/lib/promscrape/config.go @@ -728,7 +728,7 @@ func (stc *StaticConfig) appendScrapeWork(dst []*ScrapeWork, swc *scrapeWorkConf func (swc *scrapeWorkConfig) getScrapeWork(target string, extraLabels, metaLabels map[string]string) (*ScrapeWork, error) { bb := scrapeWorkKeyBufPool.Get() defer scrapeWorkKeyBufPool.Put(bb) - bb.B = appendScrapeWorkKey(bb.B[:0], extraLabels, metaLabels) + bb.B = appendScrapeWorkKey(bb.B[:0], target, extraLabels, metaLabels) keyStrUnsafe := bytesutil.ToUnsafeString(bb.B) if needSkipScrapeWork(keyStrUnsafe) { return nil, nil @@ -745,7 +745,9 @@ func (swc *scrapeWorkConfig) getScrapeWork(target string, extraLabels, metaLabel var scrapeWorkKeyBufPool bytesutil.ByteBufferPool -func appendScrapeWorkKey(dst []byte, extraLabels, metaLabels map[string]string) []byte { +func appendScrapeWorkKey(dst []byte, target string, extraLabels, metaLabels map[string]string) []byte { + dst = append(dst, target...) + dst = append(dst, ',') dst = appendSortedKeyValuePairs(dst, extraLabels) dst = appendSortedKeyValuePairs(dst, metaLabels) return dst diff --git a/lib/promscrape/config_test.go b/lib/promscrape/config_test.go index f2273ccc4..6ca1cf993 100644 --- a/lib/promscrape/config_test.go +++ b/lib/promscrape/config_test.go @@ -4,6 +4,7 @@ import ( "crypto/tls" "fmt" "reflect" + "strconv" "testing" "time" @@ -444,6 +445,11 @@ func resetNonEssentialFields(sws []*ScrapeWork) { } } +// String returns human-readable representation for sw. +func (sw *ScrapeWork) String() string { + return strconv.Quote(sw.key()) +} + func TestGetFileSDScrapeWorkSuccess(t *testing.T) { f := func(data string, expectedSws []*ScrapeWork) { t.Helper() @@ -463,7 +469,7 @@ func TestGetFileSDScrapeWorkSuccess(t *testing.T) { } } if !reflect.DeepEqual(sws, expectedSws) { - t.Fatalf("unexpected scrapeWork; got\n%v\nwant\n%v", sws, expectedSws) + t.Fatalf("unexpected scrapeWork; got\n%+v\nwant\n%+v", sws, expectedSws) } } f(` @@ -608,7 +614,7 @@ func TestGetStaticScrapeWorkSuccess(t *testing.T) { } resetNonEssentialFields(sws) if !reflect.DeepEqual(sws, expectedSws) { - t.Fatalf("unexpected scrapeWork; got\n%v\nwant\n%v", sws, expectedSws) + t.Fatalf("unexpected scrapeWork; got\n%+v\nwant\n%+v", sws, expectedSws) } } f(``, nil)