Skip to content
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

Support :source_location tag option for query log tags #50969

Merged
merged 1 commit into from
Feb 6, 2024

Conversation

fatkodima
Copy link
Member

@fatkodima fatkodima commented Feb 5, 2024

Follow up to #42240 (as discussed in #42240 (comment)).

We lost the ability to use the :line option with QueryLogs (#42240 (comment)), while there is one in the marginalia. It was costly to use it before in the gem, but we made it fast (basecamp/marginalia#138), so it is not that costly and people deliberately decide if they want to use it.

I would like to have the :line option (upd: decided to name it as :source_location), it is very much useful. Without it, if we have a slow db query log, for example, it is very hard to know from which part of the codebase the query was generated. Even having an :action component, you need to investigate the unfamiliar codebase to find out where the query was triggered.

cc @byroot

end
else
def query_source_location # :nodoc:
LogSubscriber.backtrace_cleaner.clean(caller(1).lazy).first
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this lazy make much sense. Might as well explicitly do caller_location(1).each.

if Thread.respond_to?(:each_caller_location)
def query_source_location # :nodoc:
Thread.each_caller_location do |location|
frame = LogSubscriber.backtrace_cleaner.clean_frame(location)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
frame = LogSubscriber.backtrace_cleaner.clean_frame(location)
frame = LogSubscriber.backtrace_cleaner.clean_frame(location.path)

Might as well pass only the path, no?

@@ -71,7 +72,10 @@ module ActiveRecord
#
# config.active_record.cache_query_log_tags = true
module QueryLogs
mattr_accessor :taggings, instance_accessor: false, default: {}
mattr_accessor :taggings, instance_accessor: false, default: {
line: -> { query_source_location }
Copy link
Member

Choose a reason for hiding this comment

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

This should be set in activerecord/railtie.rb like the others, otherwise it risks being overidden by users.

@fatkodima
Copy link
Member Author

Updated, please take a look.

Copy link
Member

@byroot byroot left a comment

Choose a reason for hiding this comment

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

Looks OK now, but there is a surprising amount of tests failures. I suspect they're not related but might be worth trying a rebase?

@@ -26,6 +26,7 @@ module ActiveRecord
# * +socket+
# * +db_host+
# * +database+
# * +line+
Copy link
Member

Choose a reason for hiding this comment

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

Also I wonder if line really makes sense as a name, as it's not just the line, but the whole location. Maybe location or source_location?

Copy link
Member Author

Choose a reason for hiding this comment

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

I named it after the marginalia's :line, but yeah, changed it to :source_location.
CI is still red, but looks like is not related to this PR.

@fatkodima fatkodima changed the title Support :line tag option for query log tags Support :source_location tag option for query log tags Feb 5, 2024
@byroot
Copy link
Member

byroot commented Feb 6, 2024

It's weird because main is green, and you got the same failures after your rebase. I'll try to check if main is hosed.

@byroot
Copy link
Member

byroot commented Feb 6, 2024

Yeah, similar failures on other PRs...

@byroot
Copy link
Member

byroot commented Feb 6, 2024

Nevermind, it's a minitest regression, @yahonda pined it in #50978 ❤️

@byroot byroot merged commit 0602e1e into rails:main Feb 6, 2024
3 of 4 checks passed
@fatkodima fatkodima deleted the query_logs-line branch February 6, 2024 10:05
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.

None yet

2 participants