diff --git a/query.go b/query.go index d07817e0..205312ad 100644 --- a/query.go +++ b/query.go @@ -125,6 +125,10 @@ func (q *Query) Collect(ctx context.Context, conn *sql.DB, ch chan<- Metric) { // run executes the query on the provided database, in the provided context. func (q *Query) run(ctx context.Context, conn *sql.DB) (*sql.Rows, errors.WithContext) { + if conn == nil { + return nil, errors.Errorf(q.logContext, "nil database connection") + } + if slog.Default().Enabled(ctx, slog.LevelDebug) { start := time.Now() defer func() { diff --git a/target.go b/target.go index 8c05d285..8aedd042 100644 --- a/target.go +++ b/target.go @@ -136,12 +136,13 @@ func (t *target) Collect(ctx context.Context, ch chan<- Metric) { var wg sync.WaitGroup // Don't bother with the collectors if target is down. if targetUp { + conn := t.conn wg.Add(len(t.collectors)) for _, c := range t.collectors { // If using a single DB connection, collectors will likely run sequentially anyway. But we might have more. go func(collector Collector) { defer wg.Done() - collector.Collect(ctx, t.conn, ch) + collector.Collect(ctx, conn, ch) }(c) } }