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

refactor sidekiq handler #197

Merged
merged 7 commits into from
Feb 5, 2015
Merged
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
39 changes: 19 additions & 20 deletions lib/rollbar/sidekiq.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
# encoding: utf-8

PARAM_BLACKLIST = %w[backtrace error_backtrace error_message error_class]
module Rollbar
class Sidekiq
PARAM_BLACKLIST = %w[backtrace error_backtrace error_message error_class]

if Sidekiq::VERSION < '3'
module Rollbar
class Sidekiq
def call(worker, msg, queue)
yield
rescue Exception => e
params = msg.reject{ |k| PARAM_BLACKLIST.include?(k) }
scope = { :request => { :params => params } }
def self.handle_exception(msg_or_context, e)
params = msg_or_context.reject{ |k| PARAM_BLACKLIST.include?(k) }
scope = { :request => { :params => params } }

Rollbar.scope(scope).error(e, :use_exception_level_filters => true)
raise
end
Rollbar.scope(scope).error(e, :use_exception_level_filters => true)
end

def call(worker, msg, queue)
yield
rescue Exception => e
Rollbar::Sidekiq.handle_exception(msg, e)
raise
end
end
end

Sidekiq.configure_server do |config|
Sidekiq.configure_server do |config|
if Sidekiq::VERSION < '3'
config.server_middleware do |chain|
chain.add Rollbar::Sidekiq
end
end
else
Sidekiq.configure_server do |config|
else
config.error_handlers << Proc.new do |e, context|
params = context.reject{ |k| PARAM_BLACKLIST.include?(k) }
scope = { :request => { :params => params } }

Rollbar.scope(scope).error(e, :use_exception_level_filters => true)
Rollbar::Sidekiq.handle_exception(context, e)
end
end
end
2 changes: 1 addition & 1 deletion rollbar.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'girl_friday', '>= 0.11.1'
gem.add_development_dependency 'sucker_punch', '>= 1.0.0' if RUBY_VERSION != '1.8.7'
gem.add_development_dependency 'sidekiq', '>= 2.13.0' if RUBY_VERSION != '1.8.7'
gem.add_development_dependency 'genspec', '>= 0.2.7'
gem.add_development_dependency 'genspec', '>= 0.2.8'
gem.add_development_dependency 'sinatra'
gem.add_development_dependency 'resque'
gem.add_development_dependency 'delayed_job'
Expand Down
47 changes: 47 additions & 0 deletions spec/rollbar/sidekiq_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'spec_helper'

unless RUBY_VERSION == '1.8.7'
require 'sidekiq'
require 'rollbar/sidekiq'
end

describe Rollbar::Sidekiq, :reconfigure_notifier => false do
describe '.handle_exception' do
let(:msg_or_context) { ['hello', 'error_backtrace', 'backtrace', 'goodbye'] }
let(:exception) { StandardError.new('oh noes') }
let(:rollbar) { double }
let(:expected_args) { { :request => { :params => ['hello', 'goodbye'] } } }

subject { described_class }

it 'constructs scope from filtered params' do
allow(rollbar).to receive(:error)
expect(Rollbar).to receive(:scope).with(expected_args) {rollbar}

described_class.handle_exception(msg_or_context, exception)
end

it 'sends the passed-in error to rollbar' do
allow(Rollbar).to receive(:scope).and_return(rollbar)
expect(rollbar).to receive(:error).with(exception, :use_exception_level_filters => true)

described_class.handle_exception(msg_or_context, exception)
end
end

describe '#call' do
let(:msg) { ['hello'] }
let(:exception) { StandardError.new('oh noes') }
let(:middleware_block) { proc { raise exception } }

subject { Rollbar::Sidekiq.new }

it 'sends the error to Rollbar::Sidekiq.handle_exception' do
expect(Rollbar::Sidekiq).to receive(:handle_exception).with(msg, exception)

expect { subject.call(nil, msg, nil, &middleware_block) }.to raise_error(exception)
end
end
end unless RUBY_VERSION == '1.8.7'