fix-panic-on-shutdown-server fix panic on shutdown server

This commit is contained in:
Anthony Regeda 2019-02-28 16:18:06 +03:00 committed by Will Rouesnel
parent fce869257f
commit 725b163161

View File

@ -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,16 +1246,20 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
e.totalScrapes.Inc() e.totalScrapes.Inc()
for _, dsn := range e.dsn { for _, dsn := range e.dsn {
e.scrapeDSN(ch, dsn)
}
}
func (e *Exporter) scrapeDSN(ch chan<- prometheus.Metric, dsn string) {
server, err := e.servers.GetServer(dsn) server, err := e.servers.GetServer(dsn)
if err != nil { if err != nil {
loggableDSN := "could not parse DATA_SOURCE_NAME" loggableDSN := "could not parse DATA_SOURCE_NAME"
pDSN, pErr := parseDSN(dsn) if pDSN, pErr := parseDSN(dsn); pErr == nil {
if pErr == nil {
loggableDSN = pDSN.String() loggableDSN = pDSN.String()
} }
log.Infof("Error opening connection to database (%s): %v", loggableDSN, err) log.Errorf("Error opening connection to database (%s): %v", loggableDSN, err)
e.error.Inc() e.error.Inc()
continue return
} }
// Didn't fail, can mark connection as up for this scrape. // Didn't fail, can mark connection as up for this scrape.
@ -1274,7 +1272,6 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
} }
server.Scrape(ch, e.error, e.disableSettingsMetrics) server.Scrape(ch, e.error, e.disableSettingsMetrics)
}
} }
// try to get the DataSource // try to get the DataSource