Skip to content

Commit

Permalink
add record exception api
Browse files Browse the repository at this point in the history
  • Loading branch information
mabdinur committed Jul 16, 2024
1 parent 3b7fa64 commit c1cd64f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
14 changes: 14 additions & 0 deletions lib/datadog/opentelemetry/sdk/trace/span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ def set_attribute(key, value)
res
end

def record_exception(exception, attributes: nil)
res = super
if (span = datadog_span)
span.set_error(
[
attributes&.fetch('exception.type', nil) || exception.class.to_s,
attributes&.fetch('exception.message', nil) || exception.message,
attributes&.fetch('exception.stacktrace', nil) || exception.backtrace
]
)
end
res
end

# `alias` performed to match {OpenTelemetry::SDK::Trace::Span} aliasing upstream
alias []= set_attribute

Expand Down
57 changes: 51 additions & 6 deletions spec/datadog/opentelemetry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,51 @@
let(:setter) { :set_attribute }
end

describe '#record_exception' do
subject! do
start_span
start_span.record_exception(StandardError.new('Error'), attributes: attributes)
start_span.finish
end

let(:start_span) { otel_tracer.start_span('start-span', **span_options) }
let(:active_span) { Datadog::Tracing.active_span }

context 'without attributes' do
let(:attributes) { nil }
it 'sets records an event and sets the status of the span to error' 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.keys).to match_array(
['exception.message', 'exception.type',
'exception.stacktrace']
)
expect(span.events[0].attributes['exception.message']).to eq('Error')
expect(span.events[0].attributes['exception.type']).to eq('StandardError')
expect(span.events[0].attributes['exception.stacktrace']).to include(":in `full_message': Error (StandardError)")

expect(span).to have_error
end
end

context 'with attributes' 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
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 have_error
end
end
end

describe '#[]=' do
include_context 'Span#set_attribute'
let(:setter) { :[]= }
Expand All @@ -629,28 +674,28 @@
describe '#add_event' do
subject! do
start_span
start_span.add_event('Exception was raised!', **event_options)
start_span.add_event('Exception was raised!', attributes: attributes, timestamp: timestamp)
start_span.finish
end

let(:start_span) { otel_tracer.start_span('start-span', **span_options) }
let(:active_span) { Datadog::Tracing.active_span }

context 'with name, attributes and timestamp' do
let(:event_options) do
{ attributes: { raised: false, handler: 'default', count: 1 }, timestamp: 17206369349 }
end
let(:attributes) { { 'raised' => false, 'handler' => 'default', 'count' => 1 } }
let(:timestamp) { 17206369349 }

it 'adds one event to the span' do
expect(span.events.count).to eq(1)
expect(span.events[0].name).to eq('Exception was raised!')
expect(span.events[0].time_unix_nano).to eq(17206369349000000000)
expect(span.events[0].attributes).to eq({ raised: false, handler: 'default', count: 1 })
expect(span.events[0].attributes).to eq(attributes)
end
end

context 'without a timestamp or attributes' do
let(:event_options) { {} }
let(:attributes) { nil }
let(:timestamp) { nil }

it 'adds one event with timestamp set to the current time and attributes set to an empty hash' do
expect(span.events.count).to eq(1)
Expand Down

0 comments on commit c1cd64f

Please sign in to comment.