From 76223cdda85972215fd99d4d6214019796faf9ec Mon Sep 17 00:00:00 2001 From: Kurtis Bass Date: Wed, 14 Dec 2022 14:30:13 -0500 Subject: [PATCH] probe: clean-up database connection after probe to prevent connection leak Signed-off-by: Kurtis Bass --- cmd/postgres_exporter/probe.go | 3 +++ collector/probe.go | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/cmd/postgres_exporter/probe.go b/cmd/postgres_exporter/probe.go index 28d1395..167e827 100644 --- a/cmd/postgres_exporter/probe.go +++ b/cmd/postgres_exporter/probe.go @@ -104,6 +104,9 @@ func handleProbe(logger log.Logger) http.HandlerFunc { return } + // Cleanup underlying connections to prevent connection leaks + defer pc.Close() + // TODO(@sysadmind): Remove the registry.MustRegister() call below and instead handle the collection here. That will allow // for the passing of context, handling of timeouts, and more control over the collection. // The current NewProbeCollector() implementation relies on the MustNewConstMetric() call to create the metrics which is not diff --git a/collector/probe.go b/collector/probe.go index 37cb151..c8bf8ee 100644 --- a/collector/probe.go +++ b/collector/probe.go @@ -83,3 +83,7 @@ func (pc *ProbeCollector) Collect(ch chan<- prometheus.Metric) { } wg.Wait() } + +func (pc *ProbeCollector) Close() error { + return pc.db.Close() +}