Class: ScoutApm::BackgroundJobIntegrations::SidekiqMiddleware

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

Instance Method Summary collapse

Instance Method Details

#call(worker, msg, queue) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/scout_apm/background_job_integrations/sidekiq.rb', line 45

def call(worker, msg, queue)
  job_class = msg["class"] # TODO: Validate this across different versions of Sidekiq
  if job_class == "ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper" && msg.has_key?("wrapped")
    job_class = msg["wrapped"]
  end

  latency = (Time.now.to_f - (msg['enqueued_at'] || msg['created_at']))

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

  req.start_layer( ScoutApm::Layer.new("Queue", queue) )
  req.start_layer( ScoutApm::Layer.new("Job", job_class) )

  begin
    yield
  rescue
    req.error!
    raise
  end
ensure
  req.stop_layer # Job
  req.stop_layer # Queue
end