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 {
|
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
2
db.go
@ -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) {
|
||||||
|
@ -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) {
|
||||||
|
@ -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) {
|
||||||
|
Loading…
Reference in New Issue
Block a user