Skip to content

Commit

Permalink
Refactor approach to give us queue information in all versions of sid…
Browse files Browse the repository at this point in the history
…ekiq
  • Loading branch information
snmaynard committed Jun 21, 2018
1 parent 4b4d846 commit 475df87
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 26 deletions.
4 changes: 3 additions & 1 deletion features/sidekiq.feature
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Scenario Outline: An unhandled RuntimeError sends a report
And the request contained the api key "a35a2a72bd230ac0aa0f52715bbdc6aa"
And the event "unhandled" is true
And the event "severity" is "error"
And the event "context" is "UnhandledError@default"
And the event "severityReason.type" is "unhandledExceptionMiddleware"
And the event "severityReason.attributes.framework" is "Sidekiq"
And the exception "errorClass" equals "RuntimeError"
Expand Down Expand Up @@ -62,6 +63,7 @@ Scenario Outline: A handled RuntimeError can be notified
And the request used payload v4 headers
And the request contained the api key "a35a2a72bd230ac0aa0f52715bbdc6aa"
And the event "unhandled" is false
And the event "context" is "HandledError@default"
And the event "severity" is "warning"
And the event "severityReason.type" is "handledException"
And the exception "errorClass" equals "RuntimeError"
Expand Down Expand Up @@ -93,4 +95,4 @@ Scenario Outline: A handled RuntimeError can be notified
| 2.5 | ~> 2 | false |
| 2.5 | ~> 3 | true |
| 2.5 | ~> 4 | true |
| 2.5 | ~> 5 | true |
| 2.5 | ~> 5 | true |
49 changes: 28 additions & 21 deletions lib/bugsnag/integrations/sidekiq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Sidekiq

FRAMEWORK_ATTRIBUTES = {
:framework => "Sidekiq"
}
} unless const_defined?(:FRAMEWORK_ATTRIBUTES)

def initialize
Bugsnag.configuration.internal_middleware.use(Bugsnag::Middleware::Sidekiq)
Expand All @@ -21,36 +21,43 @@ def call(worker, msg, queue)
Bugsnag.configuration.set_request_data :sidekiq, { :msg => msg, :queue => queue }
yield
rescue Exception => ex
raise ex if [Interrupt, SystemExit, SignalException].include? ex.class
notify(ex)
self.class.notify(ex) unless self.class.sidekiq_supports_error_handlers
raise
ensure
Bugsnag.configuration.clear_request_data
end
end

def notify(exception)
Bugsnag.notify(exception, true) do |report|
report.severity = "error"
report.severity_reason = {
:type => Bugsnag::Report::UNHANDLED_EXCEPTION_MIDDLEWARE,
:attributes => FRAMEWORK_ATTRIBUTES
}
def self.notify(exception)
unless [Interrupt, SystemExit, SignalException].include? exception.class
Bugsnag.notify(exception, true) do |report|
report.severity = "error"
report.severity_reason = {
:type => Bugsnag::Report::UNHANDLED_EXCEPTION_MIDDLEWARE,
:attributes => FRAMEWORK_ATTRIBUTES
}
end
end
end
end
end

::Sidekiq.configure_server do |config|
if Gem::Version.new(Sidekiq::VERSION) >= Gem::Version.new('3.0.0')
config.error_handlers << proc do |ex, context|
Bugsnag.configuration.set_request_data :sidekiq, { :msg => context, :queue => context['queue'] }
bugsnag_handler = ::Bugsnag::Sidekiq.new
bugsnag_handler.notify(ex)
def self.sidekiq_supports_error_handlers
Gem::Version.new(::Sidekiq::VERSION) >= Gem::Version.new('3.0.0')
end
else
config.server_middleware do |chain|
chain.add ::Bugsnag::Sidekiq

def self.configure_server(server)
if Bugsnag::Sidekiq.sidekiq_supports_error_handlers
server.error_handlers << proc do |ex, context|
Bugsnag::Sidekiq.notify(ex)
end
end

server.server_middleware do |chain|
chain.add ::Bugsnag::Sidekiq
end
end
end
end

::Sidekiq.configure_server do |config|
Bugsnag::Sidekiq.configure_server(config)
end
13 changes: 9 additions & 4 deletions spec/integrations/sidekiq_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ def perform(value)
end

describe Bugsnag::Sidekiq do
context "integration tests" do
# Integration testing v3 is handled by maze as sidekiq doesnt
# support error_handlers in testing mode
context "integration tests in v2" do
before do
Sidekiq::Testing.inline!
Sidekiq::Testing.server_middleware do |chain|
chain.add Bugsnag::Sidekiq
end
stub_const('Sidekiq::VERSION', '2.0.0')
Bugsnag::Sidekiq.configure_server(Sidekiq::Testing)
end

it "works" do
Expand Down Expand Up @@ -55,6 +56,10 @@ def perform(value)
error_handlers = []
expect(config).to receive(:error_handlers).and_return(error_handlers)

chain = double
expect(config).to receive(:server_middleware).and_yield(chain)
expect(chain).to receive(:add).with(::Bugsnag::Sidekiq)

load './lib/bugsnag/integrations/sidekiq.rb'
expect(error_handlers.size).to eq(1)
end
Expand Down

0 comments on commit 475df87

Please sign in to comment.