class ExceptionDog::Handler

Constants

BACKTRACE_LINES
MAX_LINE_LENGTH
MAX_TEXT_LEGNTH
MAX_TITLE_LENGTH

Attributes

configuration[R]
logger[R]

Public Class Methods

current_trace_id() click to toggle source
# File lib/exception_dog/handler.rb, line 59
def self.current_trace_id
  context = Thread.current[:datadog_context]
  context&.trace_id
end
dd_trace_enabled() click to toggle source
# File lib/exception_dog/handler.rb, line 64
def self.dd_trace_enabled
  @dd_trace_enabled ||= Object.const_get('Datadog::Context') rescue false
end
new(configuration) click to toggle source
# File lib/exception_dog/handler.rb, line 10
def initialize(configuration)
  @configuration = configuration
  @logger = @configuration.logger
  @notifier = Object.const_get(@configuration.notifier).new(configuration)
end

Public Instance Methods

aggregation_key(exception) click to toggle source
# File lib/exception_dog/handler.rb, line 39
def aggregation_key(exception)
  "#{exception.class.name}-#{exception.message}-#{exception.backtrace&.first}".hash.to_s
end
attach_dd_trace_id(data) click to toggle source
# File lib/exception_dog/handler.rb, line 43
def attach_dd_trace_id(data)
  data[:trace_id] = "https://app.datadoghq.com/apm/trace/#{self.class.current_trace_id}" if self.class.current_trace_id
end
exception_text(exception, data) click to toggle source
# File lib/exception_dog/handler.rb, line 31
def exception_text(exception, data)
  detail = [exception.class.name[0..MAX_LINE_LENGTH], exception.message[0..MAX_LINE_LENGTH]]
  data.each do |key, val|
    detail << "#{key}: #{val && val.to_s[0..MAX_LINE_LENGTH]}"
  end
  (detail + format_backtrace(exception.backtrace)).compact.join("\n")
end
format_backtrace(backtrace) click to toggle source

remove backticks, single quotes, n and ensure each line is reasonably small

# File lib/exception_dog/handler.rb, line 52
def format_backtrace(backtrace)
  backtrace ||= []
  backtrace[0..BACKTRACE_LINES].collect do |line|
    "#{line.gsub(/\n|\`|\'/, '')}".split(//).last(MAX_LINE_LENGTH).join
  end
end
ignored(exception) click to toggle source
# File lib/exception_dog/handler.rb, line 47
def ignored(exception)
  configuration.ignore_exceptions&.include?(exception.class.name)
end
notify(exception, data) click to toggle source
# File lib/exception_dog/handler.rb, line 17
def notify(exception, data)
  return if ignored(exception)
  attach_dd_trace_id(data) if self.class.dd_trace_enabled
  title = exception.message[0..MAX_TITLE_LENGTH]
  text = exception_text(exception, data)[0..MAX_TEXT_LEGNTH]
  opts = {}
  opts[:priority] ||= 'normal'
  opts[:tags] = ["environment:#{configuration.environment}", "service:#{configuration.service_name}"] + configuration.tags
  opts[:aggregation_key] = aggregation_key(exception)
  opts[:source_type_name] = configuration.source_type_name
  opts[:alert_type] = 'error'
  @notifier.notify(title, text, opts)
end