1
0
mirror of https://github.com/prometheus-community/postgres_exporter.git synced 2025-12-21 04:00:58 +03:00

Ensure database connections are always closed (#1177)

Signed-off-by: Sam DeHaan <sam.dehaan@grafana.com>
Signed-off-by: Sam DeHaan <dehaansa@gmail.com>
Co-authored-by: Sam DeHaan <sam.dehaan@grafana.com>
Co-authored-by: Sam DeHaan <dehaansa@gmail.com>
This commit is contained in:
Cristian Greco
2025-07-18 20:53:45 +02:00
committed by GitHub
parent aa98bb30ef
commit 06a553c816
3 changed files with 7 additions and 2 deletions

View File

@@ -173,11 +173,11 @@ func (p PostgresCollector) Collect(ch chan<- prometheus.Metric) {
// Set up the database connection for the collector.
err := inst.setup()
defer inst.Close()
if err != nil {
p.logger.Error("Error opening connection to database", "err", err)
return
}
defer inst.Close()
wg := sync.WaitGroup{}
wg.Add(len(p.Collectors))
@@ -190,6 +190,10 @@ func (p PostgresCollector) Collect(ch chan<- prometheus.Metric) {
wg.Wait()
}
func (p *PostgresCollector) Close() error {
return p.instance.Close()
}
func execute(ctx context.Context, name string, c Collector, instance *instance, ch chan<- prometheus.Metric, logger *slog.Logger) {
begin := time.Now()
err := c.Update(ctx, instance, ch)