From 8dea1f04a5afc28f0e0634aade3bf977ee4f9adf Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Tue, 2 Sep 2025 14:51:19 +0100 Subject: [PATCH] Scrape tests: Better series references `collectResultAppender` on its own will now use the labels hash instead of a random number. This avoids the situation where a series could be added twice under different references. When there is an underlying appender, use the reference it generates. Signed-off-by: Bryan Boreham --- scrape/helpers_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/scrape/helpers_test.go b/scrape/helpers_test.go index abc2011bef..3fc4cdca4a 100644 --- a/scrape/helpers_test.go +++ b/scrape/helpers_test.go @@ -19,7 +19,6 @@ import ( "encoding/binary" "fmt" "math" - "math/rand" "strings" "sync" "testing" @@ -148,10 +147,11 @@ func (a *collectResultAppender) Append(ref storage.SeriesRef, lset labels.Labels f: v, }) - if ref == 0 { - ref = storage.SeriesRef(rand.Uint64()) - } if a.next == nil { + if ref == 0 { + // Use labels hash as a stand-in for unique series reference, to avoid having to track all series. + ref = storage.SeriesRef(lset.Hash()) + } return ref, nil } @@ -195,10 +195,10 @@ func (a *collectResultAppender) UpdateMetadata(ref storage.SeriesRef, l labels.L a.mtx.Lock() defer a.mtx.Unlock() a.pendingMetadata = append(a.pendingMetadata, metadataEntry{metric: l, m: m}) - if ref == 0 { - ref = storage.SeriesRef(rand.Uint64()) - } if a.next == nil { + if ref == 0 { + ref = storage.SeriesRef(l.Hash()) + } return ref, nil }