From f48df1f5d61757d666138548b1ddfcf57c6c0a9b Mon Sep 17 00:00:00 2001 From: Leonardo Pucci <38509299+leopucci@users.noreply.github.com> Date: Fri, 22 Mar 2019 18:37:52 -0300 Subject: [PATCH 1/2] Prevent connection error overflow on windows This small modification assure that Metricbeat closes the connections in case of errors. As reported by #11393. The thing is that on windows you have to install openssl to enable postgresql secure connections, the default is to use insecure connection. While pq library uses by default security connections. So the library is throwing an error: pq: SSL is not enabled on the server and not closing connections resulting on an overflow of connections if you set the period below 5 seconds. --- metricbeat/module/postgresql/database/database.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/metricbeat/module/postgresql/database/database.go b/metricbeat/module/postgresql/database/database.go index 447b8614d46..93de09bd694 100644 --- a/metricbeat/module/postgresql/database/database.go +++ b/metricbeat/module/postgresql/database/database.go @@ -57,12 +57,13 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) { // of an error set the Error field of mb.Event or simply call report.Error(). func (m *MetricSet) Fetch(reporter mb.ReporterV2) { db, err := sql.Open("postgres", m.HostData().URI) + defer db.Close() if err != nil { logger.Error(err) reporter.Error(err) return } - defer db.Close() + results, err := postgresql.QueryStats(db, "SELECT * FROM pg_stat_database") if err != nil { From 1cc9682acfe76faea6340a6177795d170f7e2d4a Mon Sep 17 00:00:00 2001 From: Leonardo Pucci <38509299+leopucci@users.noreply.github.com> Date: Sun, 24 Mar 2019 13:07:55 -0300 Subject: [PATCH 2/2] Nil check --- metricbeat/module/postgresql/database/database.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/metricbeat/module/postgresql/database/database.go b/metricbeat/module/postgresql/database/database.go index 93de09bd694..5855500d7fe 100644 --- a/metricbeat/module/postgresql/database/database.go +++ b/metricbeat/module/postgresql/database/database.go @@ -57,7 +57,9 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) { // of an error set the Error field of mb.Event or simply call report.Error(). func (m *MetricSet) Fetch(reporter mb.ReporterV2) { db, err := sql.Open("postgres", m.HostData().URI) + if db != nil { defer db.Close() + } if err != nil { logger.Error(err) reporter.Error(err)