Class: ErrorStalker::ExceptionReport

Inherits:
Object
  • Object
show all
Defined in:
lib/error_stalker/exception_report.rb

Overview

An ExceptionReport contains all of the information we have on an exception, which can then be transformed into whatever format is needed for further investigation. Some data stores may override this class, but they should be able to be treated as instances of this class regardless.

Constant Summary collapse

STACK_DIGEST_LENGTH =

The number of characters in this exception’s stacktrace that should be used to uniquify this exception. Exceptions raised from the same place should have the same stacktrace, up to STACK_DIGEST_LENGTH characters.

4096

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ ExceptionReport

Build a new ExceptionReport. params[:application] is a string identifying the application or component the exception was sent from, params[:exception] is the exception object you want to report (or a string error message), and params[:data] is any extra arbitrary data you want to log along with this report.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/error_stalker/exception_report.rb', line 42

def initialize(params = {})
  params = symbolize_keys(params)
  @id = params[:id]
  @application = params[:application]
  @machine = params[:machine] || machine_name
  @timestamp = params[:timestamp] || Time.now
  @type = params[:type] || params[:exception].class.name
  
  if params[:exception].is_a?(Exception)
    @exception = params[:exception].to_s
  else
    @exception = params[:exception]
  end
  
  @data = params[:data]
  
  if params[:backtrace]
    @backtrace = params[:backtrace]
  else
    @backtrace = params[:exception].backtrace if params[:exception].is_a?(Exception)
  end

  @digest = params[:digest] if params[:digest]
end

Instance Attribute Details

#applicationObject (readonly)

The name of the application that caused this exception.



12
13
14
# File 'lib/error_stalker/exception_report.rb', line 12

def application
  @application
end

#backtraceObject (readonly)

The backtrace corresponding to this exception. Should be an array of strings, each referring to a single stack frame.



31
32
33
# File 'lib/error_stalker/exception_report.rb', line 31

def backtrace
  @backtrace
end

#dataObject (readonly)

A hash of extra data logged along with this exception.



27
28
29
# File 'lib/error_stalker/exception_report.rb', line 27

def data
  @data
end

#exceptionObject (readonly)

The exception object (or string message) this report represents



24
25
26
# File 'lib/error_stalker/exception_report.rb', line 24

def exception
  @exception
end

#idObject

A unique identifier for this exception



34
35
36
# File 'lib/error_stalker/exception_report.rb', line 34

def id
  @id
end

#machineObject (readonly)

The name of the machine that raised this exception.



15
16
17
# File 'lib/error_stalker/exception_report.rb', line 15

def machine
  @machine
end

#timestampObject (readonly)

The time that this exception occurred



18
19
20
# File 'lib/error_stalker/exception_report.rb', line 18

def timestamp
  @timestamp
end

#typeObject (readonly)

The class name of exception



21
22
23
# File 'lib/error_stalker/exception_report.rb', line 21

def type
  @type
end

Instance Method Details

#digestObject

Generate a ‘mostly-unique’ hash code for this exception, that should be the same for similar exceptions and different for different exceptions. This is used to group similar exceptions together.



77
78
79
# File 'lib/error_stalker/exception_report.rb', line 77

def digest
  @digest ||= Digest::MD5.hexdigest((backtrace ? backtrace.to_s[0,STACK_DIGEST_LENGTH] : exception.to_s) + type.to_s)
end

#to_jsonObject

Serialize this object to json, so we can send it over the wire.



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/error_stalker/exception_report.rb', line 82

def to_json
  {
    :application => application,
    :machine => machine,
    :timestamp => timestamp,
    :type => type,
    :exception => exception,
    :data => data,
    :backtrace => backtrace
  }.to_json
end