Created
July 11, 2014 06:05
-
-
Save jkarmel/bec7f13d4be11e27c0a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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