From dff1c395f6ed2215974f158e2f8db8a97b7572fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rcio=20Car=C3=B4so?= <18220855+msscaroso@users.noreply.github.com> Date: Tue, 24 Oct 2023 12:34:42 +0100 Subject: [PATCH] Expose --storage.tsdb.retention.time in metric prometheus_tsdb_retention_limit_seconds (#12986) * Expose --storage.tsdb.retention.time in a metric Signed-off-by: Marcio Caroso --------- Signed-off-by: Marcio Caroso --- tsdb/db.go | 7 +++++++ tsdb/db_test.go | 13 +++++++++++++ 2 files changed, 20 insertions(+) diff --git a/tsdb/db.go b/tsdb/db.go index 86cb39b8b..8b3d4d300 100644 --- a/tsdb/db.go +++ b/tsdb/db.go @@ -248,6 +248,7 @@ type dbMetrics struct { tombCleanTimer prometheus.Histogram blocksBytes prometheus.Gauge maxBytes prometheus.Gauge + retentionDuration prometheus.Gauge } func newDBMetrics(db *DB, r prometheus.Registerer) *dbMetrics { @@ -321,6 +322,10 @@ func newDBMetrics(db *DB, r prometheus.Registerer) *dbMetrics { Name: "prometheus_tsdb_retention_limit_bytes", Help: "Max number of bytes to be retained in the tsdb blocks, configured 0 means disabled", }) + m.retentionDuration = prometheus.NewGauge(prometheus.GaugeOpts{ + Name: "prometheus_tsdb_retention_limit_seconds", + Help: "How long to retain samples in storage.", + }) m.sizeRetentionCount = prometheus.NewCounter(prometheus.CounterOpts{ Name: "prometheus_tsdb_size_retentions_total", Help: "The number of times that blocks were deleted because the maximum number of bytes was exceeded.", @@ -341,6 +346,7 @@ func newDBMetrics(db *DB, r prometheus.Registerer) *dbMetrics { m.tombCleanTimer, m.blocksBytes, m.maxBytes, + m.retentionDuration, ) } return m @@ -877,6 +883,7 @@ func open(dir string, l log.Logger, r prometheus.Registerer, opts *Options, rngs maxBytes = 0 } db.metrics.maxBytes.Set(float64(maxBytes)) + db.metrics.retentionDuration.Set((time.Duration(opts.RetentionDuration) * time.Millisecond).Seconds()) if err := db.reload(); err != nil { return nil, err diff --git a/tsdb/db_test.go b/tsdb/db_test.go index 773561c6c..243290c5e 100644 --- a/tsdb/db_test.go +++ b/tsdb/db_test.go @@ -1494,6 +1494,19 @@ func TestTimeRetention(t *testing.T) { require.Equal(t, expBlocks[len(expBlocks)-1].MaxTime, actBlocks[len(actBlocks)-1].meta.MaxTime) } +func TestRetentionDurationMetric(t *testing.T) { + db := openTestDB(t, &Options{ + RetentionDuration: 1000, + }, []int64{100}) + defer func() { + require.NoError(t, db.Close()) + }() + + expRetentionDuration := 1.0 + actRetentionDuration := prom_testutil.ToFloat64(db.metrics.retentionDuration) + require.Equal(t, expRetentionDuration, actRetentionDuration, "metric retention duration mismatch") +} + func TestSizeRetention(t *testing.T) { opts := DefaultOptions() opts.OutOfOrderTimeWindow = 100