Co-authored-by: Jeffrey Smith <jasafpro@gmail.com> Co-committed-by: Jeffrey Smith <jasafpro@gmail.com>
27 lines
652 B
Go
27 lines
652 B
Go
package metrics
|
|
|
|
import (
|
|
"database/sql"
|
|
"time"
|
|
)
|
|
|
|
// StartDBCollector begins a background goroutine that reads sql.DBStats
|
|
// every interval and updates the Prometheus DB pool gauges.
|
|
func StartDBCollector(db *sql.DB, interval time.Duration) {
|
|
if db == nil {
|
|
return
|
|
}
|
|
go func() {
|
|
ticker := time.NewTicker(interval)
|
|
defer ticker.Stop()
|
|
for range ticker.C {
|
|
stats := db.Stats()
|
|
DBOpenConnections.Set(float64(stats.OpenConnections))
|
|
DBInUseConnections.Set(float64(stats.InUse))
|
|
DBIdleConnections.Set(float64(stats.Idle))
|
|
DBWaitCount.Set(float64(stats.WaitCount))
|
|
DBWaitDuration.Set(stats.WaitDuration.Seconds())
|
|
}
|
|
}()
|
|
}
|