diff --git a/lib/sidekiq/exception_utils.rb b/lib/sidekiq/exception_utils.rb deleted file mode 100644 index b56df47..0000000 --- a/lib/sidekiq/exception_utils.rb +++ /dev/null @@ -1,32 +0,0 @@ -# frozen_string_literal: true - -# 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 diff --git a/lib/sidekiq/logging/exception_utils.rb b/lib/sidekiq/logging/exception_utils.rb new file mode 100644 index 0000000..61875b5 --- /dev/null +++ b/lib/sidekiq/logging/exception_utils.rb @@ -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 diff --git a/lib/sidekiq/logging/shared.rb b/lib/sidekiq/logging/shared.rb index 3be3f8f..cc5cb65 100644 --- a/lib/sidekiq/logging/shared.rb +++ b/lib/sidekiq/logging/shared.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'sidekiq/exception_utils' +require 'sidekiq/logging/exception_utils' module Sidekiq module Logging @@ -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 @@ -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