Class: ScoutApm::BackgroundJobIntegrations::SidekiqMiddleware

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

Overview

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

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#call(_worker, msg, queue) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/scout_apm/background_job_integrations/sidekiq.rb', line 60

def call(_worker, msg, queue)
  req = ScoutApm::RequestManager.lookup
  req.job!
  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(msg)))
    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(msg) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/scout_apm/background_job_integrations/sidekiq.rb', line 83

def job_class(msg)
  job_class = msg.fetch('class', UNKNOWN_CLASS_PLACEHOLDER)
  if job_class == ACTIVE_JOB_KLASS && msg.key?('wrapped')
    job_class = msg['wrapped']
  end
  job_class
rescue
  UNKNOWN_CLASS_PLACEHOLDER
end

#latency(msg, time = Time.now.to_f) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/scout_apm/background_job_integrations/sidekiq.rb', line 93

def latency(msg, time = Time.now.to_f)
  created_at = msg['enqueued_at'] || msg['created_at']
  if created_at
    (time - created_at)
  else
    0
  end
rescue
  0
end