fix(monitor): DB-accurate max_age metrics#89
Merged
smudge merged 1 commit intoBetterment:mainfrom Feb 5, 2026
Merged
Conversation
smudge
commented
Feb 5, 2026
smudge
commented
Feb 5, 2026
Member
Author
There was a problem hiding this comment.
Probably best to view this diff with ?w=1 in the URL (to skip whitespace-only changes).
smudge
commented
Feb 5, 2026
|
|
||
| match do |block| | ||
| @expected = { event_name: expected_event_name, payload: expected_payload, value: expected_value } | ||
| if @approximately && current_adapter != 'postgresql' |
Member
Author
There was a problem hiding this comment.
On postgres, we can fully align Timecop with the transaction's NOW() -- on other DB engines, we need to account for slight variance in query time.
smudge
commented
Feb 5, 2026
|
|
||
| db_adapter = ENV["ADAPTER"] | ||
| gemfile = ENV["BUNDLE_GEMFILE"] | ||
| db_adapter ||= gemfile && gemfile[%r{gemfiles/(.*?)/}] && $1 # rubocop:disable Style/PerlBackrefs |
Member
Author
There was a problem hiding this comment.
I removed this line entirely as we no longer rely on adapter-specific gemfiles.
5950a87 to
39332a5
Compare
This ensures that max_lock_age and max_age are not artificially inflated by slower query times. Previously, if the query took N seconds to perform, N seconds would be artificially added to `max_age` and `max_lock_age`. Now, we rely on the DB's own reported UTC time at the time of the query. In PostgreSQL, this will correspond to the start of the transaction, and in other DB engines this will correspond to the DB's clock time as the query executes, so there is still some small variance but it is much smaller than before. /no-platform
39332a5 to
b52bcfa
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This ensures that
max_lock_ageandmax_ageare not artificially inflated by slower query times. Previously, if the query took N seconds to perform, N seconds would be artificially added tomax_ageandmax_lock_age. (Typically this is not a problem for sub-second queries on small-medium tables, but on large tables this can trigger occasional alert thresholds on sensitive/high-priority queues.)With this PR, we rely on the DB's own reported UTC time at the time of the query to compute timestamp ages as of said query.
In PostgreSQL, the DB's "now" will correspond to the start of the transaction (as should the state of the rows it returns), and in other DB engines it will correspond to a clock time during execution, so there is still room for variance but it is much smaller than before.
/no-platform