Skip to content

Instantly share code, notes, and snippets.

@jkarmel
Last active August 29, 2015 14:03
Show Gist options
  • Save jkarmel/919776c4b8a2937013ed to your computer and use it in GitHub Desktop.
Save jkarmel/919776c4b8a2937013ed to your computer and use it in GitHub Desktop.
module Profiling
class CallStackTracker
cattr_accessor :query_call_stacks do
[]
end
IGNORED_SQL = [/^PRAGMA (?!(table_info))/, /^SELECT currval/, /^SELECT CAST/, /^SELECT @@IDENTITY/, /^SELECT @@ROWCOUNT/, /^SAVEPOINT/, /^ROLLBACK TO SAVEPOINT/, /^RELEASE SAVEPOINT/, /^SHOW max_identifier_length/]
def call(name, start, finish, message_id, values)
# FIXME: this seems bad. we should probably have a better way to indicate
# the query was cached
unless 'CACHE' == values[:name]
self.class.query_call_stacks << caller unless IGNORED_SQL.any? { |r| values[:sql] =~ r }
end
end
end
def self.query_call_stacks(&block)
ActiveRecord::QueryCounter.query_call_stacks = []
yield
ActiveRecord::QueryCounter.query_call_stacks
end
end
ActiveSupport::Notifications.subscribe('sql.active_record', Profiling::CallStackTracker.new)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment