Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Namespace ExceptionUtils module #40

Merged
merged 3 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
32 changes: 0 additions & 32 deletions lib/sidekiq/exception_utils.rb

This file was deleted.

36 changes: 36 additions & 0 deletions lib/sidekiq/logging/exception_utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

module Sidekiq
module Logging
# Utility that allows us to get a hash representation of an exception
module ExceptionUtils
module_function

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

if (cause = exc.cause) && max_depth_left.positive?
# Pass the current backtrace as the parent_backtrace to the cause to shorten cause's backtrace list
error_hash['cause'] = get_exception_with_cause_hash(cause, exc.backtrace, max_depth_left: max_depth_left - 1)
end

error_hash
end

def backtrace_for(exception, parent_backtrace = nil)
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
end
end
6 changes: 3 additions & 3 deletions lib/sidekiq/logging/shared.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'sidekiq/exception_utils'
require 'sidekiq/logging/exception_utils'

module Sidekiq
module Logging
Expand Down Expand Up @@ -54,7 +54,7 @@ def log_job_exception(job, started_at, exc)

config = Sidekiq::Logstash.configuration
if config.log_job_exception_with_causes
payload['error'] = ExceptionUtils.get_exception_with_cause_hash(
payload['error'] = Sidekiq::Logging::ExceptionUtils.get_exception_with_cause_hash(
exc, max_depth_left: config.causes_logging_max_depth
)
else
Expand All @@ -66,7 +66,7 @@ def log_job_exception(job, started_at, exc)
payload['error_cause'] = {
'class' => cause.class.to_s,
'message' => cause.message,
'backtrace' => ExceptionUtils.backtrace_for(cause, exc.backtrace)
'backtrace' => Sidekiq::Logging::ExceptionUtils.backtrace_for(cause, exc.backtrace)
}
end
end
Expand Down
Loading