@@ -20,6 +20,7 @@ import (
2020 "gopkg.in/yaml.v2"
2121
2222 "crypto/sha256"
23+
2324 "github.com/blang/semver"
2425 _ "github.com/lib/pq"
2526 "github.com/prometheus/client_golang/prometheus"
@@ -971,10 +972,6 @@ func (e *Exporter) getDB(conn string) (*sql.DB, error) {
971972 if err != nil {
972973 return nil , err
973974 }
974- err = d .Ping ()
975- if err != nil {
976- return nil , err
977- }
978975
979976 d .SetMaxOpenConns (1 )
980977 d .SetMaxIdleConns (1 )
@@ -983,6 +980,15 @@ func (e *Exporter) getDB(conn string) (*sql.DB, error) {
983980 log .Infoln ("Established new database connection." )
984981 }
985982
983+ // Always send a ping and possibly invalidate the connection if it fails
984+ if err := e .dbConnection .Ping (); err != nil {
985+ cerr := e .dbConnection .Close ()
986+ log .Infoln ("Error while closing non-pinging DB connection:" , cerr )
987+ e .dbConnection = nil
988+ e .psqlUp .Set (0 )
989+ return nil , err
990+ }
991+
986992 return e .dbConnection , nil
987993}
988994
@@ -1007,11 +1013,14 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
10071013 loggableDsn = pDsn .String ()
10081014 }
10091015 log .Infof ("Error opening connection to database (%s): %s" , loggableDsn , err )
1016+ e .psqlUp .Set (0 )
10101017 e .error .Set (1 )
1011- e .psqlUp .Set (0 ) // Force "up" to 0 here.
10121018 return
10131019 }
10141020
1021+ // Didn't fail, can mark connection as up for this scrape.
1022+ e .psqlUp .Set (1 )
1023+
10151024 // Check if map versions need to be updated
10161025 if err := e .checkMapVersions (ch , db ); err != nil {
10171026 log .Warnln ("Proceeding with outdated query maps, as the Postgres version could not be determined:" , err )
0 commit comments