Class: ExceptionNotifier::DatadogNotifier::DatadogExceptionEvent

Inherits:
Object
  • Object
show all
Includes:
BacktraceCleaner
Defined in:
lib/exception_notifier/datadog_notifier.rb

Constant Summary collapse

MAX_TITLE_LENGTH =
120
MAX_VALUE_LENGTH =
300
MAX_BACKTRACE_SIZE =
3
ALERT_TYPE =
'error'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BacktraceCleaner

#clean_backtrace

Constructor Details

#initialize(exception, options) ⇒ DatadogExceptionEvent

Returns a new instance of DatadogExceptionEvent.



40
41
42
43
# File 'lib/exception_notifier/datadog_notifier.rb', line 40

def initialize(exception, options)
  @exception = exception
  @options = options
end

Instance Attribute Details

#exceptionObject (readonly)

Returns the value of attribute exception.



37
38
39
# File 'lib/exception_notifier/datadog_notifier.rb', line 37

def exception
  @exception
end

#optionsObject (readonly)

Returns the value of attribute options.



37
38
39
# File 'lib/exception_notifier/datadog_notifier.rb', line 37

def options
  @options
end

Instance Method Details

#backtraceObject



53
54
55
# File 'lib/exception_notifier/datadog_notifier.rb', line 53

def backtrace
  @backtrace ||= exception.backtrace ? clean_backtrace(exception) : []
end

#controllerObject



49
50
51
# File 'lib/exception_notifier/datadog_notifier.rb', line 49

def controller
  @controller ||= options[:env] && options[:env]['action_controller.instance']
end

#eventObject



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/exception_notifier/datadog_notifier.rb', line 65

def event
  title = formatted_title
  body = formatted_body

  Dogapi::Event.new(
    body,
    msg_title: title,
    alert_type: ALERT_TYPE,
    tags: tags,
    aggregation_key: [title]
  )
end

#formatted_backtraceObject



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/exception_notifier/datadog_notifier.rb', line 124

def formatted_backtrace
  size = [backtrace.size, MAX_BACKTRACE_SIZE].min

  text = []
  text << '### **Backtrace**'
  text << '````'
  size.times { |i| text << backtrace[i] }
  text << '````'
  text << '___'
  text.join("\n")
end

#formatted_bodyObject



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/exception_notifier/datadog_notifier.rb', line 85

def formatted_body
  text = []

  text << '%%%'
  text << formatted_request if request
  text << formatted_session if request
  text << formatted_backtrace
  text << '%%%'

  text.join("\n")
end

#formatted_key_value(key, value) ⇒ Object



97
98
99
# File 'lib/exception_notifier/datadog_notifier.rb', line 97

def formatted_key_value(key, value)
  "**#{key}:** #{value}"
end

#formatted_requestObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/exception_notifier/datadog_notifier.rb', line 101

def formatted_request
  text = []
  text << '### **Request**'
  text << formatted_key_value('URL', request.url)
  text << formatted_key_value('HTTP Method', request.request_method)
  text << formatted_key_value('IP Address', request.remote_ip)
  text << formatted_key_value('Parameters', request.filtered_parameters.inspect)
  text << formatted_key_value('Timestamp', Time.current)
  text << formatted_key_value('Server', Socket.gethostname)
  text << formatted_key_value('Rails root', Rails.root) if defined?(Rails) && Rails.respond_to?(:root)
  text << formatted_key_value('Process', $PROCESS_ID)
  text << '___'
  text.join("\n")
end

#formatted_sessionObject



116
117
118
119
120
121
122
# File 'lib/exception_notifier/datadog_notifier.rb', line 116

def formatted_session
  text = []
  text << '### **Session**'
  text << formatted_key_value('Data', request.session.to_hash)
  text << '___'
  text.join("\n")
end

#formatted_titleObject



78
79
80
81
82
83
# File 'lib/exception_notifier/datadog_notifier.rb', line 78

def formatted_title
  title =
    "#{title_prefix}#{controller_subtitle} (#{exception.class}) #{exception.message.inspect}"

  truncate(title, MAX_TITLE_LENGTH)
end

#inspect_object(object) ⇒ Object



140
141
142
143
144
145
146
147
# File 'lib/exception_notifier/datadog_notifier.rb', line 140

def inspect_object(object)
  case object
  when Hash, Array
    truncate(object.inspect, MAX_VALUE_LENGTH)
  else
    object.to_s
  end
end

#requestObject



45
46
47
# File 'lib/exception_notifier/datadog_notifier.rb', line 45

def request
  @request ||= ActionDispatch::Request.new(options[:env]) if options[:env]
end

#tagsObject



57
58
59
# File 'lib/exception_notifier/datadog_notifier.rb', line 57

def tags
  options[:tags] || []
end

#title_prefixObject



61
62
63
# File 'lib/exception_notifier/datadog_notifier.rb', line 61

def title_prefix
  options[:title_prefix] || ''
end

#truncate(string, max) ⇒ Object



136
137
138
# File 'lib/exception_notifier/datadog_notifier.rb', line 136

def truncate(string, max)
  string.length > max ? "#{string[0...max]}..." : string
end