From 264b2e026864a71d3344eccb03312a955724effc Mon Sep 17 00:00:00 2001 From: Osama Sayegh Date: Thu, 3 Sep 2020 08:55:55 +0300 Subject: [PATCH] FEATURE: Allow skip_paths config to contain regular expressions (#461) --- README.md | 2 +- lib/mini_profiler/profiler.rb | 12 ++++++++++-- spec/integration/mini_profiler_spec.rb | 8 ++++++++ spec/support/common_store_spec.rb | 2 +- website/sample.rb | 4 ++-- 5 files changed, 22 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 21798f71..d980f4b4 100644 --- a/README.md +++ b/README.md @@ -365,7 +365,7 @@ Option|Default|Description -------|---|-------- pre_authorize_cb|Rails: dev only
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`
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. diff --git a/lib/mini_profiler/profiler.rb b/lib/mini_profiler/profiler.rb index ea327edd..3ed1f76e 100644 --- a/lib/mini_profiler/profiler.rb +++ b/lib/mini_profiler/profiler.rb @@ -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 diff --git a/spec/integration/mini_profiler_spec.rb b/spec/integration/mini_profiler_spec.rb index 190af726..e67742a2 100644 --- a/spec/integration/mini_profiler_spec.rb +++ b/spec/integration/mini_profiler_spec.rb @@ -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' diff --git a/spec/support/common_store_spec.rb b/spec/support/common_store_spec.rb index a97ac580..4146800b 100644 --- a/spec/support/common_store_spec.rb +++ b/spec/support/common_store_spec.rb @@ -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]) diff --git a/website/sample.rb b/website/sample.rb index bd80f6ea..954a0fbc 100644 --- a/website/sample.rb +++ b/website/sample.rb @@ -19,7 +19,7 @@ def load(*args) @page_struct end alias_method :load_snapshot, :load - + def save(*args) end @@ -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) }