Class: Sapience::Appender::Sentry

Inherits:
Subscriber show all
Defined in:
lib/sapience/appender/sentry.rb

Instance Attribute Summary

Attributes inherited from Subscriber

#application, #formatter, #host

Attributes inherited from Base

#filter, #name

Instance Method Summary collapse

Methods inherited from Subscriber

#close, #flush, #level

Methods inherited from Base

#fast_tag, #level, #level=, #measure, #payload, #pop_tags, #push_tags, #silence, #tagged, #tags, #with_payload

Constructor Details

#initialize(options = {}, &block) ⇒ Sentry

Create Appender

Parameters

level: [:trace | :debug | :info | :warn | :error | :fatal]
  Override the log level for this appender.
  Default: :error

formatter: [Object|Proc|Symbol|Hash]
  An instance of a class that implements #call, or a Proc to be used to format
  the output from this appender
  Default: Use the built-in formatter (See: #call)

filter: [Regexp|Proc]
  RegExp: Only include log messages where the class name matches the supplied.
  regular expression. All other messages will be ignored.
  Proc: Only include log messages where the supplied Proc returns true
        The Proc must return true or false.

host: [String]
  Name of this host to appear in log messages.
  Default: Sapience.config.host

application: [String]
  Name of this application to appear in log messages.
  Default: Sapience.config.application


41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sapience/appender/sentry.rb', line 41

def initialize(options = {}, &block)
  validate_options!(options)

  options[:level] ||= :error
  Raven.configure do |config|
    config.dsn  = options.delete(:dsn)
    config.tags = { environment: Sapience.environment }
  end

  super(options, &block)
end

Instance Method Details

#log(log) ⇒ Object

Send an error notification to sentry



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/sapience/appender/sentry.rb', line 60

def log(log)
  return false unless should_log?(log)

  context = formatter.call(log, self)
  if log.exception
    context.delete(:exception)
    Raven.capture_exception(log.exception, context)
  else
    message = {
      error_class:   context.delete(:name),
      error_message: context.delete(:message),
      context: context,
    }
    message[:backtrace] = log.backtrace if log.backtrace
    Raven.capture_message(message[:error_message], message)
  end
  true
end

#validate_options!(options = {}) ⇒ Object



53
54
55
56
57
# File 'lib/sapience/appender/sentry.rb', line 53

def validate_options!(options = {})
  fail ArgumentError, "Options should be a Hash" unless options.is_a?(Hash)
  dsn_valid = options[:dsn].to_s =~ URI::DEFAULT_PARSER.regexp[:ABS_URI]
  fail ArgumentError, "The :dsn key (#{options[:dsn]}) is not a valid URI" unless dsn_valid
end