Skip to content
This repository has been archived by the owner on Aug 22, 2024. It is now read-only.

Commit

Permalink
Fix connection handling (#30)
Browse files Browse the repository at this point in the history
Co-authored-by: Lesovsky Aleksey <[email protected]>
  • Loading branch information
lesovsky and Lesovsky Aleksey authored Apr 1, 2022
1 parent c37fada commit d098923
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
4 changes: 1 addition & 3 deletions internal/collector/collector_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,9 @@ func updateFromMultipleDatabases(config Config, descSets []typedDescSet, ch chan
err = updateSingleDescSet(conn, s, ch, true)
if err != nil {
log.Errorf("collect failed: %s; skip", err)
conn.Close()
continue
}

// Close connection.
// Close connection and go next database.
conn.Close()
}
}
Expand Down
4 changes: 3 additions & 1 deletion internal/collector/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ func discoverPgStatStatements(connStr string) (bool, string, string, error) {
var setting string
err = conn.Conn().QueryRow(context.Background(), "SELECT setting FROM pg_settings WHERE name = 'shared_preload_libraries'").Scan(&setting)
if err != nil {
conn.Close()
return false, "", "", err
}

Expand Down Expand Up @@ -238,10 +239,11 @@ func discoverPgStatStatements(connStr string) (bool, string, string, error) {

// If pg_stat_statements found, update source and return connection.
if schema := extensionInstalledSchema(conn, "pg_stat_statements"); schema != "" {
conn.Close()
return true, conn.Conn().Config().Database, schema, nil
}

// Otherwise close connection and go to next database in the list.
// Otherwise, close connection and go to next database in the list.
conn.Close()
}

Expand Down
4 changes: 2 additions & 2 deletions internal/collector/postgres_statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,14 @@ func (c *postgresStatementsCollector) Update(config Config, ch chan<- prometheus
return err
}

defer conn.Close()

// get pg_stat_statements stats
res, err := conn.Query(selectStatementsQuery(config.serverVersionNum, config.pgStatStatementsSchema))
if err != nil {
return err
}

conn.Close()

// parse pg_stat_statements stats
stats := parsePostgresStatementsStats(res, []string{"user", "database", "queryid", "query"})

Expand Down

0 comments on commit d098923

Please sign in to comment.