Skip to content

Commit

Permalink
fix: Update untraced to suppress logging "Calling finish on an ende…
Browse files Browse the repository at this point in the history
…d Span" warnings (#1620)

* fix: Update `untraced` to suppress logging "Calling finish on an ended Span" warnings

* add tests for untraced

* disable Metrics/PerceivedComplexity in TracerProvider#internal_start_span

* remove setting ALWAYS_ON from test cases

* return a non-recording span without calling @sampler.should_sample

---------

Co-authored-by: Francis Bogsanyi <[email protected]>
  • Loading branch information
yoheyk and fbogsany authored Apr 23, 2024
1 parent 6bae4e1 commit a1f1629
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
2 changes: 0 additions & 2 deletions sdk/lib/opentelemetry/sdk/trace/tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ def start_root_span(name, attributes: nil, links: nil, start_timestamp: nil, kin

def start_span(name, with_parent: nil, attributes: nil, links: nil, start_timestamp: nil, kind: nil)
with_parent ||= Context.current
return super(name, with_parent: with_parent, attributes: attributes, links: links, start_timestamp: start_timestamp, kind: kind) if Common::Utilities.untraced?(with_parent)

name ||= 'empty'
kind ||= :internal

Expand Down
9 changes: 7 additions & 2 deletions sdk/lib/opentelemetry/sdk/trace/tracer_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,21 @@ def add_span_processor(span_processor)
end

# @api private
def internal_start_span(name, kind, attributes, links, start_timestamp, parent_context, instrumentation_scope) # rubocop:disable Metrics/MethodLength
def internal_start_span(name, kind, attributes, links, start_timestamp, parent_context, instrumentation_scope) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
parent_span = OpenTelemetry::Trace.current_span(parent_context)
parent_span_context = parent_span.context

if parent_span_context.valid?
parent_span_id = parent_span_context.span_id
trace_id = parent_span_context.trace_id
end

trace_id ||= @id_generator.generate_trace_id

if OpenTelemetry::Common::Utilities.untraced?(parent_context)
span_id = parent_span_id || @id_generator.generate_span_id
return OpenTelemetry::Trace.non_recording_span(OpenTelemetry::Trace::SpanContext.new(trace_id: trace_id, span_id: span_id))
end

result = @sampler.should_sample?(trace_id: trace_id, parent_context: parent_context, links: links, name: name, kind: kind, attributes: attributes)
span_id = @id_generator.generate_span_id
if result.recording? && !@stopped
Expand Down
23 changes: 23 additions & 0 deletions sdk/test/opentelemetry/sdk/trace/tracer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -329,5 +329,28 @@
_(span.status.code).must_equal(OpenTelemetry::Trace::Status::ERROR)
_(span.status.description).must_equal('Unhandled exception of type: RuntimeError')
end

it 'yields a no-op span within an untraced block' do
tracer.in_span('root') do
span_id = OpenTelemetry::Trace.current_span.context.span_id
OpenTelemetry::Common::Utilities.untraced do
tracer.in_span('op') do |span|
_(span).must_be_instance_of(OpenTelemetry::Trace::Span)
_(span.context.span_id).must_equal span_id
_(span.context.trace_flags).wont_be :sampled?
_(span).wont_be :recording?
end
end
end
end

it 'does not log "Calling finish on an ended Span" warnings when untraced? is called' do
OpenTelemetry::TestHelpers.with_test_logger do |log_stream|
tracer.in_span('root') do
OpenTelemetry::Common::Utilities.untraced { tracer.in_span('op') {} }
end
_(log_stream.string).wont_include 'Calling finish on an ended Span'
end
end
end
end

0 comments on commit a1f1629

Please sign in to comment.