From 0fe34f6d788be5ede6bc0d705b80343e12298b9c Mon Sep 17 00:00:00 2001 From: Julien Pivotto Date: Thu, 9 Nov 2023 10:18:00 -0600 Subject: [PATCH] Follow-up to #13060: Add test to ensure staleness tracking This commit introduces an additional test in `scrape_test.go` to verify staleness tracking when `trackTimestampStaleness` is enabled. The new `TestScrapeLoopAppendStalenessIfTrackTimestampStaleness` function asserts that the scrape loop correctly appends staleness markers when necessary, reflecting the expected behavior with the feature flag turned on. The previous tests were only testing end of scrape staleness. Signed-off-by: Julien Pivotto --- scrape/scrape_test.go | 51 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/scrape/scrape_test.go b/scrape/scrape_test.go index ccd651f49..a2e0d00c6 100644 --- a/scrape/scrape_test.go +++ b/scrape/scrape_test.go @@ -2038,6 +2038,57 @@ func TestScrapeLoopAppendNoStalenessIfTimestamp(t *testing.T) { require.Equal(t, want, app.resultFloats, "Appended samples not as expected:\n%s", appender) } +func TestScrapeLoopAppendStalenessIfTrackTimestampStaleness(t *testing.T) { + app := &collectResultAppender{} + sl := newScrapeLoop(context.Background(), + nil, nil, nil, + nopMutator, + nopMutator, + func(ctx context.Context) storage.Appender { return app }, + nil, + 0, + true, + true, + 0, 0, + nil, + 0, + 0, + false, + false, + false, + nil, + false, + newTestScrapeMetrics(t), + ) + + now := time.Now() + slApp := sl.appender(context.Background()) + _, _, _, err := sl.append(slApp, []byte("metric_a 1 1000\n"), "", now) + require.NoError(t, err) + require.NoError(t, slApp.Commit()) + + slApp = sl.appender(context.Background()) + _, _, _, err = sl.append(slApp, []byte(""), "", now.Add(time.Second)) + require.NoError(t, err) + require.NoError(t, slApp.Commit()) + + // DeepEqual will report NaNs as being different, so replace with a different value. + app.resultFloats[1].f = 42 + want := []floatSample{ + { + metric: labels.FromStrings(model.MetricNameLabel, "metric_a"), + t: 1000, + f: 1, + }, + { + metric: labels.FromStrings(model.MetricNameLabel, "metric_a"), + t: timestamp.FromTime(now.Add(time.Second)), + f: 42, + }, + } + require.Equal(t, want, app.resultFloats, "Appended samples not as expected:\n%s", appender) +} + func TestScrapeLoopAppendExemplar(t *testing.T) { tests := []struct { title string