Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

memoized backtrace_cleaner instance (Fixes #12) #14

Merged
merged 1 commit into from
Sep 16, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/sql-logging/statistics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Statistics
@@show_sql_backtrace = true
@@show_top_sql_queries = :total_time
@@top_sql_queries = 10
@@backtrace_cleaner = nil

cattr_accessor :show_sql_backtrace, :top_sql_queries

Expand All @@ -35,8 +36,13 @@ def self.reset_statistics!
@@top_queries = {}
end

@@backtrace_cleaner = Rails.backtrace_cleaner.dup
@@backtrace_cleaner.add_silencer { |line| line =~ %r{sql-logging/lib} }
def self.backtrace_cleaner
unless @@backtrace_cleaner
@@backtrace_cleaner = Rails.backtrace_cleaner.dup
@@backtrace_cleaner.add_silencer { |line| line =~ %r{sql-logging/lib} }
end
@@backtrace_cleaner
end

def self.record_query(sql, name, msec, result)
unless name.blank? || name =~ / Columns$/ || name == :skip_logging
Expand Down Expand Up @@ -66,7 +72,7 @@ def self.record_query(sql, name, msec, result)
@@rows += ntuples
@@bytes += bytes

backtrace = @@backtrace_cleaner.clean(caller).join("\n ")
backtrace = backtrace_cleaner.clean(caller).join("\n ")
unless @@show_top_sql_queries == false
key = "#{name}:#{backtrace}"
unless query = @@top_queries[key]
Expand Down