diff --git a/example/main.go b/example/main.go index 8eaedfa..0d91c98 100644 --- a/example/main.go +++ b/example/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "os" "github.com/go-pg/pg/v9" @@ -17,6 +18,9 @@ func main() { db := pg.Connect(dbOpts) + // create unique pool name from DB address and database name + poolName := fmt.Printf("%s/%s", dbOpts.Addr, dbOpts.Database) + mon := monitor.NewMonitor( // Observer package must match your go-pg version. // E.g. for go-pg v10.x.x use package gopgv10. @@ -31,6 +35,8 @@ func main() { // my_app_go_pg_pool_hits{} monitor.MetricsWithNamespace("my_app"), ), + // set pool name + monitor.MonitorWithPoolName(poolName), ) mon.Open() diff --git a/metrics.go b/metrics.go index ac07aca..e968652 100644 --- a/metrics.go +++ b/metrics.go @@ -17,13 +17,13 @@ type Metrics struct { constLabels prometheus.Labels - hits prometheus.Gauge - misses prometheus.Gauge - timeouts prometheus.Gauge + hits *prometheus.GaugeVec + misses *prometheus.GaugeVec + timeouts *prometheus.GaugeVec - totalConns prometheus.Gauge - idleConns prometheus.Gauge - staleConns prometheus.Gauge + totalConns *prometheus.GaugeVec + idleConns *prometheus.GaugeVec + staleConns *prometheus.GaugeVec registerer prometheus.Registerer } @@ -71,7 +71,7 @@ func NewMetrics(opts ...MetricsOption) *Metrics { opt(m) } - hits := prometheus.NewGauge( + hits := prometheus.NewGaugeVec( prometheus.GaugeOpts{ Namespace: m.namespace, Subsystem: m.subsystem, @@ -79,11 +79,12 @@ func NewMetrics(opts ...MetricsOption) *Metrics { Name: "pool_hits", Help: "Number of times free connection was found in the pool", }, + []string{"pool_name"}, ) m.registerer.MustRegister(hits) m.hits = hits - misses := prometheus.NewGauge( + misses := prometheus.NewGaugeVec( prometheus.GaugeOpts{ Namespace: m.namespace, Subsystem: m.subsystem, @@ -91,11 +92,12 @@ func NewMetrics(opts ...MetricsOption) *Metrics { Name: "pool_misses", Help: "Number of times free connection was NOT found in the pool", }, + []string{"pool_name"}, ) m.registerer.MustRegister(misses) m.misses = misses - timeouts := prometheus.NewGauge( + timeouts := prometheus.NewGaugeVec( prometheus.GaugeOpts{ Namespace: m.namespace, Subsystem: m.subsystem, @@ -103,11 +105,12 @@ func NewMetrics(opts ...MetricsOption) *Metrics { Name: "pool_timeouts", Help: "Number of times a wait timeout occurred", }, + []string{"pool_name"}, ) m.registerer.MustRegister(timeouts) m.timeouts = timeouts - totalConns := prometheus.NewGauge( + totalConns := prometheus.NewGaugeVec( prometheus.GaugeOpts{ Namespace: m.namespace, Subsystem: m.subsystem, @@ -115,11 +118,12 @@ func NewMetrics(opts ...MetricsOption) *Metrics { Name: "pool_total_connections", Help: "Number of total connections in the pool", }, + []string{"pool_name"}, ) m.registerer.MustRegister(totalConns) m.totalConns = totalConns - idleConns := prometheus.NewGauge( + idleConns := prometheus.NewGaugeVec( prometheus.GaugeOpts{ Namespace: m.namespace, Subsystem: m.subsystem, @@ -127,11 +131,12 @@ func NewMetrics(opts ...MetricsOption) *Metrics { Name: "pool_idle_connections", Help: "Number of idle connections in the pool", }, + []string{"pool_name"}, ) m.registerer.MustRegister(idleConns) m.idleConns = idleConns - staleConns := prometheus.NewGauge( + staleConns := prometheus.NewGaugeVec( prometheus.GaugeOpts{ Namespace: m.namespace, Subsystem: m.subsystem, @@ -139,6 +144,7 @@ func NewMetrics(opts ...MetricsOption) *Metrics { Name: "pool_stale_connections", Help: "Number of stale connections removed from the pool", }, + []string{"pool_name"}, ) m.registerer.MustRegister(staleConns) m.staleConns = staleConns diff --git a/monitor.go b/monitor.go index d823040..d6139d0 100644 --- a/monitor.go +++ b/monitor.go @@ -21,11 +21,20 @@ type Monitor struct { wg sync.WaitGroup ctx context.Context cancel context.CancelFunc + + poolName string } // MonitorOption is an option for NewMonitor. type MonitorOption func(monitor *Monitor) +// MonitorWithPoolName is an option that sets pool name for the monitor. +func MonitorWithPoolName(poolName string) MonitorOption { //nolint:golint + return func(monitor *Monitor) { + monitor.poolName = poolName + } +} + // NewMonitor returns a new configured Monitor. func NewMonitor(observer Observer, metrics *Metrics, opts ...MonitorOption) *Monitor { m := &Monitor{ @@ -79,11 +88,11 @@ func (m *Monitor) worker() { func (m *Monitor) sync() { stats := m.observer.Observe() - m.metrics.hits.Set(float64(stats.Hits)) - m.metrics.misses.Set(float64(stats.Misses)) - m.metrics.timeouts.Set(float64(stats.Timeouts)) + m.metrics.hits.WithLabelValues(m.poolName).Set(float64(stats.Hits)) + m.metrics.misses.WithLabelValues(m.poolName).Set(float64(stats.Misses)) + m.metrics.timeouts.WithLabelValues(m.poolName).Set(float64(stats.Timeouts)) - m.metrics.totalConns.Set(float64(stats.TotalConns)) - m.metrics.idleConns.Set(float64(stats.IdleConns)) - m.metrics.staleConns.Set(float64(stats.StaleConns)) + m.metrics.totalConns.WithLabelValues(m.poolName).Set(float64(stats.TotalConns)) + m.metrics.idleConns.WithLabelValues(m.poolName).Set(float64(stats.IdleConns)) + m.metrics.staleConns.WithLabelValues(m.poolName).Set(float64(stats.StaleConns)) }