github.com/quay/claircore@v1.5.28/pkg/poolstats/collector.go (about) 1 package poolstats 2 3 import ( 4 "time" 5 6 "github.com/jackc/pgx/v4/pgxpool" 7 "github.com/prometheus/client_golang/prometheus" 8 ) 9 10 var ( 11 _ prometheus.Collector = (*Collector)(nil) 12 _ stat = (*pgxpool.Stat)(nil) 13 ) 14 15 // Stat is the interface implemented by pgxpool.Stat. 16 type stat interface { 17 AcquireCount() int64 18 AcquireDuration() time.Duration 19 AcquiredConns() int32 20 CanceledAcquireCount() int64 21 ConstructingConns() int32 22 EmptyAcquireCount() int64 23 IdleConns() int32 24 MaxConns() int32 25 TotalConns() int32 26 } 27 28 type staterFunc func() stat 29 30 // Collector is a prometheus.Collector that will collect the nine statistics produced by pgxpool.Stat. 31 type Collector struct { 32 name string 33 stat staterFunc 34 35 acquireCountDesc *prometheus.Desc 36 acquireDurationDesc *prometheus.Desc 37 acquiredConnsDesc *prometheus.Desc 38 canceledAcquireCountDesc *prometheus.Desc 39 constructingConnsDesc *prometheus.Desc 40 emptyAcquireCountDesc *prometheus.Desc 41 idleConnsDesc *prometheus.Desc 42 maxConnsDesc *prometheus.Desc 43 totalConnsDesc *prometheus.Desc 44 } 45 46 // Stater is a provider of the Stat() function. Implemented by pgxpool.Pool. 47 type Stater interface { 48 Stat() *pgxpool.Stat 49 } 50 51 // NewCollector creates a new Collector to collect stats from pgxpool. 52 func NewCollector(stater Stater, appname string) *Collector { 53 fn := func() stat { return stater.Stat() } 54 return newCollector(fn, appname) 55 } 56 57 // NewCollector is an internal only constructor for a Collecter. It accepts a 58 // staterFunc which provides a closure for requesting pgxpool.Stat metrics. 59 // Labels to each metric and may be nil. A label is recommended when an 60 // application uses more than one pgxpool.Pool to enable differentiation between 61 // them. 62 func newCollector(fn staterFunc, n string) *Collector { 63 var constLabels = prometheus.Labels{"application_name": n} 64 return &Collector{ 65 name: n, 66 stat: fn, 67 acquireCountDesc: prometheus.NewDesc( 68 "pgxpool_acquire_count", 69 "Cumulative count of successful acquires from the pool.", 70 nil, constLabels), 71 acquireDurationDesc: prometheus.NewDesc( 72 "pgxpool_acquire_duration_seconds_total", 73 "Total duration of all successful acquires from the pool in nanoseconds.", 74 nil, constLabels), 75 acquiredConnsDesc: prometheus.NewDesc( 76 "pgxpool_acquired_conns", 77 "Number of currently acquired connections in the pool.", 78 nil, constLabels), 79 canceledAcquireCountDesc: prometheus.NewDesc( 80 "pgxpool_canceled_acquire_count", 81 "Cumulative count of acquires from the pool that were canceled by a context.", 82 nil, constLabels), 83 constructingConnsDesc: prometheus.NewDesc( 84 "pgxpool_constructing_conns", 85 "Number of conns with construction in progress in the pool.", 86 nil, constLabels), 87 emptyAcquireCountDesc: prometheus.NewDesc( 88 "pgxpool_empty_acquire", 89 "Cumulative count of successful acquires from the pool that waited for a resource to be released or constructed because the pool was empty.", 90 nil, constLabels), 91 idleConnsDesc: prometheus.NewDesc( 92 "pgxpool_idle_conns", 93 "Number of currently idle conns in the pool.", 94 nil, constLabels), 95 maxConnsDesc: prometheus.NewDesc( 96 "pgxpool_max_conns", 97 "Maximum size of the pool.", 98 nil, constLabels), 99 totalConnsDesc: prometheus.NewDesc( 100 "pgxpool_total_conns", 101 "Total number of resources currently in the pool. The value is the sum of ConstructingConns, AcquiredConns, and IdleConns.", 102 nil, constLabels), 103 } 104 } 105 106 // Describe implements the prometheus.Collector interface. 107 func (c *Collector) Describe(ch chan<- *prometheus.Desc) { 108 prometheus.DescribeByCollect(c, ch) 109 } 110 111 // Collect implements the prometheus.Collector interface. 112 func (c *Collector) Collect(metrics chan<- prometheus.Metric) { 113 s := c.stat() 114 metrics <- prometheus.MustNewConstMetric( 115 c.acquireCountDesc, 116 prometheus.CounterValue, 117 float64(s.AcquireCount()), 118 ) 119 metrics <- prometheus.MustNewConstMetric( 120 c.acquireDurationDesc, 121 prometheus.CounterValue, 122 s.AcquireDuration().Seconds(), 123 ) 124 metrics <- prometheus.MustNewConstMetric( 125 c.acquiredConnsDesc, 126 prometheus.GaugeValue, 127 float64(s.AcquiredConns()), 128 ) 129 metrics <- prometheus.MustNewConstMetric( 130 c.canceledAcquireCountDesc, 131 prometheus.CounterValue, 132 float64(s.CanceledAcquireCount()), 133 ) 134 metrics <- prometheus.MustNewConstMetric( 135 c.constructingConnsDesc, 136 prometheus.GaugeValue, 137 float64(s.ConstructingConns()), 138 ) 139 metrics <- prometheus.MustNewConstMetric( 140 c.emptyAcquireCountDesc, 141 prometheus.CounterValue, 142 float64(s.EmptyAcquireCount()), 143 ) 144 metrics <- prometheus.MustNewConstMetric( 145 c.idleConnsDesc, 146 prometheus.GaugeValue, 147 float64(s.IdleConns()), 148 ) 149 metrics <- prometheus.MustNewConstMetric( 150 c.maxConnsDesc, 151 prometheus.GaugeValue, 152 float64(s.MaxConns()), 153 ) 154 metrics <- prometheus.MustNewConstMetric( 155 c.totalConnsDesc, 156 prometheus.GaugeValue, 157 float64(s.TotalConns()), 158 ) 159 }