Skip to content

Commit 6e9867e

Browse files
committed
Create separate method to get spans_count.
1 parent e1818ef commit 6e9867e

File tree

2 files changed

+33
-39
lines changed

2 files changed

+33
-39
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ def perform(action = :none, **_)
5151
end
5252
end
5353

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

@@ -67,19 +67,19 @@ def check_span_count_eq(count)
6767
it_behaves_like 'measured span for integration', true do
6868
before do
6969
dummy_worker_success
70-
check_span_count_eq(2)
70+
check_spans_count_eq(2)
7171
end
7272
end
7373

7474
it 'generates two spans, one for pushing to enqueue and one for the job itself' do
7575
is_expected.to be true
76-
check_span_count_eq(2)
76+
check_spans_count_eq(2)
7777
expect(spans.length).to eq(2)
7878
end
7979

8080
it 'instruments successful job' do
8181
is_expected.to be true
82-
check_span_count_eq(2)
82+
check_spans_count_eq(2)
8383

8484
expect(job_span.service).to eq(tracer.default_service)
8585
expect(job_span.name).to eq('sucker_punch.perform')
@@ -92,7 +92,7 @@ def check_span_count_eq(count)
9292

9393
it 'instruments successful enqueuing' do
9494
is_expected.to be true
95-
check_span_count_eq(2)
95+
check_spans_count_eq(2)
9696

9797
expect(enqueue_span.service).to eq(tracer.default_service)
9898
expect(enqueue_span.name).to eq('sucker_punch.perform_async')
@@ -113,13 +113,13 @@ def check_span_count_eq(count)
113113
it_behaves_like 'measured span for integration', true do
114114
before do
115115
dummy_worker_fail
116-
check_span_count_eq(2)
116+
check_spans_count_eq(2)
117117
end
118118
end
119119

120120
it 'instruments a failed job' do
121121
is_expected.to be true
122-
check_span_count_eq(2)
122+
check_spans_count_eq(2)
123123

124124
expect(job_span.service).to eq(tracer.default_service)
125125
expect(job_span.name).to eq('sucker_punch.perform')
@@ -140,13 +140,13 @@ def check_span_count_eq(count)
140140
it_behaves_like 'measured span for integration', true do
141141
before do
142142
dummy_worker_delay
143-
check_span_count_eq(2)
143+
check_spans_count_eq(2)
144144
end
145145
end
146146

147147
it 'instruments enqueuing for a delayed job' do
148148
dummy_worker_delay
149-
check_span_count_eq(2)
149+
check_spans_count_eq(2)
150150

151151
expect(enqueue_span.service).to eq(tracer.default_service)
152152
expect(enqueue_span.name).to eq('sucker_punch.perform_in')

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

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ 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
1714

1815
# Returns the current tracer instance
1916
def tracer
@@ -27,9 +24,7 @@ def traces
2724

2825
# Returns spans and caches it (similar to +let(:spans)+).
2926
def spans
30-
mutex.synchronize do
31-
@spans ||= fetch_spans_without_sorting
32-
end
27+
@spans ||= fetch_spans
3328
end
3429

3530
# Retrieves all traces in the current tracer instance.
@@ -41,44 +36,43 @@ def fetch_traces(tracer = self.tracer)
4136
# Retrieves and sorts all spans in the current tracer instance.
4237
# This method does not cache its results.
4338
def fetch_spans(tracer = self.tracer)
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
39+
traces = fetch_traces(tracer)
40+
spans = traces.collect(&:spans)
41+
spans.flatten.sort! do |a, b|
42+
if a.name == b.name
43+
if a.resource == b.resource
44+
if a.start_time == b.start_time
45+
a.end_time <=> b.end_time
5546
else
56-
a.resource <=> b.resource
47+
a.start_time <=> b.start_time
5748
end
5849
else
59-
a.name <=> b.name
50+
a.resource <=> b.resource
6051
end
52+
else
53+
a.name <=> b.name
6154
end
6255
end
6356
end
6457

65-
def fetch_spans_without_sorting(tracer = self.tracer)
58+
def get_spans_count(tracer = self.tracer)
59+
count = 0
6660
traces = fetch_traces(tracer)
67-
spans = traces.map { |trace| trace.instance_variable_get(:@spans) || [] }
68-
spans.flatten # gets spans for every trace in the tracer instance
61+
for trace in traces
62+
count += trace.instance_variable_get(:@spans).length
63+
end
64+
return count
6965
end
7066

7167
# Remove all traces from the current tracer instance and
7268
# busts cache of +#spans+ and +#span+.
7369
def clear_traces!
74-
mutex.synchronize do
75-
tracer.instance_variable_set(:@traces, [])
70+
tracer.instance_variable_set(:@traces, [])
7671

77-
@traces = nil
78-
@trace = nil
79-
@spans = nil
80-
@span = nil
81-
end
72+
@traces = nil
73+
@trace = nil
74+
@spans = nil
75+
@span = nil
8276
end
8377

8478
RSpec.configure do |config|

0 commit comments

Comments
 (0)