class ScoutApm::BackgroundJobIntegrations::FaktoryMiddleware

We insert this middleware into the Sidekiq stack, to capture each job, and time them.

Constants

ACTIVE_JOB_KLASS
UNKNOWN_CLASS_PLACEHOLDER

Public Instance Methods

call(worker_instance, job) { || ... } click to toggle source
# File lib/scout_apm/background_job_integrations/faktory.rb, line 50
def call(worker_instance, job)
  queue = job["queue"]

  req = ScoutApm::RequestManager.lookup
  req.annotate_request(:queue_latency => latency(job))

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

    yield
  rescue
    req.error!
    raise
  ensure
    req.stop_layer if started_job
    req.stop_layer if started_queue
  end
end
job_class(job) click to toggle source
# File lib/scout_apm/background_job_integrations/faktory.rb, line 75
def job_class(job)
  job_class = job.fetch('jobtype', UNKNOWN_CLASS_PLACEHOLDER)

  if job_class == ACTIVE_JOB_KLASS && job.key?('custom') && job['custom'].key?('wrapped')
    begin
      job_class = job['custom']['wrapped']
    rescue
      ACTIVE_JOB_KLASS
    end
  end

  job_class
rescue
  UNKNOWN_CLASS_PLACEHOLDER
end
latency(job, time = Time.now) click to toggle source
# File lib/scout_apm/background_job_integrations/faktory.rb, line 91
def latency(job, time = Time.now)
  created_at = Time.parse(job['enqueued_at'] || job['created_at'])
  if created_at
    (time - created_at)
  else
    0
  end
rescue
  0
end