Module: Bugstack::Fingerprint

Defined in:
lib/bugstack/fingerprint.rb

Class Method Summary collapse

Class Method Details

.extract_location(exception) ⇒ Array<(String, String, String, Integer)>

Extract file, function, and line from an exception’s backtrace.

Parameters:

  • exception (Exception)

Returns:

  • (Array<(String, String, String, Integer)>)
    exception_type, file, function, line


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bugstack/fingerprint.rb', line 26

def self.extract_location(exception)
  exception_type = exception.class.name

  bt = exception.backtrace
  return [exception_type, "", "", nil] if bt.nil? || bt.empty?

  # Parse the first backtrace line: "file:line:in `method'"
  first_line = bt.first
  if first_line =~ /\A(.+):(\d+):in [`'](.+)'\z/
    file = Regexp.last_match(1)
    line = Regexp.last_match(2).to_i
    function = Regexp.last_match(3)
    [exception_type, file, function, line]
  else
    [exception_type, first_line, "", nil]
  end
end

.format_backtrace(exception) ⇒ String

Format an exception’s backtrace as a string.

Parameters:

  • exception (Exception)

Returns:

  • (String)


48
49
50
51
52
53
54
55
# File 'lib/bugstack/fingerprint.rb', line 48

def self.format_backtrace(exception)
  bt = exception.backtrace
  return "#{exception.class}: #{exception.message}" if bt.nil? || bt.empty?

  lines = ["#{exception.class}: #{exception.message}"]
  bt.each { |frame| lines << "  from #{frame}" }
  lines.join("\n")
end

.generate(exception_type, file, function, line = nil) ⇒ String

Generate a stable SHA-256 fingerprint for an error.

Parameters:

  • exception_type (String)
  • file (String)
  • function (String)
  • line (Integer, nil) (defaults to: nil)

Returns:

  • (String)

    16-char hex fingerprint



14
15
16
17
18
19
# File 'lib/bugstack/fingerprint.rb', line 14

def self.generate(exception_type, file, function, line = nil)
  parts = [exception_type, file, function]
  parts << line.to_s if line
  key = parts.join(":")
  Digest::SHA256.hexdigest(key)[0, 16]
end