Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(blooms): improves mempool metrics #13283

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions pkg/util/mempool/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"}),
}
}
6 changes: 4 additions & 2 deletions pkg/util/mempool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()
Expand Down Expand Up @@ -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")
}
Expand Down
Loading