Skip to content

Commit

Permalink
improve test coverage for nil and empty attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
mabdinur committed Jul 18, 2024
1 parent 9a7de18 commit 64e29a1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/datadog/opentelemetry/sdk/trace/span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def record_exception(exception, attributes: nil)
# take precedence over the type, message and stacktrace inferred from the exception object
type = attributes&.[]('exception.type') || exception.class.to_s
message = attributes&.[]('exception.message') || exception.message
stacktrace = attributes&.[]('exception.stacktrace') || exception.backtrace
stacktrace = attributes&.[]('exception.stacktrace') || exception.full_message(highlight: false, order: :top)
span.set_error_tags([type, message, stacktrace])
end
res
Expand Down
40 changes: 37 additions & 3 deletions spec/datadog/opentelemetry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@
context "attributes is #{attrs.inspect}" do
let(:attributes) { attrs }

it 'sets records an event and sets the status of the span to error' do
it 'sets records an exception event and sets span error tags using the Exception object' do
expect(span.events.count).to eq(1)
expect(span.events[0].name).to eq('exception')
expect(span.events[0].time_unix_nano / 1e9).to be_within(1).of(Time.now.to_f)
Expand All @@ -633,22 +633,56 @@
":in `full_message': Error (StandardError)"
)
expect(span).to_not have_error
expect(span).to have_error_message('Error')
expect(span).to have_error_stack(include(":in `full_message': Error (StandardError)"))
expect(span).to have_error_type('StandardError')
end
end
end

context 'with attributes' do
context 'with attributes containing nil values' do
let(:attributes) { { 'exception.stacktrace' => nil, 'exception.type' => nil, 'exception.message' => nil } }

it 'sets records an exception event and sets span error tags using the Exception object' do
expect(span.events.count).to eq(1)
expect(span.events[0].name).to eq('exception')
expect(span.events[0].attributes).to eq({})
expect(span).to_not have_error
expect(span).to have_error_message('Error')
expect(span).to have_error_stack(include(":in `full_message': Error (StandardError)"))
expect(span).to have_error_type('StandardError')
end
end

context 'with attributes containing empty values' do
let(:attributes) { { 'exception.stacktrace' => '', 'exception.type' => '', 'exception.message' => '' } }

it 'sets records an exception event and does NOT set span error tags' do
expect(span.events.count).to eq(1)
expect(span.events[0].name).to eq('exception')
expect(span.events[0].attributes).to eq(attributes)
expect(span).to_not have_error
expect(span).to_not have_error_message
expect(span).to_not have_error_stack
expect(span).to_not have_error_type
end
end

context 'with attributes containing exception stacktrace, type and message' do
let(:attributes) do
{ 'exception.stacktrace' => 'funny_stack', 'exception.type' => 'CustomError', 'exception.message' => 'NewError',
'candy' => true }
end

it 'sets records an event and sets the status of the span to error using attributes' do
it 'sets records an exception event and sets span error tags using the attributes hash' do
expect(span.events.count).to eq(1)
expect(span.events[0].name).to eq('exception')
expect(span.events[0].time_unix_nano / 1e9).to be_within(1).of(Time.now.to_f)
expect(span.events[0].attributes).to eq(attributes)
expect(span).to_not have_error
expect(span).to have_error_message('NewError')
expect(span).to have_error_stack("funny_stack")
expect(span).to have_error_type('CustomError')
end
end
end
Expand Down

0 comments on commit 64e29a1

Please sign in to comment.