From 90583c890621ee8c5598adca37c5b9308e67cd6c Mon Sep 17 00:00:00 2001 From: Julien Pivotto Date: Fri, 1 Jul 2022 14:28:56 +0200 Subject: [PATCH] TestScrapeLoopCache: Display content of the appender (#10937) This should help identifying windows tests flakiness. Signed-off-by: Julien Pivotto --- scrape/helpers_test.go | 16 ++++++++++++++++ scrape/scrape_test.go | 20 ++++++++++---------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/scrape/helpers_test.go b/scrape/helpers_test.go index 686cf9632..bead84574 100644 --- a/scrape/helpers_test.go +++ b/scrape/helpers_test.go @@ -15,7 +15,9 @@ package scrape import ( "context" + "fmt" "math/rand" + "strings" "github.com/prometheus/prometheus/model/exemplar" "github.com/prometheus/prometheus/model/labels" @@ -106,3 +108,17 @@ func (a *collectResultAppender) Rollback() error { } return a.next.Rollback() } + +func (a *collectResultAppender) String() string { + var sb strings.Builder + for _, s := range a.result { + sb.WriteString(fmt.Sprintf("committed: %s %f %d\n", s.metric, s.v, s.t)) + } + for _, s := range a.pendingResult { + sb.WriteString(fmt.Sprintf("pending: %s %f %d\n", s.metric, s.v, s.t)) + } + for _, s := range a.rolledbackResult { + sb.WriteString(fmt.Sprintf("rolledback: %s %f %d\n", s.metric, s.v, s.t)) + } + return sb.String() +} diff --git a/scrape/scrape_test.go b/scrape/scrape_test.go index d887a0c9d..9d0e16df3 100644 --- a/scrape/scrape_test.go +++ b/scrape/scrape_test.go @@ -1125,7 +1125,7 @@ func TestScrapeLoopRunCreatesStaleMarkersOnFailedScrape(t *testing.T) { // 1 successfully scraped sample, 1 stale marker after first fail, 5 report samples for // each scrape successful or not. - require.Equal(t, 27, len(appender.result), "Appended samples not as expected") + require.Equal(t, 27, len(appender.result), "Appended samples not as expected:\n%s", appender) require.Equal(t, 42.0, appender.result[0].v, "Appended first sample not as expected") require.True(t, value.IsStaleNaN(appender.result[6].v), "Appended second sample not as expected. Wanted: stale NaN Got: %x", math.Float64bits(appender.result[6].v)) @@ -1189,7 +1189,7 @@ func TestScrapeLoopRunCreatesStaleMarkersOnParseFailure(t *testing.T) { // 1 successfully scraped sample, 1 stale marker after first fail, 5 report samples for // each scrape successful or not. - require.Equal(t, 17, len(appender.result), "Appended samples not as expected") + require.Equal(t, 17, len(appender.result), "Appended samples not as expected:\n%s", appender) require.Equal(t, 42.0, appender.result[0].v, "Appended first sample not as expected") require.True(t, value.IsStaleNaN(appender.result[6].v), "Appended second sample not as expected. Wanted: stale NaN Got: %x", math.Float64bits(appender.result[6].v)) @@ -1272,7 +1272,7 @@ func TestScrapeLoopCache(t *testing.T) { // 1 successfully scraped sample, 1 stale marker after first fail, 5 report samples for // each scrape successful or not. - require.Equal(t, 26, len(appender.result), "Appended samples not as expected") + require.Equal(t, 26, len(appender.result), "Appended samples not as expected:\n%s", appender) } func TestScrapeLoopCacheMemoryExhaustionProtection(t *testing.T) { @@ -1642,7 +1642,7 @@ func TestScrapeLoopAppendSampleLimit(t *testing.T) { v: 1, }, } - require.Equal(t, want, resApp.rolledbackResult, "Appended samples not as expected") + require.Equal(t, want, resApp.rolledbackResult, "Appended samples not as expected:\n%s", appender) now = time.Now() slApp = sl.appender(context.Background()) @@ -1707,7 +1707,7 @@ func TestScrapeLoop_ChangingMetricString(t *testing.T) { v: 2, }, } - require.Equal(t, want, capp.result, "Appended samples not as expected") + require.Equal(t, want, capp.result, "Appended samples not as expected:\n%s", appender) } func TestScrapeLoopAppendStaleness(t *testing.T) { @@ -1759,7 +1759,7 @@ func TestScrapeLoopAppendStaleness(t *testing.T) { v: 42, }, } - require.Equal(t, want, app.result, "Appended samples not as expected") + require.Equal(t, want, app.result, "Appended samples not as expected:\n%s", appender) } func TestScrapeLoopAppendNoStalenessIfTimestamp(t *testing.T) { @@ -1800,7 +1800,7 @@ func TestScrapeLoopAppendNoStalenessIfTimestamp(t *testing.T) { v: 1, }, } - require.Equal(t, want, app.result, "Appended samples not as expected") + require.Equal(t, want, app.result, "Appended samples not as expected:\n%s", appender) } func TestScrapeLoopAppendExemplar(t *testing.T) { @@ -2108,7 +2108,7 @@ func TestScrapeLoopAppendGracefullyIfAmendOrOutOfOrderOrOutOfBounds(t *testing.T v: 1, }, } - require.Equal(t, want, app.result, "Appended samples not as expected") + require.Equal(t, want, app.result, "Appended samples not as expected:\n%s", appender) require.Equal(t, 4, total) require.Equal(t, 4, added) require.Equal(t, 1, seriesAdded) @@ -2410,7 +2410,7 @@ func TestScrapeLoop_RespectTimestamps(t *testing.T) { v: 1, }, } - require.Equal(t, want, capp.result, "Appended samples not as expected") + require.Equal(t, want, capp.result, "Appended samples not as expected:\n%s", appender) } func TestScrapeLoop_DiscardTimestamps(t *testing.T) { @@ -2451,7 +2451,7 @@ func TestScrapeLoop_DiscardTimestamps(t *testing.T) { v: 1, }, } - require.Equal(t, want, capp.result, "Appended samples not as expected") + require.Equal(t, want, capp.result, "Appended samples not as expected:\n%s", appender) } func TestScrapeLoopDiscardDuplicateLabels(t *testing.T) {