Files
zeekay 6994cf05aa kv, not redis: rename the package, the extras and the URL scheme
We do not use Redis. The client was a go-redis fork and still said so
everywhere: `package redis`, `redis.NewClient`, `redis://`, `extra/redisotel`.
Consumers wrote `redis.Client` against a Valkey-backed store called KV.

  package redis            -> package kv          (93 decls)
  redis.X                  -> kv.X                (2,794 call sites)
  "redis: …" errors        -> "kv: …"             (206)
  extra/redis{otel,cmd,prometheus,census} -> extra/kv{otel,cmd,prometheus,census}
  redis:// , rediss://     -> kv:// , kvs://      (ParseURL)
  Redis*/‌*Redis* idents    -> KV*/*KV*
  REDIS_VERSION, REDIS_ENDPOINTS_CONFIG_PATH -> KV_*

I previously argued this rename was unsafe because much of it was "wire
contract". That was wrong, and the categorisation shows why: RESP command names
are GET/SET/HSET — they never contain the word. Of 5,771 occurrences only two
kinds were server-visible at all: the ParseURL scheme (we choose what we pass;
our configs already use kv://) and the CLIENT SETINFO lib-name label, which the
server merely records. Nothing here changes a byte of protocol.

Two things deliberately keep the old spelling:
  RediSearch  — names a specific external module the FT.* commands map to.
  StoreDist   — contains the letters by accident (sto-reDis-t), not the word.

Breaking for consumers by design: every import site must move redis.X -> kv.X.
Minor bump, never a major — this module stays v9 forever.
2026-07-26 08:34:19 -07:00

118 lines
3.3 KiB
Go

package kvprometheus
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/hanzokv/go/v9"
)
// StatGetter provides a method to get pool statistics.
type StatGetter interface {
PoolStats() *kv.PoolStats
}
// Collector collects statistics from a kv client.
// It implements the prometheus.Collector interface.
type Collector struct {
getter StatGetter
hitDesc *prometheus.Desc
missDesc *prometheus.Desc
timeoutDesc *prometheus.Desc
totalDesc *prometheus.Desc
idleDesc *prometheus.Desc
staleDesc *prometheus.Desc
}
var _ prometheus.Collector = (*Collector)(nil)
// NewCollector returns a new Collector based on the provided StatGetter.
// The given namespace and subsystem are used to build the fully qualified metric name,
// i.e. "{namespace}_{subsystem}_{metric}".
// The provided metrics are:
// - pool_hit_total
// - pool_miss_total
// - pool_timeout_total
// - pool_conn_total_current
// - pool_conn_idle_current
// - pool_conn_stale_total
func NewCollector(namespace, subsystem string, getter StatGetter) *Collector {
return &Collector{
getter: getter,
hitDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "pool_hit_total"),
"Number of times a connection was found in the pool",
nil, nil,
),
missDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "pool_miss_total"),
"Number of times a connection was not found in the pool",
nil, nil,
),
timeoutDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "pool_timeout_total"),
"Number of times a timeout occurred when looking for a connection in the pool",
nil, nil,
),
totalDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "pool_conn_total_current"),
"Current number of connections in the pool",
nil, nil,
),
idleDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "pool_conn_idle_current"),
"Current number of idle connections in the pool",
nil, nil,
),
staleDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "pool_conn_stale_total"),
"Number of times a connection was removed from the pool because it was stale",
nil, nil,
),
}
}
// Describe implements the prometheus.Collector interface.
func (s *Collector) Describe(descs chan<- *prometheus.Desc) {
descs <- s.hitDesc
descs <- s.missDesc
descs <- s.timeoutDesc
descs <- s.totalDesc
descs <- s.idleDesc
descs <- s.staleDesc
}
// Collect implements the prometheus.Collector interface.
func (s *Collector) Collect(metrics chan<- prometheus.Metric) {
stats := s.getter.PoolStats()
metrics <- prometheus.MustNewConstMetric(
s.hitDesc,
prometheus.CounterValue,
float64(stats.Hits),
)
metrics <- prometheus.MustNewConstMetric(
s.missDesc,
prometheus.CounterValue,
float64(stats.Misses),
)
metrics <- prometheus.MustNewConstMetric(
s.timeoutDesc,
prometheus.CounterValue,
float64(stats.Timeouts),
)
metrics <- prometheus.MustNewConstMetric(
s.totalDesc,
prometheus.GaugeValue,
float64(stats.TotalConns),
)
metrics <- prometheus.MustNewConstMetric(
s.idleDesc,
prometheus.GaugeValue,
float64(stats.IdleConns),
)
metrics <- prometheus.MustNewConstMetric(
s.staleDesc,
prometheus.CounterValue,
float64(stats.StaleConns),
)
}