Module: ErrorRadar::SpikeDetector

Defined in:
lib/error_radar/spike_detector.rb

Overview

Detects sudden error-rate spikes for a given fingerprint.

Strategy (in order of preference):

1. If track_occurrences is on — query error_radar_occurrences for exact
 count in the window. Accurate across processes and survives restarts.
2. In-memory ring buffer per fingerprint. Fast, zero extra DB queries, but
 resets on process restart and is per-worker (not shared across Puma workers).

Constant Summary collapse

MUTEX =
Mutex.new
HITS =

fingerprint => Array

Hash.new { |h, k| h[k] = [] }
MAX_HITS_PER_KEY =

Cap per-key buffer to avoid unbounded growth on very high-volume errors

1000

Class Method Summary collapse

Class Method Details

.check(log) ⇒ Object

Returns the number of recent hits if >= config threshold, false otherwise.



29
30
31
32
33
34
35
36
# File 'lib/error_radar/spike_detector.rb', line 29

def self.check(log)
  threshold = ErrorRadar.config.spike_threshold.to_i
  window    = ErrorRadar.config.spike_window_minutes.to_i
  return false unless threshold > 0 && window > 0

  count = recent_count(log, window)
  count >= threshold ? count : false
end

.record_hit(fingerprint) ⇒ Object

Record a hit in the in-memory ring buffer. Called by Notifier before spike detection so the current hit is counted.



20
21
22
23
24
25
26
# File 'lib/error_radar/spike_detector.rb', line 20

def self.record_hit(fingerprint)
  MUTEX.synchronize do
    arr = HITS[fingerprint]
    arr << Time.current
    arr.shift while arr.size > MAX_HITS_PER_KEY
  end
end