-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add row_count field to sql.active_record notification #50887
Merged
rafaelfranca
merged 1 commit into
rails:main
from
marvin-bitterlich:marvin.bitterlich/result-set
Jan 29, 2024
Merged
Add row_count field to sql.active_record notification #50887
rafaelfranca
merged 1 commit into
rails:main
from
marvin-bitterlich:marvin.bitterlich/result-set
Jan 29, 2024
+70
−12
Conversation
This file contains 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
e077e76
to
4e57153
Compare
eugeneius
reviewed
Jan 26, 2024
activerecord/lib/active_record/connection_adapters/mysql2/database_statements.rb
Show resolved
Hide resolved
eugeneius
reviewed
Jan 26, 2024
4e57153
to
66f2da0
Compare
eugeneius
reviewed
Jan 26, 2024
activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb
Outdated
Show resolved
Hide resolved
activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb
Outdated
Show resolved
Hide resolved
activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
Outdated
Show resolved
Hide resolved
activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
Outdated
Show resolved
Hide resolved
activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb
Outdated
Show resolved
Hide resolved
activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb
Outdated
Show resolved
Hide resolved
activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb
Outdated
Show resolved
Hide resolved
activerecord/lib/active_record/connection_adapters/trilogy/database_statements.rb
Outdated
Show resolved
Hide resolved
66f2da0
to
4b32094
Compare
This field returns the amount of rows returned by the query that emitted the notification. This metric is useful in cases where one wants to detect queries with big result sets.
4b32094
to
e9a2288
Compare
eugeneius
approved these changes
Jan 29, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me, but I'll leave it open a bit longer in case others have feedback.
4 tasks
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.
Motivation / Background
In Intercom we are working on detecting queries with large result sets, as Vitess based databases (like in our case Planetscale) block queries that return more than (by default) 100k result set rows.
We have an internal patch for this that works well for us, but thought to contribute this upstream, so others can benefit
Detail
This Pull Request changes
{*}Adapter.log
, which emitssql.active_record
notifications.Previously the instrument call only logs information that goes into a request, but allows the caller inside the block to modify the payload. We make use of this inside each adapter to add a new field to each
sql.active_record
notification with the number of result rows being returned in each query.This allows consumers of this notification (example) to consume this metric.
There is previous work in this regard with the
RecordFetchWarning
module that can be included, which demonstrates the need to monitor queries with large result sets. In our case these log lines weren't easy to integrate into our observability stack, and having a numerical value attached to each sql trace allowed us to use the full power of our observability suite.This pr also includes three tests in the instrumentation test suite that verify this working for model instanciation queries, direct value queries like
pluck
and rawActiveRecord::Base.connection.execute(sql)
queries.Performance
The change asks the resultset how many elements it has. Expectation was that that should be a constant lookup, and benchmarks suggest this to be true:
I have tested this change with
examples/performance
on adapterssqlite3
,mysql2
andpostgresql
with no measurable difference in performance (less than 0.1% when standard deviation was 1%).Additional information
I originally approached this inside
abstract_adapter
but it turns out every adapters Result uses a different method, so I moved the call into each adapters respective usage oflog
RecordFetchWarning
works by patchingexec_queries
, so it aggregates the result set size of multiple queries into one, which is less helpful for this case, as the limits imposed by Vitess are per query. Additionally, this does not support the case of executing raw SQL on the connection, which is something that this patch captures.As for the field name on the payload
row_count
seemed like a good name as it follows the same pattern asresult_count
does in https://guides.rubyonrails.org/active_support_instrumentation.html#instantiation-active-recordChecklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]