Skip to content

Commit

Permalink
Add fork testing
Browse files Browse the repository at this point in the history
  • Loading branch information
marcotc committed Nov 20, 2020
1 parent 99c45d1 commit 764c8dd
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/ddtrace/contrib/qless/patcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def patch
# Instrument all Qless Workers
::Qless::Workers::BaseWorker.class_eval do
# These are executed in inverse order of listing here
include TracerCleaner
include QlessJob
include TracerCleaner
end
end

Expand Down
5 changes: 3 additions & 2 deletions lib/ddtrace/contrib/qless/tracer_cleaner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ module TracerCleaner
def around_perform(job)
return super unless datadog_configuration && tracer

tracer.shutdown! if forked?
super
super.tap do
tracer.shutdown! if forked?
end
end

private
Expand Down
46 changes: 46 additions & 0 deletions spec/ddtrace/contrib/qless/instrumentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,50 @@
end
end
end

context 'with forking' do
before do
# Ensures worker is using forking
expect(worker.class).to eq(Qless::Workers::ForkingWorker)
end

context 'trace context' do
subject(:perform) do
tracer.trace('parent.process') do |span|
@parent_span = span
perform_job(job_class)
end

expect(failed_jobs.count).to eq(0)
end

context 'on main process' do
it 'only contains parent process spans' do
perform

expect(span.name).to eq('parent.process')
end
end

context 'on child process' do
it 'does not include parent process spans' do
expect(job_class).to receive(:perform) do
# Mock #shutdown! in fork only
expect(tracer).to receive(:shutdown!).and_wrap_original do |m, *args|
m.call(*args)

expect(span.name).to eq('qless.job')
expect(span).to have_distributed_parent(@parent_span)
end
end

perform

# Remove hard-expectation from parent process,
# as `job_class#perform` won't be called in this process.
RSpec::Mocks.space.proxy_for(job_class).reset
end
end
end
end
end
13 changes: 13 additions & 0 deletions spec/support/span_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,17 @@ def description_of(actual) # rubocop:disable Lint/NestedMethodDefinition
define_have_error_tag(:message, Datadog::Ext::Errors::MSG)
define_have_error_tag(:stack, Datadog::Ext::Errors::STACK)
define_have_error_tag(:type, Datadog::Ext::Errors::TYPE)

# Distributed traces have the same trace_id and parent_id as upstream parent
# span, but don't actually share the same Context with the parent.
RSpec::Matchers.define :have_distributed_parent do |parent|
match do |actual|
@matcher = have_attributes(parent_id: parent.span_id, trace_id: parent.trace_id)
@matcher.matches? actual
end

failure_message do
@matcher.failure_message
end
end
end

0 comments on commit 764c8dd

Please sign in to comment.