Skip to content

Commit 0602e1e

Browse files
authored
Merge pull request rails#50969 from fatkodima/query_logs-line
Support `:source_location` tag option for query log tags
2 parents 554e5c2 + a87668a commit 0602e1e

File tree

5 files changed

+49
-4
lines changed

5 files changed

+49
-4
lines changed

activerecord/CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
* Support `:source_location` tag option for query log tags
2+
3+
```ruby
4+
config.active_record.query_log_tags << :source_location
5+
```
6+
7+
Calculating the caller location is a costly operation and should be used primarily in development
8+
(note, there is also a `config.active_record.verbose_query_logs` that serves the same purpose)
9+
or occasionally on production for debugging purposes.
10+
11+
*fatkodima*
12+
113
* Add an option to `ActiveRecord::Encryption::Encryptor` to disable compression
214

315
Allow compression to be disabled by setting `compress: false`

activerecord/lib/active_record/query_logs.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module ActiveRecord
2626
# * +socket+
2727
# * +db_host+
2828
# * +database+
29+
# * +source_location+
2930
#
3031
# Action Controller adds default tags when loaded:
3132
#
@@ -108,6 +109,20 @@ def update_formatter(format)
108109
end
109110
end
110111

112+
if Thread.respond_to?(:each_caller_location)
113+
def query_source_location # :nodoc:
114+
Thread.each_caller_location do |location|
115+
frame = LogSubscriber.backtrace_cleaner.clean_frame(location.path)
116+
return frame if frame
117+
end
118+
nil
119+
end
120+
else
121+
def query_source_location # :nodoc:
122+
LogSubscriber.backtrace_cleaner.clean(caller_locations(1).each).first
123+
end
124+
end
125+
111126
ActiveSupport::ExecutionContext.after_change { ActiveRecord::QueryLogs.clear_cache }
112127

113128
private

activerecord/lib/active_record/railtie.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,8 @@ class Railtie < Rails::Railtie # :nodoc:
415415
pid: -> { Process.pid.to_s },
416416
socket: ->(context) { context[:connection].pool.db_config.socket },
417417
db_host: ->(context) { context[:connection].pool.db_config.host },
418-
database: ->(context) { context[:connection].pool.db_config.database }
418+
database: ->(context) { context[:connection].pool.db_config.database },
419+
source_location: -> { QueryLogs.query_source_location }
419420
)
420421
ActiveRecord.disable_prepared_statements = true
421422

guides/source/configuring.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,9 +1411,9 @@ NOTE: When this is set to `true` database prepared statements will be automatica
14111411
14121412
#### `config.active_record.query_log_tags`
14131413
1414-
Define an `Array` specifying the key/value tags to be inserted in an SQL
1415-
comment. Defaults to `[ :application ]`, a predefined tag returning the
1416-
application name.
1414+
Define an `Array` specifying the key/value tags to be inserted in an SQL comment. Defaults to
1415+
`[ :application, :controller, :action, :job ]`. The available tags are: `:application`, `:controller`,
1416+
`:namespaced_controller`, `:action`, `:job`, and `:source_location`.
14171417
14181418
#### `config.active_record.query_log_tags_format`
14191419

railties/test/application/query_logs_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,23 @@ def app
149149
assert_equal("/*action='index',controller='users',database='storage%2Fproduction_animals.sqlite3'*/", comment)
150150
end
151151

152+
test "source_location information is added if enabled" do
153+
add_to_config <<~RUBY
154+
config.active_record.query_log_tags_enabled = true
155+
config.active_record.query_log_tags = [ :source_location ]
156+
157+
# Remove silencers, so we won't get all backtrace lines filtered.
158+
Rails.backtrace_cleaner.remove_silencers!
159+
RUBY
160+
161+
boot_app
162+
163+
get "/", {}, { "HTTPS" => "on" }
164+
comment = last_response.body.strip
165+
166+
assert_match(/source_location='.*'/, comment)
167+
end
168+
152169
test "controller tags are not doubled up if already configured" do
153170
add_to_config "config.active_record.query_log_tags_enabled = true"
154171
add_to_config "config.active_record.query_log_tags = [ :action, :job, :controller, :pid ]"

0 commit comments

Comments
 (0)