Sha256: bdda00bb6eefd1aa6077ecca1a6fc52b7e32c6421b4f1435a63c196d4d5e83ea

Contents?: true

Size: 1.85 KB

Versions: 2

Compression:

Stored size: 1.85 KB

Contents

# Adapted from Bugsnag code, and Sidekiq Erorr Handling instructions
#
# SideKiq: https://github.com/sidekiq/sidekiq/wiki/Error-Handling
# Bugsnag: https://github.com/bugsnag/bugsnag-ruby/blob/master/lib/bugsnag/sidekiq.rb

module Raygun

  class SidekiqReporter
    def self.call(exception, context_hash = {}, config = nil)
      user = affected_user(context_hash)
      data =  {
        custom_data: {
          sidekiq_context: context_hash
        },
        tags: ['sidekiq']
      }

      if exception.is_a?(Sidekiq::JobRetry::Handled) && exception.cause
        if Raygun.configuration.track_retried_sidekiq_jobs
          data.merge!(sidekiq_retried: true)
          exception = exception.cause
        else
          return false
        end
      end

      if exception.instance_variable_defined?(:@__raygun_correlation_id) && correlation_id = exception.instance_variable_get(:@__raygun_correlation_id)
        data.merge!(correlation_id: correlation_id)
      end
      ::Raygun.track_exception(
          exception,
          data,
          user
        )
    end

    # Extracts affected user information out of a Sidekiq worker class
    def self.affected_user(context_hash)
      job = context_hash[:job]

      return if job.nil? || job['class'].nil? || !Module.const_defined?(job['class'])

      worker_class = Module.const_get(job['class'])
      affected_user_method = Raygun.configuration.affected_user_method

      return if worker_class.nil? || !worker_class.respond_to?(affected_user_method)

      worker_class.send(affected_user_method, job['args'])
    rescue => e
      return unless Raygun.configuration.failsafe_logger

      failsafe_log("Problem in sidekiq affected user tracking: #{e.class}: #{e.message}\n\n#{e.backtrace.join("\n")}")

      nil
    end
  end
end

Sidekiq.configure_server do |config|
  config.error_handlers << Raygun::SidekiqReporter
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
raygun4ruby-4.0.2 lib/raygun/sidekiq.rb
raygun4ruby-4.0.1 lib/raygun/sidekiq.rb