Skip to content

Commit 5b972d5

Browse files
committed
Create separate method to get spans_count.
1 parent e485c12 commit 5b972d5

File tree

3 files changed

+32
-44
lines changed

3 files changed

+32
-44
lines changed

run_test.rb

Lines changed: 0 additions & 4 deletions
This file was deleted.

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: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +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
17-
1814
# Returns the current tracer instance
1915
def tracer
2016
Datadog::Tracing.send(:tracer)
@@ -27,9 +23,7 @@ def traces
2723

2824
# Returns spans and caches it (similar to +let(:spans)+).
2925
def spans
30-
mutex.synchronize do
31-
@spans ||= fetch_spans_without_sorting
32-
end
26+
@spans ||= fetch_spans
3327
end
3428

3529
# Retrieves all traces in the current tracer instance.
@@ -41,44 +35,42 @@ def fetch_traces(tracer = self.tracer)
4135
# Retrieves and sorts all spans in the current tracer instance.
4236
# This method does not cache its results.
4337
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
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
5544
else
56-
a.resource <=> b.resource
45+
a.start_time <=> b.start_time
5746
end
5847
else
59-
a.name <=> b.name
48+
a.resource <=> b.resource
6049
end
50+
else
51+
a.name <=> b.name
6152
end
6253
end
6354
end
6455

65-
def fetch_spans_without_sorting(tracer = self.tracer)
56+
def get_spans_count(tracer = self.tracer)
57+
count = 0
6658
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
59+
traces.each do |trace|
60+
count += trace.instance_variable_get(:@spans).length
61+
end
62+
count
6963
end
7064

7165
# Remove all traces from the current tracer instance and
7266
# busts cache of +#spans+ and +#span+.
7367
def clear_traces!
74-
mutex.synchronize do
75-
tracer.instance_variable_set(:@traces, [])
68+
tracer.instance_variable_set(:@traces, [])
7669

77-
@traces = nil
78-
@trace = nil
79-
@spans = nil
80-
@span = nil
81-
end
70+
@traces = nil
71+
@trace = nil
72+
@spans = nil
73+
@span = nil
8274
end
8375

8476
RSpec.configure do |config|

0 commit comments

Comments
 (0)