Skip to content

Commit e1818ef

Browse files
committed
Separate out spans' call to fetch_spans.
1 parent 9aa6492 commit e1818ef

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

spec/datadog/tracing/contrib/sucker_punch/patcher_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def perform(action = :none, **_)
5353

5454
def check_span_count_eq(count)
5555
mutex.synchronize do
56-
try_wait_until { fetch_spans_without_sorting.length == count }
56+
try_wait_until { fetch_spans.length == count }
5757
end
5858
end
5959

spec/datadog/tracing/contrib/support/tracer_helpers.rb

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ module Contrib
1111
# For contrib, we only allow one tracer to be active:
1212
# the global tracer in +Datadog::Tracing+.
1313
module TracerHelpers
14+
def mutex
15+
@mutex ||= Mutex.new
16+
end
17+
1418
# Returns the current tracer instance
1519
def tracer
1620
Datadog::Tracing.send(:tracer)
@@ -23,7 +27,9 @@ def traces
2327

2428
# Returns spans and caches it (similar to +let(:spans)+).
2529
def spans
26-
@spans ||= fetch_spans
30+
mutex.synchronize do
31+
@spans ||= fetch_spans_without_sorting
32+
end
2733
end
2834

2935
# Retrieves all traces in the current tracer instance.
@@ -35,39 +41,44 @@ def fetch_traces(tracer = self.tracer)
3541
# Retrieves and sorts all spans in the current tracer instance.
3642
# This method does not cache its results.
3743
def fetch_spans(tracer = self.tracer)
38-
traces = fetch_traces(tracer)
39-
traces.collect(&:spans).flatten.sort! do |a, b|
40-
if a.name == b.name
41-
if a.resource == b.resource
42-
if a.start_time == b.start_time
43-
a.end_time <=> b.end_time
44+
mutex.synchronize do
45+
traces = fetch_traces(tracer)
46+
spans = traces.collect(&:spans)
47+
spans.flatten.sort! do |a, b|
48+
if a.name == b.name
49+
if a.resource == b.resource
50+
if a.start_time == b.start_time
51+
a.end_time <=> b.end_time
52+
else
53+
a.start_time <=> b.start_time
54+
end
4455
else
45-
a.start_time <=> b.start_time
56+
a.resource <=> b.resource
4657
end
4758
else
48-
a.resource <=> b.resource
59+
a.name <=> b.name
4960
end
50-
else
51-
a.name <=> b.name
5261
end
5362
end
5463
end
5564

5665
def fetch_spans_without_sorting(tracer = self.tracer)
5766
traces = fetch_traces(tracer)
58-
spans = traces.collect(&:spans)
67+
spans = traces.map { |trace| trace.instance_variable_get(:@spans) || [] }
5968
spans.flatten # gets spans for every trace in the tracer instance
6069
end
6170

6271
# Remove all traces from the current tracer instance and
6372
# busts cache of +#spans+ and +#span+.
6473
def clear_traces!
65-
tracer.instance_variable_set(:@traces, [])
74+
mutex.synchronize do
75+
tracer.instance_variable_set(:@traces, [])
6676

67-
@traces = nil
68-
@trace = nil
69-
@spans = nil
70-
@span = nil
77+
@traces = nil
78+
@trace = nil
79+
@spans = nil
80+
@span = nil
81+
end
7182
end
7283

7384
RSpec.configure do |config|

0 commit comments

Comments
 (0)