From d5b3f0704379a9eaca33b711aa0097f001817fc2 Mon Sep 17 00:00:00 2001 From: Yao Zengzeng Date: Sat, 27 Jul 2019 16:52:25 +0800 Subject: [PATCH] add metric for tsdb size retention bytes (#667) Signed-off-by: YaoZengzeng --- db.go | 12 ++++++++++++ db_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/db.go b/db.go index aa9ec1785..ec830027c 100644 --- a/db.go +++ b/db.go @@ -157,6 +157,7 @@ type dbMetrics struct { startTime prometheus.GaugeFunc tombCleanTimer prometheus.Histogram blocksBytes prometheus.Gauge + maxBytes prometheus.Gauge sizeRetentionCount prometheus.Counter } @@ -227,6 +228,10 @@ func newDBMetrics(db *DB, r prometheus.Registerer) *dbMetrics { Name: "prometheus_tsdb_storage_blocks_bytes", Help: "The number of bytes that are currently used for local storage by all blocks.", }) + m.maxBytes = prometheus.NewGauge(prometheus.GaugeOpts{ + Name: "prometheus_tsdb_retention_limit_bytes", + Help: "Max number of bytes to be retained in the tsdb blocks, configured 0 means disabled", + }) 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.", @@ -244,6 +249,7 @@ func newDBMetrics(db *DB, r prometheus.Registerer) *dbMetrics { m.startTime, m.tombCleanTimer, m.blocksBytes, + m.maxBytes, m.sizeRetentionCount, ) } @@ -454,6 +460,12 @@ func Open(dir string, l log.Logger, r prometheus.Registerer, opts *Options) (db } db.metrics = newDBMetrics(db, r) + maxBytes := opts.MaxBytes + if maxBytes < 0 { + maxBytes = 0 + } + db.metrics.maxBytes.Set(float64(maxBytes)) + if !opts.NoLockfile { absdir, err := filepath.Abs(dir) if err != nil { diff --git a/db_test.go b/db_test.go index b06e05c78..25fb8a7e2 100644 --- a/db_test.go +++ b/db_test.go @@ -1138,6 +1138,30 @@ func TestSizeRetention(t *testing.T) { } +func TestSizeRetentionMetric(t *testing.T) { + cases := []struct { + maxBytes int64 + expMaxBytes int64 + }{ + {maxBytes: 1000, expMaxBytes: 1000}, + {maxBytes: 0, expMaxBytes: 0}, + {maxBytes: -1000, expMaxBytes: 0}, + } + + for _, c := range cases { + db, delete := openTestDB(t, &Options{ + BlockRanges: []int64{100}, + MaxBytes: c.maxBytes, + }) + + actMaxBytes := int64(prom_testutil.ToFloat64(db.metrics.maxBytes)) + testutil.Equals(t, actMaxBytes, c.expMaxBytes, "metric retention limit bytes mismatch") + + testutil.Ok(t, db.Close()) + delete() + } +} + func TestNotMatcherSelectsLabelsUnsetSeries(t *testing.T) { db, delete := openTestDB(t, nil) defer func() {