From c1477915d4ada6cd1695dff95c45741e26bc226a Mon Sep 17 00:00:00 2001 From: Franco Date: Tue, 3 Dec 2024 11:26:56 +0100 Subject: [PATCH 1/3] Namespace ExceptionUtils module --- lib/sidekiq/exception_utils.rb | 32 ----------------------- lib/sidekiq/logging/exception_utils.rb | 36 ++++++++++++++++++++++++++ lib/sidekiq/logging/shared.rb | 4 +-- 3 files changed, 38 insertions(+), 34 deletions(-) delete mode 100644 lib/sidekiq/exception_utils.rb create mode 100644 lib/sidekiq/logging/exception_utils.rb 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..408eca4 --- /dev/null +++ b/lib/sidekiq/logging/exception_utils.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +# Utility that allows us to get a hash representation of an exception +module Sidekiq + module Logging + 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..caf2aed 100644 --- a/lib/sidekiq/logging/shared.rb +++ b/lib/sidekiq/logging/shared.rb @@ -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 From 6e527c6b1dd2e06172fd678d094c08899ef63c96 Mon Sep 17 00:00:00 2001 From: Franco Date: Tue, 3 Dec 2024 11:31:46 +0100 Subject: [PATCH 2/3] fix warning --- lib/sidekiq/logging/exception_utils.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sidekiq/logging/exception_utils.rb b/lib/sidekiq/logging/exception_utils.rb index 408eca4..61875b5 100644 --- a/lib/sidekiq/logging/exception_utils.rb +++ b/lib/sidekiq/logging/exception_utils.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -# Utility that allows us to get a hash representation of an exception module Sidekiq module Logging + # Utility that allows us to get a hash representation of an exception module ExceptionUtils module_function From cc9a3bcbebefa4a61b9c4d95409f997ad5656cb4 Mon Sep 17 00:00:00 2001 From: Franco Date: Tue, 3 Dec 2024 11:55:07 +0100 Subject: [PATCH 3/3] fix require paths --- lib/sidekiq/logging/shared.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sidekiq/logging/shared.rb b/lib/sidekiq/logging/shared.rb index caf2aed..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