1
0
mirror of https://github.com/prometheus-community/postgres_exporter.git synced 2025-11-03 07:53:12 +03:00

Delay database connection until scrape (#882)

This no longer returns an error when creating a collector.instance when the database cannot be reached for the version query. This will resolve the entire postgresCollector not being registered for metrics collection when a database is not available. If the version query fails, the scrape will fail.

Resolves #880

Signed-off-by: Joe Adams <github@joeadams.io>
This commit is contained in:
Joe Adams
2023-08-23 17:33:47 -04:00
committed by GitHub
parent 04bb60ce31
commit b74852a535
3 changed files with 38 additions and 8 deletions

View File

@@ -22,29 +22,43 @@ import (
)
type instance struct {
dsn string
db *sql.DB
version semver.Version
}
func newInstance(dsn string) (*instance, error) {
i := &instance{}
i := &instance{
dsn: dsn,
}
// "Create" a database handle to verify the DSN provided is valid.
// Open is not guaranteed to create a connection.
db, err := sql.Open("postgres", dsn)
if err != nil {
return nil, err
}
db.Close()
return i, nil
}
func (i *instance) setup() error {
db, err := sql.Open("postgres", i.dsn)
if err != nil {
return err
}
db.SetMaxOpenConns(1)
db.SetMaxIdleConns(1)
i.db = db
version, err := queryVersion(db)
version, err := queryVersion(i.db)
if err != nil {
db.Close()
return nil, err
return fmt.Errorf("error querying postgresql version: %w", err)
} else {
i.version = version
}
i.version = version
return i, nil
return nil
}
func (i *instance) getDB() *sql.DB {