Changeset 0.33.0 (#207)
Co-authored-by: Jeffrey Smith <jasafpro@gmail.com> Co-committed-by: Jeffrey Smith <jasafpro@gmail.com>
This commit is contained in:
26
server/metrics/db_collector.go
Normal file
26
server/metrics/db_collector.go
Normal file
@@ -0,0 +1,26 @@
|
||||
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())
|
||||
}
|
||||
}()
|
||||
}
|
||||
Reference in New Issue
Block a user