Skip to content

Commit

Permalink
Create separate method to get spans_count.
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahchen6 committed Feb 6, 2025
1 parent e1818ef commit d1767ee
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 44 deletions.
4 changes: 0 additions & 4 deletions run_test.rb

This file was deleted.

20 changes: 10 additions & 10 deletions spec/datadog/tracing/contrib/sucker_punch/patcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ def perform(action = :none, **_)
end
end

def check_span_count_eq(count)
def check_spans_count_eq(count)
mutex.synchronize do
try_wait_until { fetch_spans.length == count }
try_wait_until { get_spans_count == count }
end
end

Expand All @@ -67,19 +67,19 @@ def check_span_count_eq(count)
it_behaves_like 'measured span for integration', true do
before do
dummy_worker_success
check_span_count_eq(2)
check_spans_count_eq(2)
end
end

it 'generates two spans, one for pushing to enqueue and one for the job itself' do
is_expected.to be true
check_span_count_eq(2)
check_spans_count_eq(2)
expect(spans.length).to eq(2)
end

it 'instruments successful job' do
is_expected.to be true
check_span_count_eq(2)
check_spans_count_eq(2)

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

it 'instruments successful enqueuing' do
is_expected.to be true
check_span_count_eq(2)
check_spans_count_eq(2)

expect(enqueue_span.service).to eq(tracer.default_service)
expect(enqueue_span.name).to eq('sucker_punch.perform_async')
Expand All @@ -113,13 +113,13 @@ def check_span_count_eq(count)
it_behaves_like 'measured span for integration', true do
before do
dummy_worker_fail
check_span_count_eq(2)
check_spans_count_eq(2)
end
end

it 'instruments a failed job' do
is_expected.to be true
check_span_count_eq(2)
check_spans_count_eq(2)

expect(job_span.service).to eq(tracer.default_service)
expect(job_span.name).to eq('sucker_punch.perform')
Expand All @@ -140,13 +140,13 @@ def check_span_count_eq(count)
it_behaves_like 'measured span for integration', true do
before do
dummy_worker_delay
check_span_count_eq(2)
check_spans_count_eq(2)
end
end

it 'instruments enqueuing for a delayed job' do
dummy_worker_delay
check_span_count_eq(2)
check_spans_count_eq(2)

expect(enqueue_span.service).to eq(tracer.default_service)
expect(enqueue_span.name).to eq('sucker_punch.perform_in')
Expand Down
52 changes: 22 additions & 30 deletions spec/datadog/tracing/contrib/support/tracer_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ module Contrib
# For contrib, we only allow one tracer to be active:
# the global tracer in +Datadog::Tracing+.
module TracerHelpers
def mutex
@mutex ||= Mutex.new
end

# Returns the current tracer instance
def tracer
Datadog::Tracing.send(:tracer)
Expand All @@ -27,9 +23,7 @@ def traces

# Returns spans and caches it (similar to +let(:spans)+).
def spans
mutex.synchronize do
@spans ||= fetch_spans_without_sorting
end
@spans ||= fetch_spans
end

# Retrieves all traces in the current tracer instance.
Expand All @@ -41,44 +35,42 @@ def fetch_traces(tracer = self.tracer)
# Retrieves and sorts all spans in the current tracer instance.
# This method does not cache its results.
def fetch_spans(tracer = self.tracer)
mutex.synchronize do
traces = fetch_traces(tracer)
spans = traces.collect(&:spans)
spans.flatten.sort! do |a, b|
if a.name == b.name
if a.resource == b.resource
if a.start_time == b.start_time
a.end_time <=> b.end_time
else
a.start_time <=> b.start_time
end
traces = fetch_traces(tracer)
traces.collect(&:spans).flatten.sort! do |a, b|
if a.name == b.name
if a.resource == b.resource
if a.start_time == b.start_time
a.end_time <=> b.end_time
else
a.resource <=> b.resource
a.start_time <=> b.start_time
end
else
a.name <=> b.name
a.resource <=> b.resource
end
else
a.name <=> b.name
end
end
end

def fetch_spans_without_sorting(tracer = self.tracer)
def get_spans_count(tracer = self.tracer)
count = 0
traces = fetch_traces(tracer)
spans = traces.map { |trace| trace.instance_variable_get(:@spans) || [] }
spans.flatten # gets spans for every trace in the tracer instance
for trace in traces
count += trace.instance_variable_get(:@spans).length
end
count
end

# Remove all traces from the current tracer instance and
# busts cache of +#spans+ and +#span+.
def clear_traces!
mutex.synchronize do
tracer.instance_variable_set(:@traces, [])
tracer.instance_variable_set(:@traces, [])

@traces = nil
@trace = nil
@spans = nil
@span = nil
end
@traces = nil
@trace = nil
@spans = nil
@span = nil
end

RSpec.configure do |config|
Expand Down

0 comments on commit d1767ee

Please sign in to comment.