Sha256: 1b8c8b248a9c7121efdadf775b8403523c3dd2f42f2dab173b1d0d6ee0556e84

Contents?: true

Size: 1.53 KB

Versions: 10

Compression:

Stored size: 1.53 KB

Contents

module Logatron
  class BacktraceCleaner
    # Suppress irrelevant "external" frames. In practice, these are often the frames
    # leading up to the "entry point" we care about, whether that's a rails controller
    # or a rabbit_stew handler.
    #
    # Algorithm: partition frames into contiguous external/internal chunks.
    # Starting at the bottom, suppress chunks until we find the entry point chunk.

    def clean(backtrace)
      all_chunks = backtrace.chunk { |x| external_frame?(x) }.to_a.reverse
      good_chunks = all_chunks.drop_while { |(is_ext, frames)| !entry_point_chunk?(is_ext, frames) }
      bad_chunks = all_chunks.take(all_chunks.size - good_chunks.size)
      good_bt = good_chunks.reverse.map(&:last).flatten
      bad_bt = bad_chunks.reverse.map(&:last).flatten
      good_bt + suppress(bad_bt)
    rescue
      # if something went wrong, give up on cleaning the backtrace
      backtrace
    end

    private

    # heuristic for determining externality of a frame.
    # Note: bundle/gems does not include git-sourced gems (those go in
    # bundle/bundler/gems), so git-sourced gems are considered internal.
    def external_frame?(f)
      !!(f =~ %r{bundle/gems})
    end

    # heuristic for finding the entry point
    def entry_point_chunk?(is_ext, frames)
      !is_ext && frames.size >= 3
    end

    def suppress(backtrace)
      if backtrace.size <= 3
        backtrace
      else
        [ backtrace.first,
          "... #{backtrace.size - 2} frames suppressed ...",
          backtrace.last ]
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
logatron-0.16.1 lib/logatron/backtrace_cleaner.rb
logatron-0.16.0 lib/logatron/backtrace_cleaner.rb
logatron-0.15.0 lib/logatron/backtrace_cleaner.rb
logatron-0.14.0 lib/logatron/backtrace_cleaner.rb
logatron-0.13.0 lib/logatron/backtrace_cleaner.rb
logatron-0.12.0 lib/logatron/backtrace_cleaner.rb
logatron-0.11.0 lib/logatron/backtrace_cleaner.rb
logatron-0.10.0 lib/logatron/backtrace_cleaner.rb
logatron-0.9.0 lib/logatron/backtrace_cleaner.rb
logatron-0.8.0 lib/logatron/backtrace_cleaner.rb