Class: RightSupport::Stats::Exceptions

Inherits:
Object
  • Object
show all
Includes:
Log::Mixin
Defined in:
lib/right_support/stats/exceptions.rb

Overview

Track statistics for exceptions

Constant Summary collapse

MAX_RECENT_EXCEPTIONS =

Maximum number of recent exceptions to track per category

10

Constants included from Log::Mixin

Log::Mixin::Decorator

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Log::Mixin

default_logger, default_logger=, included

Constructor Details

#initialize(server = nil, callback = nil) ⇒ Exceptions

Initialize exception data

Parameters

server(Object)

Server where exceptions are originating, must be defined for callbacks

callback(Proc)

Block with following parameters to be activated when an exception occurs

exception(Exception)

Exception

message(Packet)

Message being processed

server(Server)

Server where exception occurred



46
47
48
49
50
# File 'lib/right_support/stats/exceptions.rb', line 46

def initialize(server = nil, callback = nil)
  @server = server
  @callback = callback
  reset
end

Instance Attribute Details

#statsObject (readonly) Also known as: all

(Hash) Exceptions raised per category with keys

"total"(Integer):: Total exceptions for this category
"recent"(Array):: Most recent as a hash of "count", "type", "message", "when", and "where"


35
36
37
# File 'lib/right_support/stats/exceptions.rb', line 35

def stats
  @stats
end

Instance Method Details

#resetObject

Reset statistics

Return

true

Always return true



56
57
58
59
# File 'lib/right_support/stats/exceptions.rb', line 56

def reset
  @stats = nil
  true
end

#track(category, exception, message = nil) ⇒ Object

Track exception statistics and optionally make callback to report exception Catch any exceptions since this function may be called from within an EM block and an exception here would then derail EM

Parameters

category(String)

Exception category

exception(Exception)

Exception

Return

true

Always return true



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/right_support/stats/exceptions.rb', line 71

def track(category, exception, message = nil)
  begin
    @callback.call(exception, message, @server) if @server && @callback && message
    @stats ||= {}
    exceptions = (@stats[category] ||= {"total" => 0, "recent" => []})
    exceptions["total"] += 1
    recent = exceptions["recent"]
    last = recent.last
    if last && last["type"] == exception.class.name && last["message"] == exception.message && last["where"] == exception.backtrace.first
      last["count"] += 1
      last["when"] = Time.now.to_i
    else
      backtrace = exception.backtrace.first if exception.backtrace
      recent.shift if recent.size >= MAX_RECENT_EXCEPTIONS
      recent.push({"count" => 1, "when" => Time.now.to_i, "type" => exception.class.name,
                   "message" => exception.message, "where" => backtrace})
    end
  rescue Exception => e
    logger.exception("Failed to track exception '#{exception}'", e, :trace) rescue nil
  end
  true
end