Skip to content

Commit

Permalink
Correctly extract name from Sidekiq DelayedClass
Browse files Browse the repository at this point in the history
This is the wrapper that gets used when you use ad-hoc backgrounding
with `MyClass.delay.myfunc(1,2,3)`

We were just getting the name of the wrapper, and now we expand it out
and read off the name of the actual function being called.
  • Loading branch information
Chris Schneider committed Sep 8, 2017
1 parent 38a38f7 commit 8f16001
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions lib/scout_apm/background_job_integrations/sidekiq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ def initialize_with_scout(boss)
# We insert this middleware into the Sidekiq stack, to capture each job,
# and time them.
class SidekiqMiddleware
ACTIVE_JOB_KLASS = 'ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper'.freeze

def call(_worker, msg, queue)
req = ScoutApm::RequestManager.lookup
req.job!
Expand All @@ -79,12 +77,30 @@ def call(_worker, msg, queue)
end

UNKNOWN_CLASS_PLACEHOLDER = 'UnknownJob'.freeze
ACTIVE_JOB_KLASS = 'ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper'.freeze
DELAYED_WRAPPER_KLASS = 'Sidekiq::Extensions::DelayedClass'.freeze


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']
begin
job_class = msg['wrapped']

This comment has been minimized.

Copy link
@rcugut

rcugut Nov 28, 2020

@cschneid will this ever raise, considering the key? check on line 87 above ?

maybe:

job_class = msg['wrapped'].to_s

and would be a cheap hack-fix for #342

rescue
ACTIVE_JOB_KLASS
end
elsif job_class == DELAYED_WRAPPER_KLASS
begin
yml = msg['args'].first
deserialized_args = YAML.load(yml)
klass, method, *rest = deserialized_args
job_class = [klass,method].map(&:to_s).join(".")
rescue
DELAYED_WRAPPER_KLASS
end
end

job_class
rescue
UNKNOWN_CLASS_PLACEHOLDER
Expand Down

0 comments on commit 8f16001

Please sign in to comment.