add more metrics
All checks were successful
Build and Publish / BuildAndDeploy (push) Successful in 31s

This commit is contained in:
matst80
2024-11-09 12:07:51 +01:00
parent eb71abfa15
commit db7dc28fc7

View File

@@ -5,6 +5,20 @@ import (
"fmt" "fmt"
"log" "log"
"time" "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 { type GrainPool interface {
@@ -18,6 +32,7 @@ type Ttl struct {
} }
type GrainLocalPool struct { type GrainLocalPool struct {
gauage prometheus.Gauge
grains map[CartId]*CartGrain grains map[CartId]*CartGrain
expiry []Ttl expiry []Ttl
spawn func(id CartId) (*CartGrain, error) 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 { 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{ ret := &GrainLocalPool{
gauage: gauage,
spawn: spawn, spawn: spawn,
grains: make(map[CartId]*CartGrain), grains: make(map[CartId]*CartGrain),
expiry: make([]Ttl, 0), expiry: make([]Ttl, 0),
Ttl: ttl, Ttl: ttl,
PoolSize: size, PoolSize: size,
} }
cartPurge := time.NewTicker(time.Minute) cartPurge := time.NewTicker(time.Minute)
go func() { go func() {
<-cartPurge.C <-cartPurge.C
@@ -95,6 +116,11 @@ func (p *GrainLocalPool) GetGrain(id CartId) (*CartGrain, error) {
p.grains[id] = grain 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 return grain, err
} }