Active Record Query Logs

Automatically tag SQL queries with runtime information.

Default tags available for use:

  • application

  • pid

  • socket

  • db_host

  • database

_Action Controller and Active Job tags are also defined when used in Rails:_

  • controller

  • action

  • job

The tags used in a query can be configured directly:

ActiveRecord::QueryLogs.tags = [ :application, :controller, :action, :job ]

or via Rails configuration:

config.active_record.query_log_tags = [ :application, :controller, :action, :job ]

To add new comment tags, add a hash to the tags array containing the keys and values you want to add to the comment. Dynamic content can be created by setting a proc or lambda value in a hash, and can reference any value stored in the context object.

Example:

tags = [
  :application,
  {
    custom_tag: ->(context) { context[:controller].controller_name },
    custom_value: -> { Custom.value },
  }
]
ActiveRecord::QueryLogs.tags = tags

The QueryLogs context can be manipulated via update_context & set_context methods.

Direct updates to a context value:

ActiveRecord::QueryLogs.update_context(foo: Bar.new)

Temporary updates limited to the execution of a block:

ActiveRecord::QueryLogs.set_context(foo: Bar.new) do
  posts = Post.all
end

Tag comments can be prepended to the query:

ActiveRecord::QueryLogs.prepend_comment = true

For applications where the content will not change during the lifetime of the request or job execution, the tags can be cached for reuse in every query:

ActiveRecord::QueryLogs.cache_query_log_tags = true

This option can be set during application configuration or in a Rails initializer:

config.active_record.cache_query_log_tags = true

Methods

Class Public methods

set_context(**options)

Updates the context used to construct tags in the SQL comment during execution of the provided block. Resets the provided keys to their previous value once the block exits.

# File activerecord/lib/active_record/query_logs.rb, line 104
def set_context(**options)
  keys = options.keys
  previous_context = keys.zip(context.values_at(*keys)).to_h
  update_context(**options)
  yield if block_given?
ensure
  update_context(**previous_context)
end

update_context(**options)

Updates the context used to construct tags in the SQL comment. Resets the cached comment if cache_query_log_tags is true.

# File activerecord/lib/active_record/query_logs.rb, line 96
def update_context(**options)
  context.merge!(**options.symbolize_keys)
  self.cached_comment = nil
end

with_tag(tag, &block)

Temporarily tag any query executed within `&block`. Can be nested.

# File activerecord/lib/active_record/query_logs.rb, line 114
def with_tag(tag, &block)
  inline_tags.push(tag)
  yield if block_given?
ensure
  inline_tags.pop
end