Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api/lib/opentelemetry/trace/tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Copy link

@kirs kirs Aug 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find that Unhandled might be misleading here. Maybe just Exception of type:?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unhandled was included in the message to indicate that the exception was unhandled at the time the span was finished, based on the language in the spec:

An exception SHOULD be recorded as an Event on the span during which it occurred if and only if it remains unhandled when the span ends and causes the span status to be set to ERROR.

It may be handled later, but it was not handled within the lifetime of the span.

The spec recommends:

When the status is set to Error by Instrumentation Libraries, the Description SHOULD be documented and predictable.

Sadly, we're violating this other recommendation, but I think changing the description would be more problematic at this point:

It's NOT RECOMMENDED to duplicate status code or error.type in span status description.

raise e
ensure
Expand Down
12 changes: 12 additions & 0 deletions sdk/test/opentelemetry/sdk/trace/tracer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading