Class: RailsInformant::ErrorRecorder

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_informant/error_recorder.rb

Constant Summary collapse

MAX_OCCURRENCES_PER_GROUP =
25
OCCURRENCE_COOLDOWN =

seconds

5

Class Method Summary collapse

Class Method Details

.record(error, severity: "error", context: {}, source: nil, env: nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rails_informant/error_recorder.rb', line 14

def record(error, severity: "error", context: {}, source: nil, env: nil)
  # Never capture from an interactive console — the operator sees errors
  # live, so recording and alerting on them is noise.
  return if RailsInformant.console_mode?
  # `rails runner` is the same shape when an operator drives it by hand, but it also
  # backs cron and scheduled tasks, where those errors are the whole point. Apps that
  # only use it interactively opt out; capturing stays the default.
  return if RailsInformant.runner_mode? && !RailsInformant.config.capture_runner_errors
  return unless RailsInformant.initialized?
  return if self_caused_error?(error)

  now = Time.current
  attrs = ContextBuilder.group_attributes(error, severity:, context:, env:, now:)
  fingerprint = Fingerprint.generate(error)

  event = run_before_record_callbacks(error, attrs, env:, fingerprint:)
  return if event.halted?

  fingerprint = event.fingerprint
  attrs[:severity] = event.severity if %w[error warning info].include?(event.severity)

  group = ErrorGroup.find_or_create_for(fingerprint, attrs)
  group.detect_regression!

  return if spike_limit_exceeded?(group)

  store_occurrence(group, error, env:, context:) if should_store_occurrence?(group)
  notify(group)
rescue StandardError => e
  Rails.logger.error "[RailsInformant] Capture failed: #{e.class}: #{e.message}\n#{e.backtrace&.first(5)&.join("\n")}"
end

.reset_spike_counters!Object



10
11
12
# File 'lib/rails_informant/error_recorder.rb', line 10

def reset_spike_counters!
  @_spike_counters.clear
end