Class: RailsErrorDashboard::Services::BacktraceProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_error_dashboard/services/backtrace_processor.rb

Overview

Pure algorithm service for backtrace processing Handles truncation and signature generation with no database access.

Class Method Summary collapse

Class Method Details

.calculate_signature(backtrace, locations: nil) ⇒ String?

Calculate a signature hash from backtrace for fuzzy similarity matching Extracts file paths and method names, ignoring line numbers, then produces an order-independent SHA256 digest.

Parameters:

  • backtrace (String, Array<String>, nil)

    The backtrace

  • locations (Array<Thread::Backtrace::Location>, nil) (defaults to: nil)

    Optional structured locations

Returns:

  • (String, nil)

    16-character hex signature



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rails_error_dashboard/services/backtrace_processor.rb', line 34

def self.calculate_signature(backtrace, locations: nil)
  # Try structured locations first (more reliable, no regex)
  if locations && !locations.empty?
    return signature_from_locations(locations)
  end

  return nil if backtrace.blank?

  lines = backtrace.is_a?(String) ? backtrace.split("\n") : backtrace
  frames = lines.first(20).map do |line|
    if line =~ %r{([^/]+\.rb):.*?in `(.+)'$}
      "#{Regexp.last_match(1)}:#{Regexp.last_match(2)}"
    elsif line =~ %r{([^/]+\.rb)}
      Regexp.last_match(1)
    end
  end.compact.uniq

  return nil if frames.empty?

  file_paths = frames.map { |frame| frame.split(":").first }.sort
  Digest::SHA256.hexdigest(file_paths.join("|"))[0..15]
end

.truncate(backtrace, max_lines: nil) ⇒ String?

Truncate backtrace to a maximum number of lines

Parameters:

  • backtrace (Array<String>, nil)

    The backtrace lines

  • max_lines (Integer) (defaults to: nil)

    Maximum lines to keep

Returns:

  • (String, nil)

    Truncated backtrace as a single string



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rails_error_dashboard/services/backtrace_processor.rb', line 12

def self.truncate(backtrace, max_lines: nil)
  return nil if backtrace.nil?

  max_lines ||= RailsErrorDashboard.configuration.max_backtrace_lines

  limited_backtrace = backtrace.first(max_lines)
  result = limited_backtrace.join("\n")

  if backtrace.length > max_lines
    truncation_notice = "... (#{backtrace.length - max_lines} more lines truncated)"
    result = result.empty? ? truncation_notice : result + "\n" + truncation_notice
  end

  result
end