From db7dc28fc75d28b7dfe6f56037b75161d1bfec3c Mon Sep 17 00:00:00 2001 From: matst80 Date: Sat, 9 Nov 2024 12:07:51 +0100 Subject: [PATCH] add more metrics --- grain-pool.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/grain-pool.go b/grain-pool.go index 061d9a3..dfbdeba 100644 --- a/grain-pool.go +++ b/grain-pool.go @@ -5,6 +5,20 @@ import ( "fmt" "log" "time" + + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" +) + +var ( + poolGrains = promauto.NewGauge(prometheus.GaugeOpts{ + Name: "cart_grains_in_pool", + Help: "The total number of grains in the pool", + }) + poolSize = promauto.NewGauge(prometheus.GaugeOpts{ + Name: "cart_pool_size", + Help: "The total number of mutations", + }) ) type GrainPool interface { @@ -18,6 +32,7 @@ type Ttl struct { } type GrainLocalPool struct { + gauage prometheus.Gauge grains map[CartId]*CartGrain expiry []Ttl spawn func(id CartId) (*CartGrain, error) @@ -26,13 +41,19 @@ type GrainLocalPool struct { } func NewGrainLocalPool(size int, ttl time.Duration, spawn func(id CartId) (*CartGrain, error)) *GrainLocalPool { + gauage := promauto.NewGauge(prometheus.GaugeOpts{ + Name: "cart_grain_pool_usage", + Help: "The current usage of the grain pool", + }) ret := &GrainLocalPool{ + gauage: gauage, spawn: spawn, grains: make(map[CartId]*CartGrain), expiry: make([]Ttl, 0), Ttl: ttl, PoolSize: size, } + cartPurge := time.NewTicker(time.Minute) go func() { <-cartPurge.C @@ -95,6 +116,11 @@ func (p *GrainLocalPool) GetGrain(id CartId) (*CartGrain, error) { p.grains[id] = grain } + l := float64(len(p.grains)) + ps := float64(p.PoolSize) + p.gauage.Set(l / ps) + poolGrains.Set(l) + poolSize.Set(ps) return grain, err }