Skip to content

Commit 2ba7806

Browse files
Add query timeouts (#268)
Add query timeouts Signed-off-by: Anders Swanson <[email protected]>
1 parent 6ff9670 commit 2ba7806

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
Our current priorities are support for Exadata metrics. We expect to address these in an upcoming release.
66

77
- Fixed a case-sensitive issue with resource name in the default metrics file.
8+
- Add query timeouts to initial database connections, which could cause the exporter to hang in multi-database configurations
9+
- Fix an issue where rapidly acquiring connections could cause the exporter to crash. This was more common in multi-database configurations, due to the increased number of connection pools.
810

911
### Version 2.0.1, June 12, 2025
1012

collector/database.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ func (d *Database) DBTypeMetric() prometheus.Metric {
4242
}
4343

4444
func (d *Database) ping(logger *slog.Logger) error {
45-
err := d.Session.Ping()
45+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
46+
defer cancel()
47+
err := d.Session.PingContext(ctx)
4648
if err != nil {
47-
4849
d.Up = 0
4950
if strings.Contains(err.Error(), "sql: database is closed") {
5051
db, dbtype := connect(logger, d.Name, d.Config)
@@ -178,20 +179,22 @@ func connect(logger *slog.Logger, dbname string, dbconfig DatabaseConfig) (*sql.
178179
db.SetConnMaxLifetime(0)
179180
logger.Debug(fmt.Sprintf("Successfully configured connection to %s", maskDsn(dbconfig.URL)), "database", dbname)
180181

181-
if _, err := db.Exec(`
182+
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
183+
defer cancel()
184+
if _, err := db.ExecContext(ctx, `
182185
begin
183186
dbms_application_info.set_client_info('oracledb_exporter');
184187
end;`); err != nil {
185188
logger.Info("Could not set CLIENT_INFO.", "database", dbname)
186189
}
187190

188191
var result int
189-
if err := db.QueryRow("select sys_context('USERENV', 'CON_ID') from dual").Scan(&result); err != nil {
192+
if err := db.QueryRowContext(ctx, "select sys_context('USERENV', 'CON_ID') from dual").Scan(&result); err != nil {
190193
logger.Info("dbtype err", "error", err, "database", dbname)
191194
}
192195

193196
var sysdba string
194-
if err := db.QueryRow("select sys_context('USERENV', 'ISDBA') from dual").Scan(&sysdba); err != nil {
197+
if err := db.QueryRowContext(ctx, "select sys_context('USERENV', 'ISDBA') from dual").Scan(&sysdba); err != nil {
195198
logger.Error("error checking my database role", "error", err, "database", dbname)
196199
}
197200
logger.Info("Connected as SYSDBA? "+sysdba, "database", dbname)

0 commit comments

Comments
 (0)