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

Allow callers from some files to be ignored #124

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ Select your editor on the extension options page: chrome://extensions. Following
* Atom
* RubyMine

## Meta Request Configuration

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.

```yaml
:ignore_paths:
- .*/directory/exclude_this_file
```

## Compatibility Warnings

If you're using [LiveReload](http://livereload.com/) or
Expand Down
1 change: 1 addition & 0 deletions meta_request/lib/meta_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module MetaRequest
autoload :Middlewares, "meta_request/middlewares"
autoload :LogInterceptor, "meta_request/log_interceptor"
autoload :AppNotifications, "meta_request/app_notifications"
autoload :Config, "meta_request/config"

def self.config
@config ||= Config.new
Expand Down
19 changes: 12 additions & 7 deletions meta_request/lib/meta_request/app_notifications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class AppNotifications
payload[:options][k] = payload.delete(k) unless k.in? CACHE_KEY_COLUMNS
end

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

SQL_BLOCK = Proc.new {|*args|
name, start, ending, transaction_id, payload = args
dev_caller = caller.detect { |c| c.include? MetaRequest.rails_root }
if dev_caller
c = Callsite.parse(dev_caller)
payload.merge!(:line => c.line, :filename => c.filename, :method => c.method)
end
name, start, ending, transaction_id, payload = args
dev_caller = caller.detect { |c| dev_caller?(c) }
if dev_caller
c = Callsite.parse(dev_caller)
payload.merge!(:line => c.line, :filename => c.filename, :method => c.method)
end
Event.new(SQL_EVENT_NAME, start, ending, transaction_id, payload)
}
# Subscribe to all events relevant to RailsPanel
Expand Down Expand Up @@ -81,6 +81,11 @@ def subscribe(event_name)
self
end

private

def self.dev_caller?(file_path)
file_path.include?(MetaRequest.rails_root) && !Config.ignored_path?(file_path)
end
end

end
31 changes: 31 additions & 0 deletions meta_request/lib/meta_request/config.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'yaml'

module MetaRequest
class Config
# logger used for reporting gem's fatal errors
Expand All @@ -10,5 +12,34 @@ def logger
def storage_pool_size
@storage_pool_size ||= 20
end

def self.ignored_path?(file_path)
ingore_paths.any? { |ignore_path| ignore_path.match(file_path) }
end

def self.config_file
@@config_file ||= load_and_parse_config_file
end

private

def self.ingore_paths
config_file[:ignore_paths]
end

def self.load_and_parse_config_file
@@config_file = load_config_file
@@config_file[:ignore_paths] ||= []
@@config_file[:ignore_paths].map! do |ignore_path|
Regexp.new(ignore_path)
end
@@config_file.freeze
end

def self.load_config_file
YAML.load_file("#{MetaRequest.rails_root}/config/meta_request.yml") || {}
rescue Errno::ENOENT
{}
end
end
end
2 changes: 1 addition & 1 deletion meta_request/lib/meta_request/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module MetaRequest
VERSION = '0.7.0'
VERSION = '0.7.1'
end
62 changes: 62 additions & 0 deletions meta_request/test/unit/meta_request/config_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require 'test_helper'
require 'tempfile'
require 'fileutils'

describe MetaRequest::Config do
CONFIG_DIRECTORY = "#{MetaRequest.rails_root}/config"
CONFIG_FILE_PATH = "#{CONFIG_DIRECTORY}/meta_request.yml"

def write_config_file(config)
file = File.new(CONFIG_FILE_PATH, "w")
file.write(config.to_yaml)
file.close
end

before(:each) do
FileUtils.mkdir_p(CONFIG_DIRECTORY)
end

after(:each) do
FileUtils.remove_dir(CONFIG_DIRECTORY, force: true)
MetaRequest::Config.class_variable_set(:@@config_file, nil)
end

describe 'config_file' do
it 'provides default config when config file is not present' do
assert_equal [], MetaRequest::Config.config_file[:ignore_paths]
end

it 'provides default config when config file is empty' do
write_config_file(nil)
assert_equal [], MetaRequest::Config.config_file[:ignore_paths]
end

it 'converts ignore paths array to regexs' do
config = {
ignore_paths: ['.*files', 'notaregex']
}
expected_ignore_paths = config[:ignore_paths].map { |p| Regexp.new(p) }
write_config_file(config)
assert_equal expected_ignore_paths, MetaRequest::Config.config_file[:ignore_paths]
end
end

describe 'ignored_path?' do
it 'does not ignore files if the ignore_paths option is empty' do
assert_equal [], MetaRequest::Config.config_file[:ignore_paths]
assert_equal false, MetaRequest::Config.ignored_path?('this/file/is/not/ignored.txt')
end

it 'ignores files that match the ignore_paths' do
config = {
ignore_paths: ['.*ignored.*', 'notaregex']
}
write_config_file(config)
assert_equal true, MetaRequest::Config.ignored_path?('this/file/is/ignored.txt')
assert_equal true, MetaRequest::Config.ignored_path?('notaregex')
assert_equal false, MetaRequest::Config.ignored_path?('this/file/is/included.txt')
end

end
end