Skip to content

Commit 59f338b

Browse files
authored
Merge pull request rails#46265 from bdewater/query-logs-docs [ci-skip]
Update Active Record Query Logs docs
2 parents 7779b5e + 7a56b76 commit 59f338b

File tree

2 files changed

+64
-38
lines changed

2 files changed

+64
-38
lines changed

activerecord/lib/active_record/query_logs.rb

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,56 +6,59 @@
66
module ActiveRecord
77
# = Active Record Query Logs
88
#
9-
# Automatically tag SQL queries with runtime information.
9+
# Automatically append comments to SQL queries with runtime information tags. This can be used to trace troublesome
10+
# SQL statements back to the application code that generated these statements.
1011
#
11-
# Default tags available for use:
12+
# Query logs can be enabled via Rails configuration in <tt>config/application.rb</tt> or an initializer:
13+
#
14+
# config.active_record.query_log_tags_enabled = true
15+
#
16+
# By default the name of the application, the name and action of the controller, or the name of the job are logged.
17+
# The default format is {SQLCommenter}[https://open-telemetry.github.io/opentelemetry-sqlcommenter/].
18+
# The tags shown in a query comment can be configured via Rails configuration:
19+
#
20+
# config.active_record.query_log_tags = [ :application, :controller, :action, :job ]
21+
#
22+
# Active Record defines default tags available for use:
1223
#
1324
# * +application+
1425
# * +pid+
1526
# * +socket+
1627
# * +db_host+
1728
# * +database+
1829
#
19-
# _Action Controller and Active Job tags are also defined when used in Rails:_
30+
# Action Controller adds default tags when loaded:
2031
#
2132
# * +controller+
2233
# * +action+
23-
# * +job+
24-
#
25-
# The tags used in a query can be configured directly:
34+
# * +namespaced_controller+
2635
#
27-
# ActiveRecord::QueryLogs.tags = [ :application, :controller, :action, :job ]
36+
# Active Job adds default tags when loaded:
2837
#
29-
# or via Rails configuration:
30-
#
31-
# config.active_record.query_log_tags = [ :application, :controller, :action, :job ]
38+
# * +job+
3239
#
33-
# To add new comment tags, add a hash to the tags array containing the keys and values you
34-
# want to add to the comment. Dynamic content can be created by setting a proc or lambda value in a hash,
35-
# and can reference any value stored in the +context+ object.
40+
# New comment tags can be defined by adding them in a +Hash+ to the tags +Array+. Tags can have dynamic content by
41+
# setting a +Proc+ or lambda value in the +Hash+, and can reference any value stored by Rails in the +context+ object.
42+
# ActiveSupport::CurrentAttributes can be used to store application values. Tags with +nil+ values are
43+
# omitted from the query comment.
3644
#
3745
# Example:
3846
#
39-
# tags = [
40-
# :application,
41-
# {
42-
# custom_tag: ->(context) { context[:controller]&.controller_name },
43-
# custom_value: -> { Custom.value },
44-
# }
45-
# ]
46-
# ActiveRecord::QueryLogs.tags = tags
47-
#
48-
# The QueryLogs +context+ can be manipulated via the +ActiveSupport::ExecutionContext.set+ method.
49-
#
50-
# Temporary updates limited to the execution of a block:
51-
#
52-
# ActiveSupport::ExecutionContext.set(foo: Bar.new) do
53-
# posts = Post.all
54-
# end
55-
#
56-
# Direct updates to a context value:
57-
#
58-
# ActiveSupport::ExecutionContext[:foo] = Bar.new
47+
# config.active_record.query_log_tags = [
48+
# :namespaced_controller,
49+
# :action,
50+
# :job,
51+
# {
52+
# request_id: ->(context) { context[:controller]&.request&.request_id },
53+
# job_id: ->(context) { context[:job]&.job_id },
54+
# tenant_id: -> { Current.tenant&.id },
55+
# static: "value",
56+
# },
57+
# ]
58+
#
59+
# By default the name of the application, the name and action of the controller, or the name of the job are logged
60+
# using the {SQLCommenter}[https://open-telemetry.github.io/opentelemetry-sqlcommenter/] format. This can be changed
61+
# via {config.active_record.query_log_tags_format}[https://guides.rubyonrails.org/configuring.html#config-active-record-query-log-tags-format]
5962
#
6063
# Tag comments can be prepended to the query:
6164
#
@@ -64,10 +67,6 @@ module ActiveRecord
6467
# For applications where the content will not change during the lifetime of
6568
# the request or job execution, the tags can be cached for reuse in every query:
6669
#
67-
# ActiveRecord::QueryLogs.cache_query_log_tags = true
68-
#
69-
# This option can be set during application configuration or in a Rails initializer:
70-
#
7170
# config.active_record.cache_query_log_tags = true
7271
module QueryLogs
7372
mattr_accessor :taggings, instance_accessor: false, default: {}

guides/source/debugging_rails_applications.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,34 @@ Below each database statement you can see arrows pointing to the specific source
236236

237237
Verbose query logs are enabled by default in the development environment logs after Rails 5.2.
238238

239-
WARNING: We recommend against using this setting in production environments. It relies on Ruby's `Kernel#caller` method which tends to allocate a lot of memory in order to generate stacktraces of method calls.
239+
WARNING: We recommend against using this setting in production environments. It relies on Ruby's `Kernel#caller` method which tends to allocate a lot of memory in order to generate stacktraces of method calls. Use query log tags (see below) instead.
240+
241+
SQL Query Comments
242+
------------------
243+
244+
SQL statements can be commented with tags containing runtime information, such as the name of the controller or job, to
245+
trace troublesome queries back to the area of the application that generated these statements. This is useful when you are
246+
logging slow queries (e.g. [MySQL](https://dev.mysql.com/doc/refman/en/slow-query-log.html), [PostgreSQL](https://www.postgresql.org/docs/current/runtime-config-logging.html#GUC-LOG-MIN-DURATION-STATEMENT)),
247+
viewing currently running queries, or for end-to-end tracing tools.
248+
249+
To enable, add in `application.rb` or any environment initializer:
250+
251+
```rb
252+
config.active_record.query_log_tags_enabled = true
253+
```
254+
255+
By default the name of the application, the name and action of the controller, or the name of the job are logged. The
256+
default format is [SQLCommenter](https://open-telemetry.github.io/opentelemetry-sqlcommenter/). For example:
257+
258+
```
259+
Article Load (0.2ms) SELECT "articles".* FROM "articles" /*application='Blog',controller='articles',action='index'*/
260+
261+
Article Update (0.3ms) UPDATE "articles" SET "title" = ?, "updated_at" = ? WHERE "posts"."id" = ? /*application='Blog',job='ImproveTitleJob'*/ [["title", "Improved Rails debugging guide"], ["updated_at", "2022-10-16 20:25:40.091371"], ["id", 1]]
262+
```
263+
264+
The behaviour of [`ActiveRecord::QueryLogs`](https://api.rubyonrails.org/classes/ActiveRecord/QueryLogs.html) can be
265+
modified to include anything that helps connect the dots from the SQL query, such as request and job ids for
266+
application logs, account and tenant identifiers, etc.
240267

241268
### Tagged Logging
242269

0 commit comments

Comments
 (0)