class ExceptionNotifier::DatadogNotifier::DatadogExceptionEvent

Constants

ALERT_TYPE
MAX_BACKTRACE_SIZE
MAX_TITLE_LENGTH
MAX_VALUE_LENGTH

Attributes

exception[R]
options[R]

Public Class Methods

new(exception, options) click to toggle source
# File lib/exception_notifier/datadog_notifier.rb, line 40
def initialize(exception, options)
  @exception = exception
  @options = options
end

Public Instance Methods

backtrace() click to toggle source
# File lib/exception_notifier/datadog_notifier.rb, line 53
def backtrace
  @backtrace ||= exception.backtrace ? clean_backtrace(exception) : []
end
controller() click to toggle source
# File lib/exception_notifier/datadog_notifier.rb, line 49
def controller
  @controller ||= options[:env] && options[:env]['action_controller.instance']
end
event() click to toggle source
# 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_backtrace() click to toggle source
# 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_body() click to toggle source
# 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) click to toggle source
# File lib/exception_notifier/datadog_notifier.rb, line 97
def formatted_key_value(key, value)
  "**#{key}:** #{value}"
end
formatted_request() click to toggle source
# 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_session() click to toggle source
# 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_title() click to toggle source
# 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) click to toggle source
# 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
request() click to toggle source
# File lib/exception_notifier/datadog_notifier.rb, line 45
def request
  @request ||= ActionDispatch::Request.new(options[:env]) if options[:env]
end
tags() click to toggle source
# File lib/exception_notifier/datadog_notifier.rb, line 57
def tags
  options[:tags] || []
end
title_prefix() click to toggle source
# File lib/exception_notifier/datadog_notifier.rb, line 61
def title_prefix
  options[:title_prefix] || ''
end
truncate(string, max) click to toggle source
# File lib/exception_notifier/datadog_notifier.rb, line 136
def truncate(string, max)
  string.length > max ? "#{string[0...max]}..." : string
end

Private Instance Methods

controller_subtitle() click to toggle source
# File lib/exception_notifier/datadog_notifier.rb, line 151
def controller_subtitle
  "#{controller.controller_name} #{controller.action_name}" if controller
end