Skip to content

Commit a6ce85b

Browse files
authored
feat: add record_exception option for in_span (#1911)
* feat: add record_exception option for in_span
1 parent 7f5db11 commit a6ce85b

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

api/lib/opentelemetry/trace/tracer.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ class Tracer
3131
#
3232
# @yield [span, context] yields the newly created span and a context containing the
3333
# span to the block.
34-
def in_span(name, attributes: nil, links: nil, start_timestamp: nil, kind: nil)
34+
def in_span(name, attributes: nil, links: nil, start_timestamp: nil, kind: nil, record_exception: true)
3535
span = nil
3636
span = start_span(name, attributes: attributes, links: links, start_timestamp: start_timestamp, kind: kind)
3737
Trace.with_span(span) { |s, c| yield s, c }
3838
rescue Exception => e # rubocop:disable Lint/RescueException
39-
span&.record_exception(e)
39+
span&.record_exception(e) if record_exception
4040
span&.status = Status.error("Unhandled exception of type: #{e.class}")
4141
raise e
4242
ensure

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,18 @@
330330
_(span.status.description).must_equal('Unhandled exception of type: RuntimeError')
331331
end
332332

333+
it 'does not record an exception if record_exception is false' do
334+
span = nil
335+
_(proc do
336+
tracer.in_span('op', record_exception: false) do |s|
337+
span = s
338+
raise 'this is fine'
339+
end
340+
end).must_raise(RuntimeError)
341+
342+
_(span.events).must_be_nil
343+
end
344+
333345
it 'yields a no-op span within an untraced block' do
334346
tracer.in_span('root') do
335347
span_id = OpenTelemetry::Trace.current_span.context.span_id

0 commit comments

Comments
 (0)