class ScoutApm::BackgroundJobIntegrations::Sneakers

Constants

ACTIVE_JOB_KLASS
UNKNOWN_CLASS_PLACEHOLDER
UNKNOWN_QUEUE_PLACEHOLDER

Public Instance Methods

forking?() click to toggle source
# File lib/scout_apm/background_job_integrations/sneakers.rb, line 22
def forking?
  false
end
initialize_with_scout(*args) click to toggle source
# File lib/scout_apm/background_job_integrations/sneakers.rb, line 32
def initialize_with_scout(*args)
  agent = ::ScoutApm::Agent.instance
  agent.start
  initialize_without_scout(*args)
end
install() click to toggle source
# File lib/scout_apm/background_job_integrations/sneakers.rb, line 26
def install
  install_worker_override
end
install_worker_override() click to toggle source
# File lib/scout_apm/background_job_integrations/sneakers.rb, line 30
def install_worker_override
  ::Sneakers::Worker.module_eval do
    def initialize_with_scout(*args)
      agent = ::ScoutApm::Agent.instance
      agent.start
      initialize_without_scout(*args)
    end

    alias_method :initialize_without_scout, :initialize
    alias_method :initialize, :initialize_with_scout

    def process_work_with_scout(*args)
      delivery_info, _metadata, msg, _handler = args

      queue = delivery_info[:routing_key] || UNKNOWN_QUEUE_PLACEHOLDER

      job_class = begin
        if self.class == ActiveJob::QueueAdapters::SneakersAdapter::JobWrapper
          msg["job_class"] || UNKNOWN_CLASS_PLACEHOLDER
        else
          self.class.name
        end
      rescue => e
        UNKNOWN_CLASS_PLACEHOLDER
      end

      req = ScoutApm::RequestManager.lookup

      # RabbitMQ does not store a created-at timestamp
      # req.annotate_request(:queue_latency => latency(msg))

      begin
        req.start_layer(ScoutApm::Layer.new('Queue', queue))
        started_queue = true
        req.start_layer(ScoutApm::Layer.new('Job', job_class))
        started_job = true

        process_work_without_scout(*args)
      rescue Exception => e
        req.error!
        raise
      ensure
        req.stop_layer if started_job
        req.stop_layer if started_queue
      end
    end

    alias_method :process_work_without_scout, :process_work
    alias_method :process_work, :process_work_with_scout
  end
end
name() click to toggle source
# File lib/scout_apm/background_job_integrations/sneakers.rb, line 4
def name
  :sneakers
end
present?() click to toggle source
# File lib/scout_apm/background_job_integrations/sneakers.rb, line 8
def present?
  defined?(::Sneakers) && supported_version?
end
process_work_with_scout(*args) click to toggle source
# File lib/scout_apm/background_job_integrations/sneakers.rb, line 41
def process_work_with_scout(*args)
  delivery_info, _metadata, msg, _handler = args

  queue = delivery_info[:routing_key] || UNKNOWN_QUEUE_PLACEHOLDER

  job_class = begin
    if self.class == ActiveJob::QueueAdapters::SneakersAdapter::JobWrapper
      msg["job_class"] || UNKNOWN_CLASS_PLACEHOLDER
    else
      self.class.name
    end
  rescue => e
    UNKNOWN_CLASS_PLACEHOLDER
  end

  req = ScoutApm::RequestManager.lookup

  # RabbitMQ does not store a created-at timestamp
  # req.annotate_request(:queue_latency => latency(msg))

  begin
    req.start_layer(ScoutApm::Layer.new('Queue', queue))
    started_queue = true
    req.start_layer(ScoutApm::Layer.new('Job', job_class))
    started_job = true

    process_work_without_scout(*args)
  rescue Exception => e
    req.error!
    raise
  ensure
    req.stop_layer if started_job
    req.stop_layer if started_queue
  end
end
supported_version?() click to toggle source

Only support Sneakers 2.7 and up

# File lib/scout_apm/background_job_integrations/sneakers.rb, line 13
def supported_version?
  result = Gem::Version.new(::Sneakers::VERSION) > Gem::Version.new("2.7.0")
  ScoutApm::Agent.instance.logger.info("Skipping Sneakers instrumentation. Only versions 2.7+ are supported. See docs or contact support@scoutapm.com for instrumentation of older versions.")
  result
rescue
  ScoutApm::Agent.instance.logger.info("Failed comparing Sneakers Version. Skipping")
  false
end