Skip to content

Commit

Permalink
Add error_cause property to log message.
Browse files Browse the repository at this point in the history
  • Loading branch information
iMacTia committed Nov 29, 2024
1 parent d71ca62 commit 0de9ea2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
22 changes: 14 additions & 8 deletions lib/sidekiq/exception_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@

# Utility that allows us to get a hash representation of an exception
module ExceptionUtils
def self.get_exception_with_cause_hash(exc, parent_backtrace = nil, max_depth_left)
backtrace = exc.backtrace || []
if parent_backtrace
common_lines = backtrace.reverse.zip(parent_backtrace.reverse).take_while { |a, b| a == b }

backtrace = backtrace[0...-common_lines.length] if common_lines.any?
end
extend self

def get_exception_with_cause_hash(exc, parent_backtrace = nil, max_depth_left)
error_hash = {
'class' => exc.class.to_s,
'message' => exc.message,
'backtrace' => backtrace
'backtrace' => backtrace_for(exc, parent_backtrace)
}

if (cause = exc.cause) && max_depth_left.positive?
Expand All @@ -23,4 +18,15 @@ def self.get_exception_with_cause_hash(exc, parent_backtrace = nil, max_depth_le

error_hash
end

def backtrace_for(exception, parent_backtrace)
backtrace = exception.backtrace || []
if parent_backtrace
common_lines = backtrace.reverse.zip(parent_backtrace.reverse).take_while { |a, b| a == b }

backtrace = backtrace[0...-common_lines.length] if common_lines.any?
end

backtrace
end
end
9 changes: 8 additions & 1 deletion lib/sidekiq/logging/shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,15 @@ def log_job_exception(job, started_at, exc)
else
exc = exc.cause || exc if exc.is_a? Sidekiq::JobRetry::Handled
payload['error_message'] = exc.message
payload['error'] = exc.class
payload['error'] = exc.class.to_s
payload['error_backtrace'] = %('#{exc.backtrace.join("\n")}')
if (cause = exc.cause)
payload['error_cause'] = {
'class' => cause.class.to_s,
'message' => cause.message,
'backtrace' => ExceptionUtils.backtrace_for(cause, exc.backtrace)
}
end
end

process_payload(payload)
Expand Down
16 changes: 14 additions & 2 deletions spec/sidekiq/logstash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,31 @@ def process(worker, params = [], encrypt: false)
it 'logs the exception with job retry' do
expect { process(SpecWorker, [true]) }.to raise_error(RuntimeError)

expect(log_message['error_message']).to eq('You know nothing, Jon Snow.')
expect(log_message['error']).to eq('RuntimeError')
expect(log_message['error_message']).to eq('You know nothing, Jon Snow.')
expect(log_message['error_backtrace'].split("\n").first).to include('workers/spec_worker.rb:')
expect(log_message['error_cause']).to match(
hash_including(
'class' => 'RuntimeError',
'message' => 'Error rescuing error'
)
)
end

it 'logs the exception without job retry' do
allow(SpecWorker).to receive(:get_sidekiq_options).and_return({ 'retry' => false, 'queue' => 'default' })

expect { process(SpecWorker, [true]) }.to raise_error(RuntimeError)

expect(log_message['error_message']).to eq('You know nothing, Jon Snow.')
expect(log_message['error']).to eq('RuntimeError')
expect(log_message['error_message']).to eq('You know nothing, Jon Snow.')
expect(log_message['error_backtrace'].split("\n").first).to include('workers/spec_worker.rb:')
expect(log_message['error_cause']).to match(
hash_including(
'class' => 'RuntimeError',
'message' => 'Error rescuing error'
)
)
end

context 'log_job_exception_with_causes enabled' do
Expand Down

0 comments on commit 0de9ea2

Please sign in to comment.