This repository has been archived on 2026-04-03. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
core/server/metrics/db_collector.go
Jeffrey Smith ed3e9363f2 Changeset 0.33.0 (#207)
Co-authored-by: Jeffrey Smith <jasafpro@gmail.com>
Co-committed-by: Jeffrey Smith <jasafpro@gmail.com>
2026-03-19 21:37:32 +00:00

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())
}
}()
}