Skip to content

Support optional health check queries in MultiActiveRecordCheck#55

Open
mdkent wants to merge 2 commits into
masterfrom
multi-check-health-query
Open

Support optional health check queries in MultiActiveRecordCheck#55
mdkent wants to merge 2 commits into
masterfrom
multi-check-health-query

Conversation

@mdkent

@mdkent mdkent commented Jun 5, 2026

Copy link
Copy Markdown
Member

Summary

Easymon::MultiActiveRecordCheck previously determined database health solely via connection.active?. This adds support for an optional health check query that returns a single truthy/falsey value (e.g. 1/0 from MySQL), useful for richer checks like read-only state or replication lag.

Two ways to supply a query:

Default for all connections via the query: option:

check = Easymon::MultiActiveRecordCheck.new(query: "SELECT @@read_only") {
  { "Primary": ActiveRecord::Base.connection }
}

Per connection, by pairing a connection with its query (takes precedence over the default):

check = Easymon::MultiActiveRecordCheck.new {
  {
    "Primary": [ActiveRecord::Base.connection, "SELECT @@read_only"],
    "PrimaryReplica": [Easymon::PrimaryReplica.connection, "SELECT TIMESTAMPDIFF(MICROSECOND, MAX(ts), NOW(6)) / 1000000 > 1 FROM percona.heartbeat"],
    "OtherReplica": Easymon::OtherReplica.connection # plain connection.active?
  }
}

Query results are cast with ActiveModel::Type::Boolean, so MySQL's 1/0 map to Up/Down and an empty result (nil) is treated as Down. Any raise during the query still rescues to Down, and existing callers without queries are unchanged.

Testing

  • Added unit tests covering the default query, per-connection queries, precedence, false results, and raised errors
  • bin/rails test test/unit/checks/multi_active_record_check_test.rb — 9 runs, 0 failures

🤖 Generated with Claude Code

Instead of relying solely on connection.active?, a health check query
returning a single truthy/falsey value (e.g. 1/0 from MySQL) can be
supplied — either as a default for all connections via the query:
option, or per connection by pairing a connection with its query in
the block's hash. Per-connection queries take precedence over the
default, and plain connections continue to use connection.active?.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings June 5, 2026 23:05

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds support for running an optional per-connection (or default) SQL health-check query in Easymon::MultiActiveRecordCheck, allowing richer DB health semantics than connection.active? alone (e.g., read-only state checks).

Changes:

  • Add query: support to MultiActiveRecordCheck, including per-connection query overrides and boolean casting of query results.
  • Extend unit tests to cover query-based checks, precedence rules, falsey results, and raised errors.

Tip

If you aren't ready for review, convert to a draft PR.
Click "Convert to draft" or run gh pr ready --undo.
Click "Ready for review" or run gh pr ready to reengage.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
lib/easymon/checks/multi_active_record_check.rb Adds optional query-based health checking (default and per-connection) and updates inline docs.
test/unit/checks/multi_active_record_check_test.rb Adds unit tests for query-based behavior and precedence.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread test/unit/checks/multi_active_record_check_test.rb
Comment thread test/unit/checks/multi_active_record_check_test.rb
Comment thread lib/easymon/checks/multi_active_record_check.rb
Comment thread lib/easymon/checks/multi_active_record_check.rb
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants