Skip to content

Instantly share code, notes, and snippets.

@basiszwo
Created April 23, 2018 13:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save basiszwo/b84fc49ce254f75ff0e6b658e1b0def8 to your computer and use it in GitHub Desktop.
Save basiszwo/b84fc49ce254f75ff0e6b658e1b0def8 to your computer and use it in GitHub Desktop.
Find active record query location
module ActiveRecord
class QueryLocator
def call(name, started, finished, unique_id, payload)
# filter caller locations to application backtrace (filter all gem calls)
# see http://ruby-doc.org/core-2.5.0/Kernel.html#method-i-caller_locations for more options
caller_names = caller_locations.select { |line| line.to_s =~ /#{Rails.root.to_s}/ }
# use the last caller in backtrace
caller_name = caller_names.first
Rails.logger.debug ["Notification:", name, started, finished, unique_id, caller_name, payload].join(" - ")
end
end
end
if Rails.configuration.active_record_query_locator
Rails.logger.info "Registering ActiveRecord QueryLocator ..."
ActiveSupport::Notifications.subscribe('sql.active_record', ActiveRecord::QueryLocator.new)
end
# Add this to your application configuration in config/application.rb
# and / or config/environments/<RAILS ENV>.rb
# Activate active record query locator
config.active_record_query_locator = true
Notification: - sql.active_record - 2018-04-23 14:12:07 +0200 - 2018-04-23 14:12:07 +0200 - 192de139b90a62d66b18 - /path/to/rails/root/app/exporter/pdf_price.rb:149:in `price_info' - {:sql=>"SELECT \"calculations\".* FROM \"calculations\" WHERE \"calculations\".\"id\" = 1238", :name=>"Calculation Load", :binds=>[], :type_casted_binds=>[], :statement_name=>nil, :connection_id=>70109843746700}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment