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

Extend Exception Level Filter configuration to accept lambda applied to all exceptions #1125

Open
wants to merge 4 commits 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
2 changes: 2 additions & 0 deletions lib/rollbar/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Configuration
:enabled,
:endpoint,
:environment,
:exception_level_filter,
:exception_level_filters,
:failover_handlers,
:filepath,
Expand Down Expand Up @@ -106,6 +107,7 @@ def initialize
@enabled = nil # set to true when configure is called
@endpoint = DEFAULT_ENDPOINT
@environment = nil
@exception_level_filter = nil
@exception_level_filters = {
'ActiveRecord::RecordNotFound' => 'warning',
'AbstractController::ActionNotFound' => 'warning',
Expand Down
6 changes: 5 additions & 1 deletion lib/rollbar/notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -488,11 +488,15 @@ def filtered_level(exception)
return unless exception

filter = configuration.exception_level_filters[exception.class.name]
if filter.respond_to?(:call)
level = if filter.respond_to?(:call)
filter.call(exception)
else
filter
end

return level if level && !level.empty?

configuration.exception_level_filter&.call(exception)
end

def report(level, message, exception, extra, context)
Expand Down
241 changes: 165 additions & 76 deletions spec/rollbar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -843,17 +843,101 @@
end

context 'without use_exception_level_filters argument' do
context 'with explicit exception_level_filters' do
it 'sends the correct filtered level' do
Rollbar.configure do |config|
config.exception_level_filters = { 'NameError' => 'warning' }
end

Rollbar.error(exception)

expect(Rollbar.last_report[:level]).to be_eql('warning')
end

it 'ignores ignored exception classes' do
Rollbar.configure do |config|
config.exception_level_filters = { 'NameError' => 'ignore' }
end

logger_mock.should_not_receive(:info)
logger_mock.should_not_receive(:warn)
logger_mock.should_not_receive(:error)

Rollbar.error(exception)
end

it 'should not use the filters if overriden at log site' do
Rollbar.configure do |config|
config.exception_level_filters = { 'NameError' => 'ignore' }
end

Rollbar.error(exception, :use_exception_level_filters => false)

expect(Rollbar.last_report[:level]).to be_eql('error')
end
end

context 'with dynamic exception_level_filter' do
it 'sends the correct filtered level' do
Rollbar.configure do |config|
config.exception_level_filter = lambda { |_error| 'warning' }
end

Rollbar.error(exception)

expect(Rollbar.last_report[:level]).to be_eql('warning')
end

it 'ignores ignored exception classes' do
Rollbar.configure do |config|
config.exception_level_filter = lambda { |_error| 'ignore' }
end

logger_mock.should_not_receive(:info)
logger_mock.should_not_receive(:warn)
logger_mock.should_not_receive(:error)

Rollbar.error(exception)
end

it 'should not use the filters if overriden at log site' do
Rollbar.configure do |config|
config.exception_level_filter = ->(exception) { 'ignore' }
end

Rollbar.error(exception, :use_exception_level_filters => false)

expect(Rollbar.last_report[:level]).to be_eql('error')
end
end

context 'with both explicit and dynamic exception_level_filter' do
it 'send the explicit filtered level' do
Rollbar.configure do |config|
config.exception_level_filters = { 'NameError' => 'info' }
config.exception_level_filter = ->(exception) { 'warning' }
end

Rollbar.error(exception)

expect(Rollbar.last_report[:level]).to be_eql('info')
end
end
end
end

context 'using :use_exception_level_filters option as true' do
context 'with explicit exception_level_filters' do
it 'sends the correct filtered level' do
Rollbar.configure do |config|
config.exception_level_filters = { 'NameError' => 'warning' }
end

Rollbar.error(exception)

Rollbar.error(exception, :use_exception_level_filters => true)
expect(Rollbar.last_report[:level]).to be_eql('warning')
end

it 'ignore ignored exception classes' do
it 'ignores ignored exception classes' do
Rollbar.configure do |config|
config.exception_level_filters = { 'NameError' => 'ignore' }
end
Expand All @@ -862,120 +946,125 @@
logger_mock.should_not_receive(:warn)
logger_mock.should_not_receive(:error)

Rollbar.error(exception)
Rollbar.error(exception, :use_exception_level_filters => true)
end

it 'should not use the filters if overriden at log site' do
it 'sets error level using lambda' do
Rollbar.configure do |config|
config.exception_level_filters = { 'NameError' => 'ignore' }
config.exception_level_filters = {
'NameError' => lambda { |_error| 'info' }
}
end

Rollbar.error(exception, :use_exception_level_filters => false)

expect(Rollbar.last_report[:level]).to be_eql('error')
end
end
end
logger_mock.should_receive(:info)
logger_mock.should_not_receive(:warn)
logger_mock.should_not_receive(:error)

context 'using :use_exception_level_filters option as true' do
it 'sends the correct filtered level' do
Rollbar.configure do |config|
config.exception_level_filters = { 'NameError' => 'warning' }
Rollbar.error(exception, :use_exception_level_filters => true)
end

Rollbar.error(exception, :use_exception_level_filters => true)
expect(Rollbar.last_report[:level]).to be_eql('warning')
end
context 'using :use_exception_level_filters option as false' do
it 'sends the correct filtered level' do
Rollbar.configure do |config|
config.exception_level_filters = { 'NameError' => 'warning' }
end

it 'ignore ignored exception classes' do
Rollbar.configure do |config|
config.exception_level_filters = { 'NameError' => 'ignore' }
end
Rollbar.error(exception, :use_exception_level_filters => false)
expect(Rollbar.last_report[:level]).to be_eql('error')
end

logger_mock.should_not_receive(:info)
logger_mock.should_not_receive(:warn)
logger_mock.should_not_receive(:error)
it 'ignores ignored exception classes' do
Rollbar.configure do |config|
config.exception_level_filters = { 'NameError' => 'ignore' }
end

Rollbar.error(exception, :use_exception_level_filters => true)
end
Rollbar.error(exception, :use_exception_level_filters => false)

it 'sets error level using lambda' do
Rollbar.configure do |config|
config.exception_level_filters = {
'NameError' => lambda { |_error| 'info' }
}
expect(Rollbar.last_report[:level]).to be_eql('error')
end
end

logger_mock.should_receive(:info)
logger_mock.should_not_receive(:warn)
logger_mock.should_not_receive(:error)

Rollbar.error(exception, :use_exception_level_filters => true)
end

context 'using :use_exception_level_filters option as false' do
context 'with dynamic exception_level_filter' do
it 'sends the correct filtered level' do
Rollbar.configure do |config|
config.exception_level_filters = { 'NameError' => 'warning' }
config.exception_level_filter = lambda { |_error| 'warning' }
end

Rollbar.error(exception, :use_exception_level_filters => false)
expect(Rollbar.last_report[:level]).to be_eql('error')
Rollbar.error(exception, :use_exception_level_filters => true)

expect(Rollbar.last_report[:level]).to be_eql('warning')
end

it 'ignore ignored exception classes' do
it 'ignores ignored exception classes' do
Rollbar.configure do |config|
config.exception_level_filters = { 'NameError' => 'ignore' }
config.exception_level_filter = lambda { |_error| 'ignore' }
end

Rollbar.error(exception, :use_exception_level_filters => false)
logger_mock.should_not_receive(:info)
logger_mock.should_not_receive(:warn)
logger_mock.should_not_receive(:error)

expect(Rollbar.last_report[:level]).to be_eql('error')
Rollbar.error(exception, :use_exception_level_filters => true)
end
end
end

context 'using :use_exception_level_filters option as true' do
it 'sends the correct filtered level' do
Rollbar.configure do |config|
config.exception_level_filters = { 'NameError' => 'warning' }
end
context 'using :use_exception_level_filters option as false' do
it 'sends the correct filtered level' do
Rollbar.configure do |config|
config.exception_level_filter = lambda { |_error| 'ignore' }
end

Rollbar.error(exception, :use_exception_level_filters => true)
expect(Rollbar.last_report[:level]).to be_eql('warning')
end
Rollbar.error(exception, :use_exception_level_filters => false)
expect(Rollbar.last_report[:level]).to be_eql('error')
end

it 'ignore ignored exception classes' do
Rollbar.configure do |config|
config.exception_level_filters = { 'NameError' => 'ignore' }
end
it 'ignores ignored exception classes' do
Rollbar.configure do |config|
config.exception_level_filter = lambda { |_error| 'ignore' }
end

logger_mock.should_not_receive(:info)
logger_mock.should_not_receive(:warn)
logger_mock.should_not_receive(:error)
Rollbar.error(exception, :use_exception_level_filters => false)

Rollbar.error(exception, :use_exception_level_filters => true)
expect(Rollbar.last_report[:level]).to be_eql('error')
end
end
end
end

context 'if not using :use_exception_level_filters option' do
it 'sends the level defined by the used method' do
Rollbar.configure do |config|
config.exception_level_filters = { 'NameError' => 'warning' }
end
context 'with explicit exception_level_filters' do
it 'sends the level defined by the used method' do
Rollbar.configure do |config|
config.exception_level_filters = { 'NameError' => 'warning' }
end

Rollbar.error(exception)
expect(Rollbar.last_report[:level]).to be_eql('error')
Rollbar.error(exception)
expect(Rollbar.last_report[:level]).to be_eql('error')
end
end

it 'ignore ignored exception classes' do
Rollbar.configure do |config|
config.exception_level_filters = { 'NameError' => 'ignore' }
context 'with both explicit and dynamic exception_level_filter' do
it 'send the explicit filtered level' do
Rollbar.configure do |config|
config.exception_level_filters = { 'NameError' => 'info' }
config.exception_level_filter = lambda { |_error| 'warning' }
end

Rollbar.error(exception, :use_exception_level_filters => true)

expect(Rollbar.last_report[:level]).to be_eql('info')
end

Rollbar.error(exception)
it 'ignores ignored exception classes' do
Rollbar.configure do |config|
config.exception_level_filters = { 'NameError' => 'ignore' }
config.exception_level_filter = lambda { |_error| 'warning' }
end

expect(Rollbar.last_report[:level]).to be_eql('error')
Rollbar.error(exception)

expect(Rollbar.last_report[:level]).to be_eql('error')
end
end
end

Expand Down