fix: Flush reason counter not initialized to 0, breaks PromQL functions (#21772)

pull/21776/head
George Robinson 2 days ago committed by GitHub
parent 08820d7fa3
commit 61b377db50
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 7
      pkg/dataobj/consumer/flush.go
  2. 12
      pkg/dataobj/consumer/flush_test.go

@ -13,9 +13,9 @@ import (
)
const (
flushReasonMaxAge = "max_age"
flushReasonBuilderFull = "builder_full"
flushReasonIdle = "idle"
flushReasonMaxAge = "max_age"
)
// A sorter allows mocking of [logsobj.Sorter] in tests.
@ -80,6 +80,11 @@ func newFlusher(sorter sorter, uploader uploader, logger log.Logger, r prometheu
NativeHistogramMinResetDuration: 0,
}),
}
// Initialize each counter to 0, otherwise neither the rate nor increase
// PromQL functions detect increases from 0 to 1.
f.flushes.WithLabelValues(flushReasonBuilderFull).Add(0)
f.flushes.WithLabelValues(flushReasonIdle).Add(0)
f.flushes.WithLabelValues(flushReasonMaxAge).Add(0)
f.flushFunc = f.flush
return f
}

@ -39,7 +39,7 @@ func TestFlusher_Flush(t *testing.T) {
}))
f := newFlusher(testSorter, testUploader, log.NewNopLogger(), reg)
// Flush the builder we created earlier.
objectPath, err := f.Flush(testCtx, testBuilder, "test_sync")
objectPath, err := f.Flush(testCtx, testBuilder, flushReasonBuilderFull)
require.NoError(t, err)
require.Equal(t, "object_001", objectPath)
// Check that the dataobj was flushed and uploaded.
@ -47,7 +47,9 @@ func TestFlusher_Flush(t *testing.T) {
require.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(`
# HELP loki_dataobj_consumer_flushes_total Total number of flushes.
# TYPE loki_dataobj_consumer_flushes_total counter
loki_dataobj_consumer_flushes_total{reason="test_sync"} 1
loki_dataobj_consumer_flushes_total{reason="builder_full"} 1
loki_dataobj_consumer_flushes_total{reason="idle"} 0
loki_dataobj_consumer_flushes_total{reason="max_age"} 0
# HELP loki_dataobj_consumer_flush_failures_total Total number of failed flushes.
# TYPE loki_dataobj_consumer_flush_failures_total counter
loki_dataobj_consumer_flush_failures_total 0
@ -66,13 +68,15 @@ func TestFlusher_Flush(t *testing.T) {
return "", errors.New("mock error")
}
// Flush the builder we created earlier.
objectPath, err := f.Flush(testCtx, testBuilder, "test_sync")
objectPath, err := f.Flush(testCtx, testBuilder, flushReasonBuilderFull)
require.EqualError(t, err, "mock error")
require.Equal(t, "", objectPath)
require.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(`
# HELP loki_dataobj_consumer_flushes_total Total number of flushes.
# TYPE loki_dataobj_consumer_flushes_total counter
loki_dataobj_consumer_flushes_total{reason="test_sync"} 1
loki_dataobj_consumer_flushes_total{reason="builder_full"} 1
loki_dataobj_consumer_flushes_total{reason="idle"} 0
loki_dataobj_consumer_flushes_total{reason="max_age"} 0
# HELP loki_dataobj_consumer_flush_failures_total Total number of failed flushes.
# TYPE loki_dataobj_consumer_flush_failures_total counter
loki_dataobj_consumer_flush_failures_total 1

Loading…
Cancel
Save