Skip to content

Commit

Permalink
feat(support): add initial backtrace cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
marian13 committed Dec 9, 2023
1 parent 6416691 commit 14733f3
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/convenient_service/dependencies/extractions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
# https://github.com/marian13/convenient_service/wiki/Docs:-Dependencies
#
require_relative "extractions/active_support_concern"
require_relative "extractions/active_support_backtrace_cleaner"
require_relative "extractions/ruby_middleware"
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
# @internal
# NOTE:
# Copied from `rails/rails` without any logic modification.
# Version: v7.0.4.3.
# Version: v7.1.2.
# Wrapped in a namespace `ConvenientService::Dependencies::Extractions::ActiveSupportBacktraceCleaner`.
#
# - https://api.rubyonrails.org/v7.0.4.3/classes/ActiveSupport/BacktraceCleaner.html
# - https://github.com/rails/rails/blob/v7.0.4.3/activesupport/lib/active_support/backtrace_cleaner.rb
# - https://api.rubyonrails.org/v7.1.2/classes/ActiveSupport/BacktraceCleaner.html
# - https://github.com/rails/rails/blob/v7.1.2/activesupport/lib/active_support/backtrace_cleaner.rb
# - https://github.com/marian13/rails/blob/main/activesupport/lib/active_support/backtrace_cleaner.rb
# - https://github.com/rails/rails
#
Expand Down
1 change: 1 addition & 0 deletions lib/convenient_service/support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

require_relative "support/abstract_method"
require_relative "support/arguments"
require_relative "support/backtrace_cleaner"
require_relative "support/cache"
require_relative "support/castable"
require_relative "support/command"
Expand Down
88 changes: 88 additions & 0 deletions lib/convenient_service/support/backtrace_cleaner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# frozen_string_literal: true

##
# @note Other gems are also trying to integrate with Rails Backtrace Cleaner.
# @see https://github.com/sidekiq/sidekiq/pull/5796
# @see https://github.com/sidekiq/sidekiq/issues/5589
# @see https://github.com/appsignal/appsignal-ruby/issues/380
#
module ConvenientService
module Support
##
# Rails v7.1.2 Backtrace Cleaner descendant.
# Has Convenient Service specific silencer - `add_convenient_service_silencer`.
# By default, it uses only `add_stdlib_silencer` and `add_convenient_service_silencer`.
#
# @see https://api.rubyonrails.org/classes/ActiveSupport/BacktraceCleaner.html
# @see https://medium.com/one-medical-technology/filtering-ruby-backtraces-for-debugging-4df75133ab71
# @see https://twitter.com/websebdev/status/1375831554518360065?ref_src=twsrc%5Etfw%7Ctwcamp%5Etweetembed%7Ctwterm%5E1375831554518360065%7Ctwgr%5E459595174e71c0f1aa3826c89f2ffca0c3da6ea3%7Ctwcon%5Es1_&ref_url=https%3A%2F%2Fwww.redditmedia.com%2Fmediaembed%2Fmegii9%2F%3Fresponsive%3Dtrueis_nightmode%3Dtrue
# @see https://stackoverflow.com/questions/22727219/using-backtracecleaner-in-rails-console
#
class BacktraceCleaner < Dependencies::Extractions::ActiveSupportBacktraceCleaner::BacktraceCleaner
##
# @return [void]
#
# @see https://api.rubyonrails.org/classes/ActiveSupport/BacktraceCleaner.html
# @see https://github.com/rails/rails/blob/v7.1.2/activesupport/lib/active_support/backtrace_cleaner.rb#L34
#
def initialize(...)
super

remove_filters!
remove_silencers!

##
# NOTE: Uses `::RbConfig` to resolve stdlib directory.
# - https://idiosyncratic-ruby.com/42-ruby-config.html
# - https://github.com/ruby/ruby/blob/master/tool/mkconfig.rb
#
add_stdlib_silencer

##
# NOTE:
#
add_convenient_service_silencer
end

##
# @note `add_gem_filter` is made public to have a way to bring back Rails Backtrace Cleaner default behavior if necessary.
#
# @return [void]
#
# @see https://github.com/rails/rails/blob/v7.1.2/activesupport/lib/active_support/backtrace_cleaner.rb#L110
#
public :add_gem_filter

##
# @note `add_gem_silencer` is made public to have a way to bring back Rails Backtrace Cleaner default behavior if necessary.
#
# @return [void]
#
# @see https://github.com/rails/rails/blob/v7.1.2/activesupport/lib/active_support/backtrace_cleaner.rb#L119
#
public :add_gem_silencer

##
# @note `add_stdlib_silencer` is made public to allow the end-user to adjust clean in any way.
#
# @return [void]
#
# @see https://github.com/rails/rails/blob/v7.1.2/activesupport/lib/active_support/backtrace_cleaner.rb#L123
#
public :add_stdlib_silencer

##
# Since Convenient Service is using middleware chains under the hood, exception backtraces may be huge.
# As a consequence, it takes too much time during the debugging process to find the application line of code that causes the exception.
# This silencer removes all Convenient Service lines from backtraces.
#
# @note To bring back Convenient Service lines to backtraces, use the `remove_silencers` or check the `CleansExceptionBacktrace` plugin.
#
# @return [void]
#
def add_convenient_service_silencer
add_silencer { |line| /convenient_service/.match?(line) }
end
end
end
end

0 comments on commit 14733f3

Please sign in to comment.