Fix pg_up never being set to 1 (introduced by #162).

Correct fix for #160.
This commit is contained in:
Will Rouesnel 2018-03-06 23:20:31 +11:00
parent 2f4fc7201f
commit fa2c1e8f0f
2 changed files with 21 additions and 5 deletions

View File

@ -20,6 +20,7 @@ import (
"gopkg.in/yaml.v2"
"crypto/sha256"
"github.com/blang/semver"
_ "github.com/lib/pq"
"github.com/prometheus/client_golang/prometheus"
@ -971,10 +972,6 @@ func (e *Exporter) getDB(conn string) (*sql.DB, error) {
if err != nil {
return nil, err
}
err = d.Ping()
if err != nil {
return nil, err
}
d.SetMaxOpenConns(1)
d.SetMaxIdleConns(1)
@ -983,6 +980,15 @@ func (e *Exporter) getDB(conn string) (*sql.DB, error) {
log.Infoln("Established new database connection.")
}
// Always send a ping and possibly invalidate the connection if it fails
if err := e.dbConnection.Ping(); err != nil {
cerr := e.dbConnection.Close()
log.Infoln("Error while closing non-pinging DB connection:", cerr)
e.dbConnection = nil
e.psqlUp.Set(0)
return nil, err
}
return e.dbConnection, nil
}
@ -1007,11 +1013,14 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
loggableDsn = pDsn.String()
}
log.Infof("Error opening connection to database (%s): %s", loggableDsn, err)
e.psqlUp.Set(0)
e.error.Set(1)
e.psqlUp.Set(0) // Force "up" to 0 here.
return
}
// Didn't fail, can mark connection as up for this scrape.
e.psqlUp.Set(1)
// Check if map versions need to be updated
if err := e.checkMapVersions(ch, db); err != nil {
log.Warnln("Proceeding with outdated query maps, as the Postgres version could not be determined:", err)

View File

@ -108,6 +108,13 @@ smoketest_postgres() {
exit 1
fi
# HACK test: check pg_up is a 1 - TODO: expand integration tests to include metric consumption
if ! grep 'pg_up.* 1' $METRICS_DIR/.metrics.single.$version.prom ; then
echo "pg_up metric was not 1 despite exporter and database being up"
kill $exporter_pid
exit 1
fi
kill $exporter_pid
docker kill $CONTAINER_NAME
docker rm -v $CONTAINER_NAME