mirror of
https://github.com/prometheus/prometheus.git
synced 2024-12-25 05:34:05 -08:00
Merge pull request #13919 from GiedriusS/dont_forget_to_unregister
tsdb/wlog: unregister metrics on WL close
This commit is contained in:
commit
b5b5e1e5ae
|
@ -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",
|
||||
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
|
@ -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())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue