Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shoryuken: use job name as resource instead of the wrapper #671

Merged
merged 2 commits into from
Jan 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/ddtrace/contrib/shoryuken/tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def initialize(options = {})

def call(worker_instance, queue, sqs_msg, body)
@tracer.trace(Ext::SPAN_JOB, service: @shoryuken_service, span_type: Datadog::Ext::AppTypes::WORKER) do |span|
span.resource = worker_instance.class.name
span.resource = resource(worker_instance, body)
span.set_tag(Ext::TAG_JOB_ID, sqs_msg.message_id)
span.set_tag(Ext::TAG_JOB_QUEUE, queue)
span.set_tag(Ext::TAG_JOB_ATTRIBUTES, sqs_msg.attributes) if sqs_msg.respond_to?(:attributes)
Expand All @@ -23,6 +23,14 @@ def call(worker_instance, queue, sqs_msg, body)

private

def resource(worker_instance, body)
# If it's a Hash, try to get the job class from it.
# This is for ActiveJob compatibility.
job_class = body['job_class'] if body.is_a?(Hash)
# If nothing is available, use the worker class name.
job_class || worker_instance.class.name
end

def set_service_info(service)
return if @tracer.services[service]
@tracer.set_service_info(
Expand Down
65 changes: 59 additions & 6 deletions spec/ddtrace/contrib/shoryuken/tracer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,82 @@
require 'shoryuken'

RSpec.describe Datadog::Contrib::Shoryuken::Tracer do
let(:tracer) { ::Datadog::Tracer.new(writer: FauxWriter.new) }
let(:options) { { tracer: tracer } }
let(:shoryuken_tracer) { described_class.new }

let(:tracer) { get_test_tracer }
let(:configuration_options) { { tracer: tracer } }
let(:spans) { tracer.writer.spans }
let(:span) { spans.first }

before do
Shoryuken.worker_executor = Shoryuken::Worker::InlineExecutor

Datadog.configure do |c|
c.use :shoryuken, options
c.use :shoryuken, configuration_options
end
end

context 'when a Shoryuken::Worker class' do
subject(:worker_class) do
after { Datadog.registry[:shoryuken].reset_configuration! }

shared_context 'Shoryuken::Worker' do
let(:worker_class) do
qn = queue_name
stub_const('TestWorker', Class.new do
include Shoryuken::Worker
shoryuken_options queue: qn
def perform(sqs_msg, body); end
end)
end
let(:worker) { worker_class.new }
let(:queue_name) { 'default' }
end

describe '#call' do
subject(:call) do
shoryuken_tracer.call(worker, queue_name, sqs_msg, body) do
worker.perform(sqs_msg, body)
end
end

# TODO: Convert this to an instance double, to verify stub.
let(:sqs_msg) { double('sqs_msg', message_id: message_id, attributes: attributes) }
let(:message_id) { SecureRandom.uuid }
let(:attributes) { {} }

include_context 'Shoryuken::Worker'

before do
expect { call }.to_not raise_error
expect(spans).to have(1).items
expect(span.name).to eq(Datadog::Contrib::Shoryuken::Ext::SPAN_JOB)
expect(span.get_tag(Datadog::Contrib::Shoryuken::Ext::TAG_JOB_ID)).to eq(message_id)
expect(span.get_tag(Datadog::Contrib::Shoryuken::Ext::TAG_JOB_QUEUE)).to eq(queue_name)
expect(span.get_tag(Datadog::Contrib::Shoryuken::Ext::TAG_JOB_ATTRIBUTES)).to eq(attributes.to_s)
end

context 'with a body' do
context 'that is a Hash' do
context 'that contains \'job_class\'' do
let(:body) { { 'job_class' => job_class } }
let(:job_class) { 'MyJob' }
it { expect(span.resource).to eq(job_class) }
end

context 'that does not contain \'job_class\'' do
let(:body) { {} }
it { expect(span.resource).to eq('TestWorker') }
end
end

context 'that is a String' do
let(:body) { 'my body' }
it { expect(span.resource).to eq('TestWorker') }
end
end
end

context 'when a Shoryuken::Worker class' do
include_context 'Shoryuken::Worker'

describe '#perform_async' do
subject(:perform_async) { worker_class.perform_async(body) }
Expand All @@ -43,7 +96,7 @@ def perform(sqs_msg, body); end
# TODO: These expectations do not work because Shoryuken doesn't run middleware in tests
# https://github.com/phstc/shoryuken/issues/541
# expect(spans).to have(1).items
# expect(span.name).to_not eq(Datadog::Contrib::Shoryuken::Ext::SPAN_JOB)
# expect(span.name).to eq(Datadog::Contrib::Shoryuken::Ext::SPAN_JOB)
# TODO: Stub OpenStruct mock SQS message created by InlineExecutor with data
# https://github.com/phstc/shoryuken/blob/master/lib/shoryuken/worker/inline_executor.rb#L9
# expect(span.get_tag(Datadog::Contrib::Shoryuken::Ext::TAG_JOB_ID)).to eq(message_id)
Expand Down