Skip to content

Commit d19a649

Browse files
authored
Use stack for mongo spy spans (#1211)
* Use stack for mongo spy spans * Add mutex so that the correct current transaction and span are consulted * current span and transaction are thread locals so we don't need mutexes * Fix spacing
1 parent f4aaa26 commit d19a649

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

lib/elastic_apm/spies/mongo.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Subscriber
3838
EVENT_KEY = :__elastic_instrumenter_mongo_events_key
3939

4040
def events
41-
Thread.current[EVENT_KEY] ||= {}
41+
Thread.current[EVENT_KEY] ||= []
4242
end
4343

4444
def started(event)
@@ -72,7 +72,7 @@ def push_event(event)
7272
# and the collection name is at the key `collection`
7373
collection =
7474
if event.command[event.command_name] == 1 ||
75-
event.command[event.command_name].is_a?(BSON::Int64)
75+
event.command[event.command_name].is_a?(BSON::Int64)
7676
event.command[:collection]
7777
else
7878
event.command[event.command_name]
@@ -91,14 +91,13 @@ def push_event(event)
9191
context: build_context(event)
9292
)
9393

94-
events[event.operation_id] = span
94+
events << span
9595
end
9696

9797
def pop_event(event)
98-
span = events.delete(event.operation_id)
9998
return unless (curr = ElasticAPM.current_span)
10099

101-
curr == span && ElasticAPM.end_span
100+
curr == events[-1] && ElasticAPM.end_span(events.pop)
102101
end
103102

104103
def build_context(event)

0 commit comments

Comments
 (0)