Skip to content

Use stack for mongo spy spans #1211

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

Merged
merged 4 commits into from
Dec 16, 2021
Merged
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
9 changes: 4 additions & 5 deletions lib/elastic_apm/spies/mongo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Subscriber
EVENT_KEY = :__elastic_instrumenter_mongo_events_key

def events
Thread.current[EVENT_KEY] ||= {}
Thread.current[EVENT_KEY] ||= []
end

def started(event)
Expand Down Expand Up @@ -72,7 +72,7 @@ def push_event(event)
# and the collection name is at the key `collection`
collection =
if event.command[event.command_name] == 1 ||
event.command[event.command_name].is_a?(BSON::Int64)
event.command[event.command_name].is_a?(BSON::Int64)
event.command[:collection]
else
event.command[event.command_name]
Expand All @@ -91,14 +91,13 @@ def push_event(event)
context: build_context(event)
)

events[event.operation_id] = span
events << span
end

def pop_event(event)
span = events.delete(event.operation_id)
return unless (curr = ElasticAPM.current_span)

curr == span && ElasticAPM.end_span
curr == events[-1] && ElasticAPM.end_span(events.pop)
end

def build_context(event)
Expand Down