Skip to content

Commit 29cf886

Browse files
committed
Allow callers from some files to be ignored
1 parent 1f1a4d3 commit 29cf886

File tree

6 files changed

+116
-8
lines changed

6 files changed

+116
-8
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ Select your editor on the extension options page: chrome://extensions. Following
3737
* Atom
3838
* RubyMine
3939

40+
## Meta Request Configuration
41+
42+
If the ActiveRecord caller locations are including files you don't want, you can exclude those files by adding them to the configuration in config/meta_request.yml. Files in the backtrace that match a regex in the ingore paths list will be skipped.
43+
44+
```yaml
45+
:ignore_paths:
46+
- .*/directory/exclude_this_file
47+
```
48+
4049
## Compatibility Warnings
4150
4251
If you're using [LiveReload](http://livereload.com/) or

meta_request/lib/meta_request.rb

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module MetaRequest
77
autoload :Middlewares, "meta_request/middlewares"
88
autoload :LogInterceptor, "meta_request/log_interceptor"
99
autoload :AppNotifications, "meta_request/app_notifications"
10+
autoload :Config, "meta_request/config"
1011

1112
def self.config
1213
@config ||= Config.new

meta_request/lib/meta_request/app_notifications.rb

+12-7
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class AppNotifications
2424
payload[:options][k] = payload.delete(k) unless k.in? CACHE_KEY_COLUMNS
2525
end
2626

27-
dev_caller = caller.detect { |c| c.include? MetaRequest.rails_root }
27+
dev_caller = caller.detect { |c| dev_caller?(c) }
2828
if dev_caller
2929
c = Callsite.parse(dev_caller)
3030
payload.merge!(:line => c.line, :filename => c.filename, :method => c.method)
@@ -42,12 +42,12 @@ class AppNotifications
4242
SQL_EVENT_NAME = "sql.active_record"
4343

4444
SQL_BLOCK = Proc.new {|*args|
45-
name, start, ending, transaction_id, payload = args
46-
dev_caller = caller.detect { |c| c.include? MetaRequest.rails_root }
47-
if dev_caller
48-
c = Callsite.parse(dev_caller)
49-
payload.merge!(:line => c.line, :filename => c.filename, :method => c.method)
50-
end
45+
name, start, ending, transaction_id, payload = args
46+
dev_caller = caller.detect { |c| dev_caller?(c) }
47+
if dev_caller
48+
c = Callsite.parse(dev_caller)
49+
payload.merge!(:line => c.line, :filename => c.filename, :method => c.method)
50+
end
5151
Event.new(SQL_EVENT_NAME, start, ending, transaction_id, payload)
5252
}
5353
# Subscribe to all events relevant to RailsPanel
@@ -81,6 +81,11 @@ def subscribe(event_name)
8181
self
8282
end
8383

84+
private
85+
86+
def self.dev_caller?(file_path)
87+
file_path.include?(MetaRequest.rails_root) && !Config.ignored_path?(file_path)
88+
end
8489
end
8590

8691
end

meta_request/lib/meta_request/config.rb

+31
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'yaml'
2+
13
module MetaRequest
24
class Config
35
# logger used for reporting gem's fatal errors
@@ -10,5 +12,34 @@ def logger
1012
def storage_pool_size
1113
@storage_pool_size ||= 20
1214
end
15+
16+
def self.ignored_path?(file_path)
17+
ingore_paths.any? { |ignore_path| ignore_path.match(file_path) }
18+
end
19+
20+
def self.config_file
21+
@@config_file ||= load_and_parse_config_file
22+
end
23+
24+
private
25+
26+
def self.ingore_paths
27+
config_file[:ignore_paths]
28+
end
29+
30+
def self.load_and_parse_config_file
31+
@@config_file = load_config_file
32+
@@config_file[:ignore_paths] ||= []
33+
@@config_file[:ignore_paths].map! do |ignore_path|
34+
Regexp.new(ignore_path)
35+
end
36+
@@config_file.freeze
37+
end
38+
39+
def self.load_config_file
40+
YAML.load_file("#{MetaRequest.rails_root}/config/meta_request.yml") || {}
41+
rescue Errno::ENOENT
42+
{}
43+
end
1344
end
1445
end
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module MetaRequest
2-
VERSION = '0.7.0'
2+
VERSION = '0.7.1'
33
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
require 'test_helper'
2+
require 'tempfile'
3+
require 'fileutils'
4+
5+
describe MetaRequest::Config do
6+
CONFIG_DIRECTORY = "#{MetaRequest.rails_root}/config"
7+
CONFIG_FILE_PATH = "#{CONFIG_DIRECTORY}/meta_request.yml"
8+
9+
def write_config_file(config)
10+
file = File.new(CONFIG_FILE_PATH, "w")
11+
file.write(config.to_yaml)
12+
file.close
13+
end
14+
15+
before(:each) do
16+
FileUtils.mkdir_p(CONFIG_DIRECTORY)
17+
end
18+
19+
after(:each) do
20+
FileUtils.remove_dir(CONFIG_DIRECTORY, force: true)
21+
MetaRequest::Config.class_variable_set(:@@config_file, nil)
22+
end
23+
24+
describe 'config_file' do
25+
it 'provides default config when config file is not present' do
26+
assert_equal [], MetaRequest::Config.config_file[:ignore_paths]
27+
end
28+
29+
it 'provides default config when config file is empty' do
30+
write_config_file(nil)
31+
assert_equal [], MetaRequest::Config.config_file[:ignore_paths]
32+
end
33+
34+
it 'converts ignore paths array to regexs' do
35+
config = {
36+
ignore_paths: ['.*files', 'notaregex']
37+
}
38+
expected_ignore_paths = config[:ignore_paths].map { |p| Regexp.new(p) }
39+
write_config_file(config)
40+
assert_equal expected_ignore_paths, MetaRequest::Config.config_file[:ignore_paths]
41+
end
42+
end
43+
44+
describe 'ignored_path?' do
45+
it 'does not ignore files if the ignore_paths option is empty' do
46+
assert_equal [], MetaRequest::Config.config_file[:ignore_paths]
47+
assert_equal false, MetaRequest::Config.ignored_path?('this/file/is/not/ignored.txt')
48+
end
49+
50+
it 'ignores files that match the ignore_paths' do
51+
config = {
52+
ignore_paths: ['.*ignored.*', 'notaregex']
53+
}
54+
write_config_file(config)
55+
assert_equal true, MetaRequest::Config.ignored_path?('this/file/is/ignored.txt')
56+
assert_equal true, MetaRequest::Config.ignored_path?('notaregex')
57+
assert_equal false, MetaRequest::Config.ignored_path?('this/file/is/included.txt')
58+
end
59+
60+
end
61+
end
62+

0 commit comments

Comments
 (0)