chore: add test to make sure staleness tracking is off when ref==0

Signed-off-by: Ridwan Sharif <ridwanmsharif@google.com>
This commit is contained in:
Ridwan Sharif 2026-04-15 21:39:39 +00:00 committed by bwplotka
parent 4485560a60
commit abf3726f8a
2 changed files with 19 additions and 2 deletions

View file

@ -1779,7 +1779,7 @@ loop:
}
if err == nil {
if (parsedTimestamp == nil || sl.trackTimestampsStaleness) && ce != nil {
if (parsedTimestamp == nil || sl.trackTimestampsStaleness) && ce != nil && ce.ref != 0 {
sl.cache.trackStaleness(ce.ref, ce)
}
}
@ -1798,7 +1798,7 @@ loop:
// it in the scrape cache because we don't need to emit StaleNaNs for it when it disappears.
if !seriesCached && sampleAdded {
ce = sl.cache.addRef(met, ref, lset, hash)
if ce != nil && (parsedTimestamp == nil || sl.trackTimestampsStaleness) {
if ce != nil && ce.ref != 0 && (parsedTimestamp == nil || sl.trackTimestampsStaleness) {
// Bypass staleness logic if there is an explicit timestamp.
// But make sure we only do this if we have a cache entry (ce) for our series.
sl.cache.trackStaleness(ref, ce)

View file

@ -1971,6 +1971,23 @@ test_metric 15
require.Equal(t, 1, total)
require.Equal(t, 1, added)
require.Equal(t, 0, seriesAdded)
// Third Scrape: Counter is missing.
// Since it was never successfully appended (anchored on first scrape, failed with OOO on second),
// it should NOT have been tracked for staleness. Thus, no StaleNaN should be appended.
ts3 := ts2.Add(time.Second)
scrapeC := []byte(`# TYPE test_metric counter
# EOF
`)
app = sl.appender()
_, _, _, err = app.append(scrapeC, "application/openmetrics-text", ts3)
require.NoError(t, err)
require.NoError(t, app.Commit())
// Verify that still no samples are appended (specifically no stale marker).
got = appTest.ResultSamples()
require.Empty(t, got, "Expected no samples (specifically no stale markers) because the series was never tracked for staleness")
}
func requireSampleHist(t *testing.T, s teststorage.Sample, name, expectedHist string, ts, st int64, isNaN bool) {