diff --git a/pkg/dataobj/consumer/flush.go b/pkg/dataobj/consumer/flush.go index c0fbe32121..328e82a3cc 100644 --- a/pkg/dataobj/consumer/flush.go +++ b/pkg/dataobj/consumer/flush.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 } diff --git a/pkg/dataobj/consumer/flush_test.go b/pkg/dataobj/consumer/flush_test.go index fd3f26fef4..e3960d8f40 100644 --- a/pkg/dataobj/consumer/flush_test.go +++ b/pkg/dataobj/consumer/flush_test.go @@ -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