Class: ScoutApm::BackgroundJobIntegrations::FaktoryMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/scout_apm/background_job_integrations/faktory.rb

Overview

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

Constant Summary collapse

UNKNOWN_CLASS_PLACEHOLDER =
'UnknownJob'.freeze
ACTIVE_JOB_KLASS =
'ActiveJob::QueueAdapters::FaktoryAdapter::JobWrapper'.freeze

Instance Method Summary collapse

Instance Method Details

#call(worker_instance, job) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 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) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# 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