Class: Bugsnag::Sidekiq

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::ServerMiddleware
Defined in:
lib/bugsnag/integrations/sidekiq.rb

Overview

Extracts and attaches Sidekiq job and queue information to an error report

Constant Summary collapse

FRAMEWORK_ATTRIBUTES =
{
  :framework => "Sidekiq"
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSidekiq

Returns a new instance of Sidekiq.



15
16
17
18
19
# File 'lib/bugsnag/integrations/sidekiq.rb', line 15

def initialize
  Bugsnag.configuration.internal_middleware.use(Bugsnag::Middleware::Sidekiq)
  Bugsnag.configuration.detected_app_type = "sidekiq"
  Bugsnag.configuration.runtime_versions["sidekiq"] = ::Sidekiq::VERSION
end

Class Method Details

.configure_server(server) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/bugsnag/integrations/sidekiq.rb', line 51

def self.configure_server(server)
  if Bugsnag::Sidekiq.sidekiq_supports_error_handlers
    server.error_handlers << proc do |ex, _context, _config = nil|
      Bugsnag::Sidekiq.notify(ex)
      Bugsnag.configuration.clear_request_data
    end
  end

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

.notify(exception) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/bugsnag/integrations/sidekiq.rb', line 37

def self.notify(exception)
  Bugsnag.notify(exception, true) do |report|
    report.severity = "error"
    report.severity_reason = {
      :type => Bugsnag::Report::UNHANDLED_EXCEPTION_MIDDLEWARE,
      :attributes => FRAMEWORK_ATTRIBUTES
    }
  end
end

.sidekiq_supports_error_handlersObject



47
48
49
# File 'lib/bugsnag/integrations/sidekiq.rb', line 47

def self.sidekiq_supports_error_handlers
  Gem::Version.new(::Sidekiq::VERSION) >= Gem::Version.new('3.0.0')
end

Instance Method Details

#call(worker, msg, queue) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bugsnag/integrations/sidekiq.rb', line 21

def call(worker, msg, queue)
  # store msg/queue in thread local state to be read by Bugsnag::Middleware::Sidekiq
  Bugsnag.configuration.set_request_data(:sidekiq, { :msg => msg, :queue => queue })
  error_raised = false
  yield
rescue Exception => ex
  error_raised = true
  self.class.notify(ex) unless self.class.sidekiq_supports_error_handlers
  raise
ensure
  # if an error was raised and error handlers are installed, the data will be cleared after
  # the notification is sent. Otherwise, the data must be cleared.
  keep_data = error_raised && self.class.sidekiq_supports_error_handlers
  Bugsnag.configuration.clear_request_data unless keep_data
end