Class: ShieldPressTracker::ErrorCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/shieldpress_tracker/collectors.rb

Instance Method Summary collapse

Constructor Details

#initialize(maximum) ⇒ ErrorCollector

Returns a new instance of ErrorCollector.



45
# File 'lib/shieldpress_tracker/collectors.rb', line 45

def initialize(maximum); @maximum, @events, @mutex = maximum, [], Mutex.new end

Instance Method Details

#capture(error, meta = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/shieldpress_tracker/collectors.rb', line 46

def capture(error, meta = {})
  event = { message: error.to_s[0, 4096], type: error.class.name, timestamp: Time.now.utc.iso8601 }
  event[:stack] = Array(error.backtrace).join("\n")[0, 4096] if error.respond_to?(:backtrace)
  meta = meta.dup
  { url: :url, method: :method, status_code: :statusCode }.each { |from, to| event[to] = meta.delete(from) if meta.key?(from) }
  event[:meta] = meta unless meta.empty?
  @mutex.synchronize { @events << event; @events = @events.last(@maximum) }
rescue StandardError
  nil
end

#flushObject



56
# File 'lib/shieldpress_tracker/collectors.rb', line 56

def flush; @mutex.synchronize { rows = @events; @events = []; rows } end