From aea6fae7d64e874e3b8416c6d4200382b9337a6c Mon Sep 17 00:00:00 2001 From: Yann Soubeyrand Date: Thu, 24 Dec 2020 16:41:05 +0100 Subject: [PATCH] Recover when connection cannot be established straight at startup (#415) When the connection to the PostgreSQL instance cannot be established straight at startup, a race condition can happen when autoDiscoverDatabases is true. If discoverDatabaseDSNs fails, no dsn is set as the master database, and, if scrapeDSN succeeds, checkMapVersions will have omitted the default metrics in the server metric map. The metric map won't be updated unless the version returned by the PostgreSQL instance changes. With this patch, scrapeDSN won't be run unless discoverDatabaseDSNs succeeded and thus the race condition is eliminated. Signed-off-by: Yann Soubeyrand --- cmd/postgres_exporter/postgres_exporter.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/postgres_exporter/postgres_exporter.go b/cmd/postgres_exporter/postgres_exporter.go index 5e4387dc..a73dcc84 100644 --- a/cmd/postgres_exporter/postgres_exporter.go +++ b/cmd/postgres_exporter/postgres_exporter.go @@ -1624,12 +1624,12 @@ func (e *Exporter) discoverDatabaseDSNs() []string { continue } - dsns[dsn] = struct{}{} server, err := e.servers.GetServer(dsn) if err != nil { log.Errorf("Error opening connection to database (%s): %v", loggableDSN(dsn), err) continue } + dsns[dsn] = struct{}{} // If autoDiscoverDatabases is true, set first dsn as master database (Default: false) server.master = true