diff --git a/api/lib/opentelemetry/trace/tracer.rb b/api/lib/opentelemetry/trace/tracer.rb index 2e30e87c9b..3739941619 100644 --- a/api/lib/opentelemetry/trace/tracer.rb +++ b/api/lib/opentelemetry/trace/tracer.rb @@ -31,12 +31,12 @@ class Tracer # # @yield [span, context] yields the newly created span and a context containing the # span to the block. - def in_span(name, attributes: nil, links: nil, start_timestamp: nil, kind: nil) + def in_span(name, attributes: nil, links: nil, start_timestamp: nil, kind: nil, record_exception: true) span = nil span = start_span(name, attributes: attributes, links: links, start_timestamp: start_timestamp, kind: kind) Trace.with_span(span) { |s, c| yield s, c } rescue Exception => e # rubocop:disable Lint/RescueException - span&.record_exception(e) + span&.record_exception(e) if record_exception span&.status = Status.error("Unhandled exception of type: #{e.class}") raise e ensure diff --git a/sdk/test/opentelemetry/sdk/trace/tracer_test.rb b/sdk/test/opentelemetry/sdk/trace/tracer_test.rb index 88ae0b5179..cbd11bc1f5 100644 --- a/sdk/test/opentelemetry/sdk/trace/tracer_test.rb +++ b/sdk/test/opentelemetry/sdk/trace/tracer_test.rb @@ -330,6 +330,18 @@ _(span.status.description).must_equal('Unhandled exception of type: RuntimeError') end + it 'does not record an exception if record_exception is false' do + span = nil + _(proc do + tracer.in_span('op', record_exception: false) do |s| + span = s + raise 'this is fine' + end + end).must_raise(RuntimeError) + + _(span.events).must_be_nil + 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