Permalink
Browse files

Added basic filtering for Rails' SQL stack traces

  • Loading branch information...
1 parent 3126f89 commit a6005ff90f111fe4588894f01e08f97f1914adf1 @eviltrout eviltrout committed Apr 18, 2012
@@ -29,7 +29,9 @@ def self.configuration_defaults
:auto_inject => true, # automatically inject on every html page
:base_url_path => "/mini-profiler-resources/",
:authorize_cb => lambda {|env| true}, # callback returns true if this request is authorized to profile
- :position => 'left' # Where it is displayed
+ :position => 'left', # Where it is displayed
+ :backtrace_remove => nil,
+ :backtrace_filter => nil
}
end
@@ -6,9 +6,21 @@ class MiniProfiler
# Timing system for a SQL query
class SqlTimerStruct < TimerStruct
def initialize(query, duration_ms, page)
+
+ # Allow us to filter the stack trace
+ stack_trace = ""
+ # Clean up the stack trace if there are options to do so
+ Kernel.caller.each do |ln|
+ ln.gsub!(Rack::MiniProfiler.configuration[:backtrace_remove], '') if Rack::MiniProfiler.configuration[:backtrace_remove]
+ if Rack::MiniProfiler.configuration[:backtrace_filter].nil? or ln =~ Rack::MiniProfiler.configuration[:backtrace_filter]
+ stack_trace << ln << "\n"
+ end
+ end
+
+
super("ExecuteType" => 3, # TODO
"FormattedCommandString" => query,
- "StackTraceSnippet" => Kernel.caller.join("\n"), # TODO
+ "StackTraceSnippet" => stack_trace,
"StartMilliseconds" => (Time.now.to_f * 1000).to_i - page['Started'],
"DurationMilliseconds" => duration_ms,
"FirstFetchDurationMilliseconds" => 0,
@@ -6,13 +6,18 @@ class Railtie < ::Rails::Railtie
# By default, only show the MiniProfiler in development mode
Rack::MiniProfiler.configuration[:authorize_cb] = lambda {|env| Rails.env.development? }
+ # Quiet the SQL stack traces
+ Rack::MiniProfiler.configuration[:backtrace_remove] = Rails.root.to_s + "/"
+ Rack::MiniProfiler.configuration[:backtrace_filter] = /^\/?(app|config|lib|test)/
+
# Install the Middleware
app.middleware.use Rack::MiniProfiler
# Attach to various Rails methods
::Rack::MiniProfiler.profile_method(ActionController::Base, :process) {|action| "Executing action: #{action}"}
::Rack::MiniProfiler.profile_method(ActionView::Template, :render) {|x,y| "Rendering: #{@virtual_path}"}
+
end
end
@@ -3,33 +3,61 @@
describe Rack::MiniProfiler::SqlTimerStruct do
- before do
- @sql = Rack::MiniProfiler::SqlTimerStruct.new("SELECT * FROM users", 200, Rack::MiniProfiler::PageTimerStruct.new({}))
+ describe 'valid sql timer' do
+ before do
+ @sql = Rack::MiniProfiler::SqlTimerStruct.new("SELECT * FROM users", 200, Rack::MiniProfiler::PageTimerStruct.new({}))
+ end
+
+ it 'has an ExecuteType' do
+ @sql['ExecuteType'].should_not be_nil
+ end
+
+ it 'has a FormattedCommandString' do
+ @sql['FormattedCommandString'].should_not be_nil
+ end
+
+ it 'has a StackTraceSnippet' do
+ @sql['StackTraceSnippet'].should_not be_nil
+ end
+
+ it 'has a StartMilliseconds' do
+ @sql['StartMilliseconds'].should_not be_nil
+ end
+
+ it 'has a DurationMilliseconds' do
+ @sql['DurationMilliseconds'].should_not be_nil
+ end
+
+ it 'has a IsDuplicate' do
+ @sql['IsDuplicate'].should_not be_nil
+ end
end
- it 'has an ExecuteType' do
- @sql['ExecuteType'].should_not be_nil
- end
-
- it 'has a FormattedCommandString' do
- @sql['FormattedCommandString'].should_not be_nil
- end
-
- it 'has a StackTraceSnippet' do
- @sql['StackTraceSnippet'].should_not be_nil
- end
- it 'has a StartMilliseconds' do
- @sql['StartMilliseconds'].should_not be_nil
- end
-
- it 'has a DurationMilliseconds' do
- @sql['DurationMilliseconds'].should_not be_nil
- end
-
- it 'has a IsDuplicate' do
- @sql['IsDuplicate'].should_not be_nil
- end
+ describe 'backtrace' do
+ it 'has a snippet' do
+ sql = Rack::MiniProfiler::SqlTimerStruct.new("SELECT * FROM users", 200, Rack::MiniProfiler::PageTimerStruct.new({}))
+ sql['StackTraceSnippet'].should_not be nil
+ end
+
+ it 'includes rspec in the trace (default is no filter)' do
+ sql = Rack::MiniProfiler::SqlTimerStruct.new("SELECT * FROM users", 200, Rack::MiniProfiler::PageTimerStruct.new({}))
+ sql['StackTraceSnippet'].should match /rspec/
+ end
+
+ it "doesn't include rspec if we filter for only app" do
+ Rack::MiniProfiler.configuration[:backtrace_filter] = /\/app/
+ sql = Rack::MiniProfiler::SqlTimerStruct.new("SELECT * FROM users", 200, Rack::MiniProfiler::PageTimerStruct.new({}))
+ sql['StackTraceSnippet'].should_not match /rspec/
+ end
+
+ it "includes rspec if we filter for it" do
+ Rack::MiniProfiler.configuration[:backtrace_filter] = /\/(app|rspec)/
+ sql = Rack::MiniProfiler::SqlTimerStruct.new("SELECT * FROM users", 200, Rack::MiniProfiler::PageTimerStruct.new({}))
+ sql['StackTraceSnippet'].should match /rspec/
+ end
+
+ end
end

0 comments on commit a6005ff

Please sign in to comment.