Skip to content

Commit a1f1629

Browse files
yoheykfbogsany
andauthored
fix: Update untraced to suppress logging "Calling finish on an ended 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]>
1 parent 6bae4e1 commit a1f1629

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

sdk/lib/opentelemetry/sdk/trace/tracer.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ def start_root_span(name, attributes: nil, links: nil, start_timestamp: nil, kin
2929

3030
def start_span(name, with_parent: nil, attributes: nil, links: nil, start_timestamp: nil, kind: nil)
3131
with_parent ||= Context.current
32-
return super(name, with_parent: with_parent, attributes: attributes, links: links, start_timestamp: start_timestamp, kind: kind) if Common::Utilities.untraced?(with_parent)
33-
3432
name ||= 'empty'
3533
kind ||= :internal
3634

sdk/lib/opentelemetry/sdk/trace/tracer_provider.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,21 @@ def add_span_processor(span_processor)
126126
end
127127

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

133133
if parent_span_context.valid?
134134
parent_span_id = parent_span_context.span_id
135135
trace_id = parent_span_context.trace_id
136136
end
137-
138137
trace_id ||= @id_generator.generate_trace_id
138+
139+
if OpenTelemetry::Common::Utilities.untraced?(parent_context)
140+
span_id = parent_span_id || @id_generator.generate_span_id
141+
return OpenTelemetry::Trace.non_recording_span(OpenTelemetry::Trace::SpanContext.new(trace_id: trace_id, span_id: span_id))
142+
end
143+
139144
result = @sampler.should_sample?(trace_id: trace_id, parent_context: parent_context, links: links, name: name, kind: kind, attributes: attributes)
140145
span_id = @id_generator.generate_span_id
141146
if result.recording? && !@stopped

sdk/test/opentelemetry/sdk/trace/tracer_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,5 +329,28 @@
329329
_(span.status.code).must_equal(OpenTelemetry::Trace::Status::ERROR)
330330
_(span.status.description).must_equal('Unhandled exception of type: RuntimeError')
331331
end
332+
333+
it 'yields a no-op span within an untraced block' do
334+
tracer.in_span('root') do
335+
span_id = OpenTelemetry::Trace.current_span.context.span_id
336+
OpenTelemetry::Common::Utilities.untraced do
337+
tracer.in_span('op') do |span|
338+
_(span).must_be_instance_of(OpenTelemetry::Trace::Span)
339+
_(span.context.span_id).must_equal span_id
340+
_(span.context.trace_flags).wont_be :sampled?
341+
_(span).wont_be :recording?
342+
end
343+
end
344+
end
345+
end
346+
347+
it 'does not log "Calling finish on an ended Span" warnings when untraced? is called' do
348+
OpenTelemetry::TestHelpers.with_test_logger do |log_stream|
349+
tracer.in_span('root') do
350+
OpenTelemetry::Common::Utilities.untraced { tracer.in_span('op') {} }
351+
end
352+
_(log_stream.string).wont_include 'Calling finish on an ended Span'
353+
end
354+
end
332355
end
333356
end

0 commit comments

Comments
 (0)