2024-07-04 15:13:19 +03:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2024-10-05 16:11:46 +03:00
|
|
|
goredis "github.com/redis/go-redis/v9"
|
|
|
|
"go.unistack.org/micro/v3/meter"
|
2024-07-04 15:13:19 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
PoolHitsTotal = "pool_hits_total"
|
|
|
|
PoolMissesTotal = "pool_misses_total"
|
|
|
|
PoolTimeoutTotal = "pool_timeout_total"
|
|
|
|
PoolConnTotalCurrent = "pool_conn_total_current"
|
|
|
|
PoolConnIdleCurrent = "pool_conn_idle_current"
|
|
|
|
PoolConnStaleTotal = "pool_conn_stale_total"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Statser interface {
|
2024-10-05 16:11:46 +03:00
|
|
|
PoolStats() *goredis.PoolStats
|
2024-07-04 15:13:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Store) statsMeter() {
|
|
|
|
var st Statser
|
|
|
|
|
2024-10-05 16:11:46 +03:00
|
|
|
if r.cli != nil {
|
|
|
|
st = r.cli
|
2024-07-04 15:13:19 +03:00
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2024-10-05 16:11:46 +03:00
|
|
|
ticker := time.NewTicker(meter.DefaultMeterStatsInterval)
|
2024-07-04 15:13:19 +03:00
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
if st == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
stats := st.PoolStats()
|
|
|
|
r.opts.Meter.Counter(PoolHitsTotal).Set(uint64(stats.Hits))
|
|
|
|
r.opts.Meter.Counter(PoolMissesTotal).Set(uint64(stats.Misses))
|
|
|
|
r.opts.Meter.Counter(PoolTimeoutTotal).Set(uint64(stats.Timeouts))
|
|
|
|
r.opts.Meter.Counter(PoolConnTotalCurrent).Set(uint64(stats.TotalConns))
|
|
|
|
r.opts.Meter.Counter(PoolConnIdleCurrent).Set(uint64(stats.IdleConns))
|
|
|
|
r.opts.Meter.Counter(PoolConnStaleTotal).Set(uint64(stats.StaleConns))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|