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

Fix Mongo NoMethodError when thread variable not set #675

Merged
merged 1 commit 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
26 changes: 21 additions & 5 deletions lib/ddtrace/contrib/mongodb/subscribers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ def started(event)
# https://github.com/mongodb/mongo-ruby-driver/blob/master/lib/mongo/monitoring.rb#L70
# https://github.com/mongodb/mongo-ruby-driver/blob/master/lib/mongo/monitoring/publishable.rb#L38-L56
span = pin.tracer.trace(Ext::SPAN_COMMAND, service: pin.service, span_type: Ext::SPAN_TYPE_COMMAND)
Thread.current[:datadog_mongo_span] ||= {}
Thread.current[:datadog_mongo_span][event.request_id] = span
set_span(event, span)

# build a quantized Query using the Parser module
query = MongoDB.query_builder(event.command_name, event.database_name, event.command)
Expand All @@ -38,7 +37,7 @@ def started(event)
end

def failed(event)
span = Thread.current[:datadog_mongo_span][event.request_id]
span = get_span(event)
return unless span

# the failure is not a real exception because it's handled by
Expand All @@ -50,11 +49,11 @@ def failed(event)
# whatever happens, the Span must be removed from the local storage and
# it must be finished to prevent any leak
span.finish unless span.nil?
Thread.current[:datadog_mongo_span].delete(event.request_id)
clear_span(event)
end

def succeeded(event)
span = Thread.current[:datadog_mongo_span][event.request_id]
span = get_span(event)
return unless span

# add fields that are available only after executing the query
Expand All @@ -66,6 +65,23 @@ def succeeded(event)
# whatever happens, the Span must be removed from the local storage and
# it must be finished to prevent any leak
span.finish unless span.nil?
clear_span(event)
end

private

def get_span(event)
Thread.current[:datadog_mongo_span] \
&& Thread.current[:datadog_mongo_span][event.request_id]
end

def set_span(event, span)
Thread.current[:datadog_mongo_span] ||= {}
Thread.current[:datadog_mongo_span][event.request_id] = span
end

def clear_span(event)
return if Thread.current[:datadog_mongo_span].nil?
Thread.current[:datadog_mongo_span].delete(event.request_id)
end
end
Expand Down
11 changes: 11 additions & 0 deletions spec/ddtrace/contrib/mongodb/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,17 @@ def discard_spans!
expect(span.status).to eq(1)
expect(span.get_tag('error.msg')).to eq('ns not found (26)')
end

context 'that triggers #failed before #started' do
subject(:failed_event) { subscriber.failed(event) }
let(:event) { instance_double(Mongo::Monitoring::Event::CommandFailed, request_id: double('request_id')) }
let(:subscriber) { Datadog::Contrib::MongoDB::MongoCommandSubscriber.new }

# Clear the thread variable out, as if #started has never run.
before(:each) { Thread.current[:datadog_mongo_span] = nil }

it { expect { failed_event }.to_not raise_error }
end
end

describe 'with LDAP/SASL authentication' do
Expand Down