Skip to content

Commit

Permalink
If job is of type ActiveJob pull underlying job name from the wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
agirlnamedsophia committed Nov 7, 2018
1 parent d91575f commit 0c7b262
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
10 changes: 9 additions & 1 deletion lib/ddtrace/contrib/delayed_job/plugin.rb
Expand Up @@ -9,7 +9,15 @@ class Plugin < Delayed::Plugin
def self.instrument(job, &block)
return block.call(job) unless tracer && tracer.enabled

tracer.trace(Ext::SPAN_JOB, service: configuration[:service_name], resource: job.name) do |span|
# When DelayedJob is used through ActiveJob, we need to parse the payload differentely
# to get the actual job name
job_name = if job.payload_object.respond_to?(:job_data)
job.payload_object.job_data['job_class']
else
job.name
end

tracer.trace(Ext::SPAN_JOB, service: configuration[:service_name], resource: job_name) do |span|
span.set_tag(Ext::TAG_ID, job.id)
span.set_tag(Ext::TAG_QUEUE, job.queue) if job.queue
span.set_tag(Ext::TAG_PRIORITY, job.priority)
Expand Down
36 changes: 30 additions & 6 deletions spec/ddtrace/contrib/delayed_job/plugin_spec.rb
Expand Up @@ -7,13 +7,26 @@
require_relative 'delayed_job_active_record'

RSpec.describe Datadog::Contrib::DelayedJob::Plugin, :delayed_job_active_record do
let(:sample_job_object) { double('sample_job', perform: nil) }
let(:sample_job_class) { class_double('SampleJob', new: sample_job_object) }
let(:sample_job_object) do
stub_const('SampleJob', Class.new do
def perform; end
end)
end
let(:active_job_sample_job_object) do
stub_const('ActiveJobSampleJob', Class.new do
def perform; end

def job_data
{
'job_class' => 'UnderlyingJobClass'
}
end
end)
end

let(:tracer) { ::Datadog::Tracer.new(writer: FauxWriter.new) }

before do
sample_job_class.as_stubbed_const

Datadog.configure { |c| c.use :delayed_job, tracer: tracer }

Delayed::Worker.delay_jobs = false
Expand All @@ -40,12 +53,23 @@

describe 'instrumented job invocation' do
let(:job_params) { {} }
subject(:job_run) { Delayed::Job.enqueue(SampleJob.new, job_params) }
subject(:job_run) { Delayed::Job.enqueue(sample_job_object.new, job_params) }

it 'creates a span' do
expect { job_run }.to change { tracer.writer.spans.first }.to be_instance_of(Datadog::Span)
end

context 'when the job looks like Active Job' do
subject(:job_run) { Delayed::Job.enqueue(active_job_sample_job_object.new, job_params) }
subject(:span) { tracer.writer.spans.first }

before { job_run }

it 'has resource name equal to underlying ActiveJob class name' do
expect(span.resource).to eq('UnderlyingJobClass')
end
end

describe 'created span' do
subject(:span) { tracer.writer.spans.first }

Expand All @@ -57,7 +81,7 @@
end

it 'has resource name equal to job name' do
expect(span.resource).to eq(RSpec::Mocks::Double.name)
expect(span.resource).to eq('SampleJob')
end

it "span tags doesn't include queue name" do
Expand Down

0 comments on commit 0c7b262

Please sign in to comment.