Skip to content

Commit

Permalink
FEATURE: Allow skip_paths config to contain regular expressions (#461)
Browse files Browse the repository at this point in the history
  • Loading branch information
OsamaSayegh committed Sep 3, 2020
1 parent 8fbc5bf commit 264b2e0
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -365,7 +365,7 @@ Option|Default|Description
-------|---|--------
pre_authorize_cb|Rails: dev only<br>Rack: always on|A lambda callback that returns true to make mini_profiler visible on a given request.
position|`'top-left'`|Display mini_profiler on `'top-right'`, `'top-left'`, `'bottom-right'` or `'bottom-left'`.
skip_paths|`[]`|Paths that skip profiling.
skip_paths|`[]`|An array of paths that skip profiling. Both `String` and `Regexp` are acceptable in the array.
skip_schema_queries|Rails dev: `true`<br>Othwerwise: `false`|`true` to skip schema queries.
auto_inject|`true`|`true` to inject the miniprofiler script in the page.
backtrace_ignores|`[]`|Regexes of lines to be removed from backtraces.
Expand Down
12 changes: 10 additions & 2 deletions lib/mini_profiler/profiler.rb
Expand Up @@ -209,8 +209,16 @@ def call(env)
# Someone (e.g. Rails engine) could change the SCRIPT_NAME so we save it
env['RACK_MINI_PROFILER_ORIGINAL_SCRIPT_NAME'] = ENV['PASSENGER_BASE_URI'] || env['SCRIPT_NAME']

skip_it = (@config.skip_paths && @config.skip_paths.any? { |p| path.start_with?(p) }) ||
query_string =~ /pp=skip/
skip_it = /pp=skip/.match?(query_string) || (
@config.skip_paths &&
@config.skip_paths.any? do |p|
if p.instance_of?(String)
path.start_with?(p)
elsif p.instance_of?(Regexp)
p.match?(path)
end
end
)
if skip_it
return client_settings.handle_cookie(@app.call(env))
end
Expand Down
8 changes: 8 additions & 0 deletions spec/integration/mini_profiler_spec.rb
Expand Up @@ -257,6 +257,14 @@ def app
expect(last_response.headers.has_key?('X-MiniProfiler-Ids')).to be(true)
end

it "skip_paths can contain regular expressions" do
Rack::MiniProfiler.config.skip_paths = [/path[^1]/]
get '/path2/a'
expect(last_response.headers.has_key?('X-MiniProfiler-Ids')).to be(false)
get '/path1/a'
expect(last_response.headers.has_key?('X-MiniProfiler-Ids')).to be(true)
end

it 'disables default functionality' do
Rack::MiniProfiler.config.enabled = false
get '/html'
Expand Down
2 changes: 1 addition & 1 deletion spec/support/common_store_spec.rb
Expand Up @@ -119,7 +119,7 @@
page = Rack::MiniProfiler::TimerStruct::Page.new({})
page[:root].record_time(400)
store.push_snapshot(page, Rack::MiniProfiler::Config.default)

loaded = store.load_snapshot(page[:id])
expect(loaded).to be_instance_of(Rack::MiniProfiler::TimerStruct::Page)
expect(loaded[:id]).to eq(page[:id])
Expand Down
4 changes: 2 additions & 2 deletions website/sample.rb
Expand Up @@ -19,7 +19,7 @@ def load(*args)
@page_struct
end
alias_method :load_snapshot, :load

def save(*args)
end

Expand Down Expand Up @@ -49,7 +49,7 @@ def fetch_snapshots(batch_size: 200, &blk)
methods.sample,
paths.sample,
SecureRandom.rand * @multipliers.sample,
((Time.new.to_f - @time_units.sample * @time_multipliers.sample) * 1000).round
((Time.now.to_f - @time_units.sample * @time_multipliers.sample) * 1000).round
)
end
@snapshots.each_slice(batch_size) { |batch| blk.call(batch) }
Expand Down

0 comments on commit 264b2e0

Please sign in to comment.