Skip to content

Commit

Permalink
Merge pull request prometheus#13919 from GiedriusS/dont_forget_to_unr…
Browse files Browse the repository at this point in the history
…egister

tsdb/wlog: unregister metrics on WL close
  • Loading branch information
ArthurSens authored Apr 18, 2024
2 parents 9faf105 + bdf4907 commit b5b5e1e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
22 changes: 21 additions & 1 deletion tsdb/wlog/wlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,28 @@ type wlMetrics struct {
currentSegment prometheus.Gauge
writesFailed prometheus.Counter
walFileSize prometheus.GaugeFunc

r prometheus.Registerer
}

func (w *wlMetrics) Unregister() {
if w.r == nil {
return
}
w.r.Unregister(w.fsyncDuration)
w.r.Unregister(w.pageFlushes)
w.r.Unregister(w.pageCompletions)
w.r.Unregister(w.truncateFail)
w.r.Unregister(w.truncateTotal)
w.r.Unregister(w.currentSegment)
w.r.Unregister(w.writesFailed)
w.r.Unregister(w.walFileSize)
}

func newWLMetrics(w *WL, r prometheus.Registerer) *wlMetrics {
m := &wlMetrics{}
m := &wlMetrics{
r: r,
}

m.fsyncDuration = prometheus.NewSummary(prometheus.SummaryOpts{
Name: "fsync_duration_seconds",
Expand Down Expand Up @@ -877,6 +895,8 @@ func (w *WL) Close() (err error) {
if err := w.segment.Close(); err != nil {
level.Error(w.logger).Log("msg", "close previous segment", "err", err)
}

w.metrics.Unregister()
w.closed = true
return nil
}
Expand Down
12 changes: 12 additions & 0 deletions tsdb/wlog/wlog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"path/filepath"
"testing"

"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
client_testutil "github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
Expand Down Expand Up @@ -561,3 +563,13 @@ func BenchmarkWAL_Log(b *testing.B) {
})
}
}

func TestUnregisterMetrics(t *testing.T) {
reg := prometheus.NewRegistry()

for i := 0; i < 2; i++ {
wl, err := New(log.NewNopLogger(), reg, t.TempDir(), CompressionNone)
require.NoError(t, err)
require.NoError(t, wl.Close())
}
}

0 comments on commit b5b5e1e

Please sign in to comment.