Merge pull request #13919 from GiedriusS/dont_forget_to_unregister

tsdb/wlog: unregister metrics on WL close
This commit is contained in:
Arthur Silva Sens 2024-04-18 16:44:03 -03:00 committed by GitHub
commit b5b5e1e5ae
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 33 additions and 1 deletions

View file

@ -228,10 +228,28 @@ type wlMetrics struct {
currentSegment prometheus.Gauge currentSegment prometheus.Gauge
writesFailed prometheus.Counter writesFailed prometheus.Counter
walFileSize prometheus.GaugeFunc 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 { func newWLMetrics(w *WL, r prometheus.Registerer) *wlMetrics {
m := &wlMetrics{} m := &wlMetrics{
r: r,
}
m.fsyncDuration = prometheus.NewSummary(prometheus.SummaryOpts{ m.fsyncDuration = prometheus.NewSummary(prometheus.SummaryOpts{
Name: "fsync_duration_seconds", Name: "fsync_duration_seconds",
@ -877,6 +895,8 @@ func (w *WL) Close() (err error) {
if err := w.segment.Close(); err != nil { if err := w.segment.Close(); err != nil {
level.Error(w.logger).Log("msg", "close previous segment", "err", err) level.Error(w.logger).Log("msg", "close previous segment", "err", err)
} }
w.metrics.Unregister()
w.closed = true w.closed = true
return nil return nil
} }

View file

@ -23,6 +23,8 @@ import (
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
client_testutil "github.com/prometheus/client_golang/prometheus/testutil" client_testutil "github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"go.uber.org/goleak" "go.uber.org/goleak"
@ -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())
}
}