Allow the redacted DSN logging to actually fire (DSN was never logged).

The DSN parsing code failed to ever detect a URL like DSN, and as such never
logged anything.
This commit is contained in:
Will Rouesnel 2017-08-04 00:01:05 +10:00
parent c692b4e76a
commit 23f4af2354
2 changed files with 25 additions and 11 deletions

View File

@ -965,9 +965,13 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
db, err := e.getDB(e.dsn) db, err := e.getDB(e.dsn)
if err != nil { if err != nil {
loggableDsn := "could not parse DATA_SOURCE_NAME" loggableDsn := "could not parse DATA_SOURCE_NAME"
if pDsn, pErr := url.Parse(e.dsn); pErr != nil { // If the DSN is parseable, log it with a blanked out password
log.Debugln("Blanking password for loggable DSN:", e.dsn) pDsn, pErr := url.Parse(e.dsn)
pDsn.User = url.UserPassword(pDsn.User.Username(), "xxx") if pErr == nil {
// Blank user info if not nil
if pDsn.User != nil {
pDsn.User = url.UserPassword(pDsn.User.Username(), "PASSWORD_REMOVED")
}
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)
@ -1017,6 +1021,12 @@ func main() {
} }
exporter := NewExporter(dsn, *queriesPath) exporter := NewExporter(dsn, *queriesPath)
defer func() {
if exporter.dbConnection != nil {
exporter.dbConnection.Close() // nolint: errcheck
}
}()
prometheus.MustRegister(exporter) prometheus.MustRegister(exporter)
http.Handle(*metricPath, prometheus.Handler()) http.Handle(*metricPath, prometheus.Handler())
@ -1026,7 +1036,4 @@ func main() {
log.Infof("Starting Server: %s", *listenAddress) log.Infof("Starting Server: %s", *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil)) log.Fatal(http.ListenAndServe(*listenAddress, nil))
if sharedDBConn != nil {
defer sharedDBConn.Close() // nolint: errcheck
}
} }

View File

@ -48,7 +48,6 @@ func (s *IntegrationSuite) TestAllNamespacesReturnResults(c *C) {
} }
}() }()
// Open a database connection // Open a database connection
db, err := sql.Open("postgres", s.e.dsn) db, err := sql.Open("postgres", s.e.dsn)
c.Assert(db, NotNil) c.Assert(db, NotNil)
@ -75,8 +74,9 @@ func (s *IntegrationSuite) TestAllNamespacesReturnResults(c *C) {
} }
} }
// TestInvalidDsnDoesntCrash tests that specifying an invalid DSN doesn't crash the exporter. // TestInvalidDsnDoesntCrash tests that specifying an invalid DSN doesn't crash
// https://github.com/wrouesnel/postgres_exporter/issues/93 // the exporter. Related to https://github.com/wrouesnel/postgres_exporter/issues/93
// although not a replication of the scenario.
func (s *IntegrationSuite) TestInvalidDsnDoesntCrash(c *C) { func (s *IntegrationSuite) TestInvalidDsnDoesntCrash(c *C) {
// Setup a dummy channel to consume metrics // Setup a dummy channel to consume metrics
ch := make(chan prometheus.Metric, 100) ch := make(chan prometheus.Metric, 100)
@ -85,6 +85,13 @@ func (s *IntegrationSuite) TestInvalidDsnDoesntCrash(c *C) {
} }
}() }()
exporter := NewExporter("an invalid dsn", *queriesPath) // Send a bad DSN
exporter := NewExporter("invalid dsn", *queriesPath)
c.Assert(exporter, NotNil)
exporter.scrape(ch) exporter.scrape(ch)
}
// Send a DSN to a non-listening port.
exporter = NewExporter("postgresql://nothing:nothing@127.0.0.1:1/nothing", *queriesPath)
c.Assert(exporter, NotNil)
exporter.scrape(ch)
}