|
| 1 | +package redisprometheus |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/prometheus/client_golang/prometheus" |
| 5 | + |
| 6 | + "github.com/go-redis/redis/v9" |
| 7 | +) |
| 8 | + |
| 9 | +// StatGetter provides a method to get pool statistics. |
| 10 | +type StatGetter interface { |
| 11 | + PoolStats() *redis.PoolStats |
| 12 | +} |
| 13 | + |
| 14 | +// Collector collects statistics from a redis client. |
| 15 | +// It implements the prometheus.Collector interface. |
| 16 | +type Collector struct { |
| 17 | + getter StatGetter |
| 18 | + hitDesc *prometheus.Desc |
| 19 | + missDesc *prometheus.Desc |
| 20 | + timeoutDesc *prometheus.Desc |
| 21 | + totalDesc *prometheus.Desc |
| 22 | + idleDesc *prometheus.Desc |
| 23 | + staleDesc *prometheus.Desc |
| 24 | +} |
| 25 | + |
| 26 | +var _ prometheus.Collector = (*Collector)(nil) |
| 27 | + |
| 28 | +// NewCollector returns a new Collector based on the provided StatGetter. |
| 29 | +// The given namespace and subsystem are used to build the fully qualified metric name, |
| 30 | +// i.e. "{namespace}_{subsystem}_{metric}". |
| 31 | +// The provided metrics are: |
| 32 | +// * pool_hit_total |
| 33 | +// * pool_miss_total |
| 34 | +// * pool_timeout_total |
| 35 | +// * pool_conn_total_current |
| 36 | +// * pool_conn_idle_current |
| 37 | +// * pool_conn_stale_total |
| 38 | +func NewCollector(namespace, subsystem string, getter StatGetter) *Collector { |
| 39 | + return &Collector{ |
| 40 | + getter: getter, |
| 41 | + hitDesc: prometheus.NewDesc( |
| 42 | + prometheus.BuildFQName(namespace, subsystem, "pool_hit_total"), |
| 43 | + "Number of times a connection was found in the pool", |
| 44 | + nil, nil, |
| 45 | + ), |
| 46 | + missDesc: prometheus.NewDesc( |
| 47 | + prometheus.BuildFQName(namespace, subsystem, "pool_miss_total"), |
| 48 | + "Number of times a connection was not found in the pool", |
| 49 | + nil, nil, |
| 50 | + ), |
| 51 | + timeoutDesc: prometheus.NewDesc( |
| 52 | + prometheus.BuildFQName(namespace, subsystem, "pool_timeout_total"), |
| 53 | + "Number of times a timeout occurred when looking for a connection in the pool", |
| 54 | + nil, nil, |
| 55 | + ), |
| 56 | + totalDesc: prometheus.NewDesc( |
| 57 | + prometheus.BuildFQName(namespace, subsystem, "pool_conn_total_current"), |
| 58 | + "Current number of connections in the pool", |
| 59 | + nil, nil, |
| 60 | + ), |
| 61 | + idleDesc: prometheus.NewDesc( |
| 62 | + prometheus.BuildFQName(namespace, subsystem, "pool_conn_idle_current"), |
| 63 | + "Current number of idle connections in the pool", |
| 64 | + nil, nil, |
| 65 | + ), |
| 66 | + staleDesc: prometheus.NewDesc( |
| 67 | + prometheus.BuildFQName(namespace, subsystem, "pool_conn_stale_total"), |
| 68 | + "Number of times a connection was removed from the pool because it was stale", |
| 69 | + nil, nil, |
| 70 | + ), |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// Describe implements the prometheus.Collector interface. |
| 75 | +func (s *Collector) Describe(descs chan<- *prometheus.Desc) { |
| 76 | + descs <- s.hitDesc |
| 77 | + descs <- s.missDesc |
| 78 | + descs <- s.timeoutDesc |
| 79 | + descs <- s.totalDesc |
| 80 | + descs <- s.idleDesc |
| 81 | + descs <- s.staleDesc |
| 82 | +} |
| 83 | + |
| 84 | +// Collect implements the prometheus.Collector interface. |
| 85 | +func (s *Collector) Collect(metrics chan<- prometheus.Metric) { |
| 86 | + stats := s.getter.PoolStats() |
| 87 | + metrics <- prometheus.MustNewConstMetric( |
| 88 | + s.hitDesc, |
| 89 | + prometheus.CounterValue, |
| 90 | + float64(stats.Hits), |
| 91 | + ) |
| 92 | + metrics <- prometheus.MustNewConstMetric( |
| 93 | + s.missDesc, |
| 94 | + prometheus.CounterValue, |
| 95 | + float64(stats.Misses), |
| 96 | + ) |
| 97 | + metrics <- prometheus.MustNewConstMetric( |
| 98 | + s.timeoutDesc, |
| 99 | + prometheus.CounterValue, |
| 100 | + float64(stats.Timeouts), |
| 101 | + ) |
| 102 | + metrics <- prometheus.MustNewConstMetric( |
| 103 | + s.totalDesc, |
| 104 | + prometheus.GaugeValue, |
| 105 | + float64(stats.TotalConns), |
| 106 | + ) |
| 107 | + metrics <- prometheus.MustNewConstMetric( |
| 108 | + s.idleDesc, |
| 109 | + prometheus.GaugeValue, |
| 110 | + float64(stats.IdleConns), |
| 111 | + ) |
| 112 | + metrics <- prometheus.MustNewConstMetric( |
| 113 | + s.staleDesc, |
| 114 | + prometheus.CounterValue, |
| 115 | + float64(stats.StaleConns), |
| 116 | + ) |
| 117 | +} |
0 commit comments