From 999a5dbdc1fc4dad04d2ff5c40c471a1302d93e2 Mon Sep 17 00:00:00 2001 From: Owen Diehl Date: Fri, 21 Jun 2024 10:38:20 -0700 Subject: [PATCH] fix(blooms): improves mempool metrics adds accesses counter correctly sets available buffers as gauge Signed-off-by: Owen Diehl --- pkg/util/mempool/metrics.go | 17 +++++++++++++++-- pkg/util/mempool/pool.go | 6 ++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/pkg/util/mempool/metrics.go b/pkg/util/mempool/metrics.go index f7d5a52eb0d91..af712ce737c3e 100644 --- a/pkg/util/mempool/metrics.go +++ b/pkg/util/mempool/metrics.go @@ -8,13 +8,19 @@ import ( ) type metrics struct { - availableBuffersPerSlab *prometheus.CounterVec + availableBuffersPerSlab *prometheus.GaugeVec errorsCounter *prometheus.CounterVec + accesses *prometheus.CounterVec } +const ( + opTypeGet = "get" + opTypePut = "put" +) + func newMetrics(r prometheus.Registerer, name string) *metrics { return &metrics{ - availableBuffersPerSlab: promauto.With(r).NewCounterVec(prometheus.CounterOpts{ + availableBuffersPerSlab: promauto.With(r).NewGaugeVec(prometheus.GaugeOpts{ Namespace: constants.Loki, Subsystem: "mempool", Name: "available_buffers_per_slab", @@ -28,5 +34,12 @@ func newMetrics(r prometheus.Registerer, name string) *metrics { Help: "The total amount of errors returned from the pool.", ConstLabels: prometheus.Labels{"pool": name}, }, []string{"slab", "reason"}), + accesses: promauto.With(r).NewCounterVec(prometheus.CounterOpts{ + Namespace: constants.Loki, + Subsystem: "mempool", + Name: "accesses_total", + Help: "The total amount of accesses to the pool.", + ConstLabels: prometheus.Labels{"pool": name}, + }, []string{"slab", "op"}), } } diff --git a/pkg/util/mempool/pool.go b/pkg/util/mempool/pool.go index b42d8d9237677..b6a37b9aaf5a7 100644 --- a/pkg/util/mempool/pool.go +++ b/pkg/util/mempool/pool.go @@ -27,7 +27,7 @@ type slab struct { func newSlab(bufferSize, bufferCount int, m *metrics) *slab { name := humanize.Bytes(uint64(bufferSize)) - m.availableBuffersPerSlab.WithLabelValues(name).Add(0) // initialize metric with value 0 + m.availableBuffersPerSlab.WithLabelValues(name).Set(0) // initialize metric with value 0 return &slab{ size: bufferSize, @@ -44,10 +44,11 @@ func (s *slab) init() { ptr := unsafe.Pointer(unsafe.SliceData(buf)) s.buffer <- ptr } - s.metrics.availableBuffersPerSlab.WithLabelValues(s.name).Add(float64(s.count)) + s.metrics.availableBuffersPerSlab.WithLabelValues(s.name).Set(float64(s.count)) } func (s *slab) get(size int) ([]byte, error) { + s.metrics.accesses.WithLabelValues(s.name, opTypeGet).Inc() s.mtx.Lock() if s.buffer == nil { s.init() @@ -77,6 +78,7 @@ func (s *slab) get(size int) ([]byte, error) { } func (s *slab) put(buf []byte) { + s.metrics.accesses.WithLabelValues(s.name, opTypePut).Inc() if s.buffer == nil { panic("slab is not initialized") }