Class: LcpRuby::Metrics::ErrorRecorder

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

Class Method Summary collapse

Class Method Details

.record(error_class:, message:, stacktrace:, context: {}) ⇒ Object

Records an error to the error log table with rate limiting and atomic upsert.

Parameters:

  • error_class (String)
  • message (String)
  • stacktrace (Array<String>)
  • context (Hash) (defaults to: {})


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/lcp_ruby/metrics/error_recorder.rb', line 11

def record(error_class:, message:, stacktrace:, context: {})
  return unless error_log_active?

  fingerprint = Fingerprint.compute(error_class, message.to_s, stacktrace || [])

  if RateLimiter.should_write?(fingerprint)
    pending = RateLimiter.drain_count(fingerprint)
    upsert(fingerprint, error_class, message, stacktrace, context, pending)
    RateLimiter.mark_written(fingerprint)
  else
    RateLimiter.increment(fingerprint)
  end
rescue StandardError => e
  raise if defined?(Rails) && Rails.env.local?
  Rails.logger.error("[LcpRuby::Metrics] ErrorRecorder failed: #{e.class}: #{e.message}") if defined?(Rails)
end