Skip to content
This repository was archived by the owner on Aug 13, 2019. It is now read-only.

Commit d5b3f07

Browse files
YaoZengzengbrian-brazil
authored andcommitted
add metric for tsdb size retention bytes (#667)
Signed-off-by: YaoZengzeng <[email protected]>
1 parent 5657860 commit d5b3f07

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

db.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ type dbMetrics struct {
157157
startTime prometheus.GaugeFunc
158158
tombCleanTimer prometheus.Histogram
159159
blocksBytes prometheus.Gauge
160+
maxBytes prometheus.Gauge
160161
sizeRetentionCount prometheus.Counter
161162
}
162163

@@ -227,6 +228,10 @@ func newDBMetrics(db *DB, r prometheus.Registerer) *dbMetrics {
227228
Name: "prometheus_tsdb_storage_blocks_bytes",
228229
Help: "The number of bytes that are currently used for local storage by all blocks.",
229230
})
231+
m.maxBytes = prometheus.NewGauge(prometheus.GaugeOpts{
232+
Name: "prometheus_tsdb_retention_limit_bytes",
233+
Help: "Max number of bytes to be retained in the tsdb blocks, configured 0 means disabled",
234+
})
230235
m.sizeRetentionCount = prometheus.NewCounter(prometheus.CounterOpts{
231236
Name: "prometheus_tsdb_size_retentions_total",
232237
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 {
244249
m.startTime,
245250
m.tombCleanTimer,
246251
m.blocksBytes,
252+
m.maxBytes,
247253
m.sizeRetentionCount,
248254
)
249255
}
@@ -454,6 +460,12 @@ func Open(dir string, l log.Logger, r prometheus.Registerer, opts *Options) (db
454460
}
455461
db.metrics = newDBMetrics(db, r)
456462

463+
maxBytes := opts.MaxBytes
464+
if maxBytes < 0 {
465+
maxBytes = 0
466+
}
467+
db.metrics.maxBytes.Set(float64(maxBytes))
468+
457469
if !opts.NoLockfile {
458470
absdir, err := filepath.Abs(dir)
459471
if err != nil {

db_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,30 @@ func TestSizeRetention(t *testing.T) {
11381138

11391139
}
11401140

1141+
func TestSizeRetentionMetric(t *testing.T) {
1142+
cases := []struct {
1143+
maxBytes int64
1144+
expMaxBytes int64
1145+
}{
1146+
{maxBytes: 1000, expMaxBytes: 1000},
1147+
{maxBytes: 0, expMaxBytes: 0},
1148+
{maxBytes: -1000, expMaxBytes: 0},
1149+
}
1150+
1151+
for _, c := range cases {
1152+
db, delete := openTestDB(t, &Options{
1153+
BlockRanges: []int64{100},
1154+
MaxBytes: c.maxBytes,
1155+
})
1156+
1157+
actMaxBytes := int64(prom_testutil.ToFloat64(db.metrics.maxBytes))
1158+
testutil.Equals(t, actMaxBytes, c.expMaxBytes, "metric retention limit bytes mismatch")
1159+
1160+
testutil.Ok(t, db.Close())
1161+
delete()
1162+
}
1163+
}
1164+
11411165
func TestNotMatcherSelectsLabelsUnsetSeries(t *testing.T) {
11421166
db, delete := openTestDB(t, nil)
11431167
defer func() {

0 commit comments

Comments
 (0)