db: allow registering multiple metrics collectors

This commit is contained in:
Simon Ser 2022-03-08 10:36:59 +01:00
parent ae68c82fc0
commit dac003960c
4 changed files with 8 additions and 6 deletions

View File

@ -316,7 +316,9 @@ func main() {
} }
if db, ok := db.(soju.MetricsCollectorDatabase); ok && srv.MetricsRegistry != nil { if db, ok := db.(soju.MetricsCollectorDatabase); ok && srv.MetricsRegistry != nil {
srv.MetricsRegistry.MustRegister(db.MetricsCollector()) if err := db.RegisterMetrics(srv.MetricsRegistry); err != nil {
log.Fatalf("failed to register database metrics: %v", err)
}
} }
sigCh := make(chan os.Signal, 1) sigCh := make(chan os.Signal, 1)

2
db.go
View File

@ -35,7 +35,7 @@ type Database interface {
type MetricsCollectorDatabase interface { type MetricsCollectorDatabase interface {
Database Database
MetricsCollector() prometheus.Collector RegisterMetrics(r prometheus.Registerer) error
} }
func OpenDB(driver, source string) (Database, error) { func OpenDB(driver, source string) (Database, error) {

View File

@ -176,8 +176,8 @@ func (db *PostgresDB) Close() error {
return db.db.Close() return db.db.Close()
} }
func (db *PostgresDB) MetricsCollector() prometheus.Collector { func (db *PostgresDB) RegisterMetrics(r prometheus.Registerer) error {
return promcollectors.NewDBStatsCollector(db.db, "main") return r.Register(promcollectors.NewDBStatsCollector(db.db, "main"))
} }
func (db *PostgresDB) Stats(ctx context.Context) (*DatabaseStats, error) { func (db *PostgresDB) Stats(ctx context.Context) (*DatabaseStats, error) {

View File

@ -259,8 +259,8 @@ func (db *SqliteDB) upgrade() error {
return tx.Commit() return tx.Commit()
} }
func (db *SqliteDB) MetricsCollector() prometheus.Collector { func (db *SqliteDB) RegisterMetrics(r prometheus.Registerer) error {
return promcollectors.NewDBStatsCollector(db.db, "main") return r.Register(promcollectors.NewDBStatsCollector(db.db, "main"))
} }
func (db *SqliteDB) Stats(ctx context.Context) (*DatabaseStats, error) { func (db *SqliteDB) Stats(ctx context.Context) (*DatabaseStats, error) {