Skip to content

Instrument connection pool checkout waits - #59308

Merged
byroot merged 1 commit into
rails:mainfrom
andrewfader:ar-connection-pool-checkout-wait
Oct 31, 2026
Merged

byroot merged 1 commit into
rails:mainfrom
andrewfader:ar-connection-pool-checkout-wait

Conversation

@andrewfader

@andrewfader andrewfader commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Motivation / Background

When every connection in a pool is leased, checkout blocks for up to checkout_timeout. Nothing reports that wait: request timing shows it as unexplained time, and the first signal of a saturated pool is a ConnectionTimeoutError. Measuring it today means prepending onto ConnectionPool#checkout.

Detail

acquire_connection now instruments its blocking poll as checkout_wait.active_record, with :connection_pool and :checkout_timeout in the payload. The event's duration is the wait; a checkout that times out still emits it, with the error under :exception.

It fires only on the path that blocks on a full pool, after the idle-connection, background-connection, and new-connection attempts have failed, so ordinary checkouts are unaffected. It is emitted outside the pool's lock, except when a checkout falls back to the pool after its pinned connection was discarded.

Documented in the instrumentation guide.

Checklist

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@andrewfader
andrewfader force-pushed the ar-connection-pool-checkout-wait branch 3 times, most recently from e1eb4ab to 29b7dc9 Compare October 29, 2026 17:54
@byroot

byroot commented Oct 31, 2026

Copy link
Copy Markdown
Member

This makes sense to me, but that event need to be added to guides/source/active_support_instrumentation.md.

Active Record emits no event when a checkout blocks on a full pool, so
the only signal of pool saturation is the ConnectionTimeoutError at the
end of the wait. Instrument the wait as checkout_wait.active_record,
with the pool and checkout_timeout in the payload. The event fires only
on the blocking path, so a checkout served by an idle or new connection
pays nothing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@andrewfader
andrewfader force-pushed the ar-connection-pool-checkout-wait branch from 29b7dc9 to 0cbab48 Compare October 31, 2026 19:13
@andrewfader

andrewfader commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor Author

@byroot
byroot merged commit 485cda7 into rails:main Oct 31, 2026
4 checks passed
@andrewfader
andrewfader deleted the ar-connection-pool-checkout-wait branch October 31, 2026 20:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants