mirror of
https://github.com/prometheus-community/postgres_exporter
synced 2025-04-27 05:28:03 +00:00
Fix pg_up never being set to 1 (introduced by #162).
Correct fix for #160.
This commit is contained in:
parent
2f4fc7201f
commit
fa2c1e8f0f
@ -20,6 +20,7 @@ import (
|
|||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
|
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
|
|
||||||
"github.com/blang/semver"
|
"github.com/blang/semver"
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
@ -971,10 +972,6 @@ func (e *Exporter) getDB(conn string) (*sql.DB, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
err = d.Ping()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
d.SetMaxOpenConns(1)
|
d.SetMaxOpenConns(1)
|
||||||
d.SetMaxIdleConns(1)
|
d.SetMaxIdleConns(1)
|
||||||
@ -983,6 +980,15 @@ func (e *Exporter) getDB(conn string) (*sql.DB, error) {
|
|||||||
log.Infoln("Established new database connection.")
|
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
|
return e.dbConnection, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1007,11 +1013,14 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
|
|||||||
loggableDsn = pDsn.String()
|
loggableDsn = pDsn.String()
|
||||||
}
|
}
|
||||||
log.Infof("Error opening connection to database (%s): %s", loggableDsn, err)
|
log.Infof("Error opening connection to database (%s): %s", loggableDsn, err)
|
||||||
|
e.psqlUp.Set(0)
|
||||||
e.error.Set(1)
|
e.error.Set(1)
|
||||||
e.psqlUp.Set(0) // Force "up" to 0 here.
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Didn't fail, can mark connection as up for this scrape.
|
||||||
|
e.psqlUp.Set(1)
|
||||||
|
|
||||||
// Check if map versions need to be updated
|
// Check if map versions need to be updated
|
||||||
if err := e.checkMapVersions(ch, db); err != nil {
|
if err := e.checkMapVersions(ch, db); err != nil {
|
||||||
log.Warnln("Proceeding with outdated query maps, as the Postgres version could not be determined:", err)
|
log.Warnln("Proceeding with outdated query maps, as the Postgres version could not be determined:", err)
|
||||||
|
@ -108,6 +108,13 @@ smoketest_postgres() {
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
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
|
kill $exporter_pid
|
||||||
docker kill $CONTAINER_NAME
|
docker kill $CONTAINER_NAME
|
||||||
docker rm -v $CONTAINER_NAME
|
docker rm -v $CONTAINER_NAME
|
||||||
|
Loading…
Reference in New Issue
Block a user