Class: ErrorRadar::ErrorLog

Inherits:
ApplicationRecord show all
Defined in:
app/models/error_radar/error_log.rb

Overview

One row per distinct failure (collapsed by fingerprint). Doubles as a task on the triage board. Table: error_radar_error_logs.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_fingerprint(category:, error_class:, source:, message:) ⇒ Object



96
97
98
99
100
101
102
103
# File 'app/models/error_radar/error_log.rb', line 96

def self.build_fingerprint(category:, error_class:, source:, message:)
  normalized = message.to_s
                      .gsub(/\d+/, '#')                        # ids, counts, timestamps
                      .gsub(/0x[0-9a-f]+/i, '0x#')             # object addresses
                      .gsub(/[0-9a-f]{8}-[0-9a-f-]{27}/i, '#') # uuids
                      .strip
  ::Digest::SHA1.hexdigest([category, error_class, source, normalized].join('|'))
end

.presence(value) ⇒ Object



109
110
111
# File 'app/models/error_radar/error_log.rb', line 109

def self.presence(value)
  value.respond_to?(:empty?) ? (value.empty? ? nil : value) : value
end

.record(category:, message:, severity: :error, error_class: nil, source: nil, backtrace: nil, context: {}, http_status: nil, request_url: nil, api_code: nil, api_subcode: nil, fingerprint: nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/models/error_radar/error_log.rb', line 52

def self.record(category:, message:, severity: :error, error_class: nil, source: nil,
                backtrace: nil, context: {}, http_status: nil, request_url: nil,
                api_code: nil, api_subcode: nil, fingerprint: nil)
  now = Time.current
  fp  = presence(fingerprint) || build_fingerprint(category: category, error_class: error_class, source: source, message: message)

  log             = find_or_initialize_by(fingerprint: fp)
  new_fingerprint = !log.persisted?

  if log.persisted?
    log.occurrences += 1
    log.status = :open if log.status_resolved? || log.status_ignored?
  else
    log.assign_attributes(
      category: category, severity: severity, error_class: error_class, source: source,
      http_status: http_status, request_url: request_url, api_code: api_code, api_subcode: api_subcode,
      first_seen_at: now, status: :open
    )
  end

  log.message      = message.to_s.truncate(ErrorRadar.config.max_message_length)
  log.backtrace    = presence(Array(backtrace).join("\n")) || log.backtrace
  log.context      = (log.context || {}).merge(context.presence || {}).deep_stringify_keys if context.present? || log.context
  log.severity     = severity if log.new_record? || severity_rank(severity) > severity_rank(log.severity)
  log.last_seen_at = now
  log.save!
  log.instance_variable_set(:@new_fingerprint, new_fingerprint)

  if ErrorRadar.config.track_occurrences
    ErrorRadar::ErrorOccurrence.record_for(
      log,
      context:     context,
      backtrace:   backtrace,
      http_status: http_status,
      request_url: request_url
    )
  end

  log
rescue StandardError => e
  ErrorRadar::Tracking.warn_internal("ErrorLog.record failed: #{e.class}: #{e.message}")
  nil
end

.severity_rank(value) ⇒ Object



105
106
107
# File 'app/models/error_radar/error_log.rb', line 105

def self.severity_rank(value)
  severities[value.to_s] || 0
end

Instance Method Details

#new_fingerprint?Boolean

Record (or roll-up) an error. Idempotent per fingerprint: identical errors increment occurrences and bump last_seen_at instead of creating a new row. NEVER raises — logging must not break the calling code path.

Returns:

  • (Boolean)


48
49
50
# File 'app/models/error_radar/error_log.rb', line 48

def new_fingerprint?
  @new_fingerprint || false
end

#reopen!Object



121
122
123
# File 'app/models/error_radar/error_log.rb', line 121

def reopen!
  update!(status: :open, resolved_at: nil)
end

#resolve!(by: nil, note: nil) ⇒ Object



117
118
119
# File 'app/models/error_radar/error_log.rb', line 117

def resolve!(by: nil, note: nil)
  update!(status: :resolved, resolved_at: Time.current, resolved_by: by, resolution_note: note.presence || resolution_note)
end

#short_messageObject



113
114
115
# File 'app/models/error_radar/error_log.rb', line 113

def short_message
  message.to_s.truncate(120)
end