db: allow registering multiple metrics collectors
This commit is contained in:
parent
ae68c82fc0
commit
dac003960c
@ -316,7 +316,9 @@ func main() {
|
||||
}
|
||||
|
||||
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)
|
||||
|
2
db.go
2
db.go
@ -35,7 +35,7 @@ type Database interface {
|
||||
|
||||
type MetricsCollectorDatabase interface {
|
||||
Database
|
||||
MetricsCollector() prometheus.Collector
|
||||
RegisterMetrics(r prometheus.Registerer) error
|
||||
}
|
||||
|
||||
func OpenDB(driver, source string) (Database, error) {
|
||||
|
@ -176,8 +176,8 @@ func (db *PostgresDB) Close() error {
|
||||
return db.db.Close()
|
||||
}
|
||||
|
||||
func (db *PostgresDB) MetricsCollector() prometheus.Collector {
|
||||
return promcollectors.NewDBStatsCollector(db.db, "main")
|
||||
func (db *PostgresDB) RegisterMetrics(r prometheus.Registerer) error {
|
||||
return r.Register(promcollectors.NewDBStatsCollector(db.db, "main"))
|
||||
}
|
||||
|
||||
func (db *PostgresDB) Stats(ctx context.Context) (*DatabaseStats, error) {
|
||||
|
@ -259,8 +259,8 @@ func (db *SqliteDB) upgrade() error {
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (db *SqliteDB) MetricsCollector() prometheus.Collector {
|
||||
return promcollectors.NewDBStatsCollector(db.db, "main")
|
||||
func (db *SqliteDB) RegisterMetrics(r prometheus.Registerer) error {
|
||||
return r.Register(promcollectors.NewDBStatsCollector(db.db, "main"))
|
||||
}
|
||||
|
||||
func (db *SqliteDB) Stats(ctx context.Context) (*DatabaseStats, error) {
|
||||
|
Loading…
Reference in New Issue
Block a user