mirror of
https://github.com/prometheus-community/postgres_exporter
synced 2025-04-25 04:28:01 +00:00
fix-panic-on-shutdown-server fix panic on shutdown server
This commit is contained in:
parent
fce869257f
commit
725b163161
@ -782,21 +782,15 @@ func NewServer(dsn string, opts ...ServerOpt) (*Server, error) {
|
|||||||
|
|
||||||
// Close disconnects from Postgres.
|
// Close disconnects from Postgres.
|
||||||
func (s *Server) Close() error {
|
func (s *Server) Close() error {
|
||||||
if s.db != nil {
|
return s.db.Close()
|
||||||
err := s.db.Close()
|
|
||||||
s.db = nil
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ping checks connection availability and possibly invalidates the connection if it fails.
|
// Ping checks connection availability and possibly invalidates the connection if it fails.
|
||||||
func (s *Server) Ping() error {
|
func (s *Server) Ping() error {
|
||||||
if err := s.db.Ping(); err != nil {
|
if err := s.db.Ping(); err != nil {
|
||||||
if cerr := s.db.Close(); cerr != nil {
|
if cerr := s.Close(); cerr != nil {
|
||||||
log.Infof("Error while closing non-pinging DB connection to %q: %v", s, cerr)
|
log.Errorf("Error while closing non-pinging DB connection to %q: %v", s, cerr)
|
||||||
}
|
}
|
||||||
s.db = nil
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@ -814,7 +808,7 @@ func (s *Server) Scrape(ch chan<- prometheus.Metric, errGauge prometheus.Gauge,
|
|||||||
|
|
||||||
if !disableSettingsMetrics {
|
if !disableSettingsMetrics {
|
||||||
if err := querySettings(ch, s); err != nil {
|
if err := querySettings(ch, s); err != nil {
|
||||||
log.Infof("Error retrieving settings: %s", err)
|
log.Errorf("Error retrieving settings: %s", err)
|
||||||
errGauge.Inc()
|
errGauge.Inc()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1252,31 +1246,34 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
|
|||||||
e.totalScrapes.Inc()
|
e.totalScrapes.Inc()
|
||||||
|
|
||||||
for _, dsn := range e.dsn {
|
for _, dsn := range e.dsn {
|
||||||
server, err := e.servers.GetServer(dsn)
|
e.scrapeDSN(ch, dsn)
|
||||||
if err != nil {
|
|
||||||
loggableDSN := "could not parse DATA_SOURCE_NAME"
|
|
||||||
pDSN, pErr := parseDSN(dsn)
|
|
||||||
if pErr == nil {
|
|
||||||
loggableDSN = pDSN.String()
|
|
||||||
}
|
|
||||||
log.Infof("Error opening connection to database (%s): %v", loggableDSN, err)
|
|
||||||
e.error.Inc()
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Didn't fail, can mark connection as up for this scrape.
|
|
||||||
e.psqlUp.Inc()
|
|
||||||
|
|
||||||
// Check if map versions need to be updated
|
|
||||||
if err := e.checkMapVersions(ch, server); err != nil {
|
|
||||||
log.Warnln("Proceeding with outdated query maps, as the Postgres version could not be determined:", err)
|
|
||||||
e.error.Inc()
|
|
||||||
}
|
|
||||||
|
|
||||||
server.Scrape(ch, e.error, e.disableSettingsMetrics)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *Exporter) scrapeDSN(ch chan<- prometheus.Metric, dsn string) {
|
||||||
|
server, err := e.servers.GetServer(dsn)
|
||||||
|
if err != nil {
|
||||||
|
loggableDSN := "could not parse DATA_SOURCE_NAME"
|
||||||
|
if pDSN, pErr := parseDSN(dsn); pErr == nil {
|
||||||
|
loggableDSN = pDSN.String()
|
||||||
|
}
|
||||||
|
log.Errorf("Error opening connection to database (%s): %v", loggableDSN, err)
|
||||||
|
e.error.Inc()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Didn't fail, can mark connection as up for this scrape.
|
||||||
|
e.psqlUp.Inc()
|
||||||
|
|
||||||
|
// Check if map versions need to be updated
|
||||||
|
if err := e.checkMapVersions(ch, server); err != nil {
|
||||||
|
log.Warnln("Proceeding with outdated query maps, as the Postgres version could not be determined:", err)
|
||||||
|
e.error.Inc()
|
||||||
|
}
|
||||||
|
|
||||||
|
server.Scrape(ch, e.error, e.disableSettingsMetrics)
|
||||||
|
}
|
||||||
|
|
||||||
// try to get the DataSource
|
// try to get the DataSource
|
||||||
// DATA_SOURCE_NAME always wins so we do not break older versions
|
// DATA_SOURCE_NAME always wins so we do not break older versions
|
||||||
// reading secrets from files wins over secrets in environment variables
|
// reading secrets from files wins over secrets in environment variables
|
||||||
|
Loading…
Reference in New Issue
Block a user