From e65e2e0dac2ab726dc8db600a3523ea37dcebf4d Mon Sep 17 00:00:00 2001 From: Ganesh Vernekar <15064823+codesome@users.noreply.github.com> Date: Sun, 5 Jul 2020 10:11:42 +0530 Subject: [PATCH] Fix panic from db metrics (#7501) Signed-off-by: Ganesh Vernekar --- tsdb/db.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/tsdb/db.go b/tsdb/db.go index 8c8b049d5..2b0525ab6 100644 --- a/tsdb/db.go +++ b/tsdb/db.go @@ -569,13 +569,6 @@ func open(dir string, l log.Logger, r prometheus.Registerer, opts *Options, rngs autoCompact: true, chunkPool: chunkenc.NewPool(), } - 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) @@ -617,6 +610,14 @@ func open(dir string, l log.Logger, r prometheus.Registerer, opts *Options, rngs return nil, err } + // Register metrics after assigning the head block. + db.metrics = newDBMetrics(db, r) + maxBytes := opts.MaxBytes + if maxBytes < 0 { + maxBytes = 0 + } + db.metrics.maxBytes.Set(float64(maxBytes)) + if err := db.reload(); err != nil { return nil, err }