diff --git a/sdk/lib/opentelemetry/sdk/trace/tracer.rb b/sdk/lib/opentelemetry/sdk/trace/tracer.rb index 4a9ab340fb..1224b989ad 100644 --- a/sdk/lib/opentelemetry/sdk/trace/tracer.rb +++ b/sdk/lib/opentelemetry/sdk/trace/tracer.rb @@ -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 diff --git a/sdk/lib/opentelemetry/sdk/trace/tracer_provider.rb b/sdk/lib/opentelemetry/sdk/trace/tracer_provider.rb index 10a8ed8aea..3569451635 100644 --- a/sdk/lib/opentelemetry/sdk/trace/tracer_provider.rb +++ b/sdk/lib/opentelemetry/sdk/trace/tracer_provider.rb @@ -126,7 +126,7 @@ 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 @@ -134,8 +134,13 @@ def internal_start_span(name, kind, attributes, links, start_timestamp, parent_c 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 diff --git a/sdk/test/opentelemetry/sdk/trace/tracer_test.rb b/sdk/test/opentelemetry/sdk/trace/tracer_test.rb index 2ced763dd6..e5488d689b 100644 --- a/sdk/test/opentelemetry/sdk/trace/tracer_test.rb +++ b/sdk/test/opentelemetry/sdk/trace/tracer_test.rb @@ -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