Class: Chronos::Core::NoticeBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/chronos/core/notice_builder.rb

Overview

Normalizes an Exception and caller context into a Notice.

Examples:

notice = builder.call(RuntimeError.new("failed"), :tags => ["billing"])

Instance Method Summary collapse

Constructor Details

#initialize(config, clock = nil) ⇒ NoticeBuilder

Returns a new instance of NoticeBuilder.



18
19
20
21
22
23
24
# File 'lib/chronos/core/notice_builder.rb', line 18

def initialize(config, clock = nil)
  @config = config
  @clock = clock || proc { Time.now }
  @backtrace_parser = BacktraceParser.new(config.root_directory)
  @cause_collector = ExceptionCauseCollector.new
  @runtime_info = RuntimeInfo.new
end

Instance Method Details

#call(exception, context = {}) ⇒ Object

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/chronos/core/notice_builder.rb', line 26

def call(exception, context = {})
  raise ArgumentError, "exception must be an Exception" unless exception.is_a?(Exception)

  context = {} unless context.is_a?(Hash)
  runtime = @runtime_info.call
  Notice.new(
    :event_id => SecureRandom.uuid,
    :exception_class => safe_class_name(exception),
    :message => safe_message(exception),
    :backtrace => @backtrace_parser.call(exception.backtrace),
    :causes => @cause_collector.call(exception),
    :severity => value(context, :severity) || "error",
    :timestamp => @clock.call.utc.iso8601(6),
    :context => value(context, :context) || context_without_reserved_keys(context),
    :parameters => value(context, :parameters) || {},
    :session => value(context, :session) || {},
    :user => value(context, :user) || {},
    :environment => @config.environment,
    :runtime => runtime[:runtime],
    :versions => {"agent" => Chronos::VERSION, "application" => @config.app_version},
    :host => runtime[:host],
    :process => runtime[:process],
    :thread => runtime[:thread],
    :tags => Array(value(context, :tags)),
    :fingerprint => value(context, :fingerprint)
  )
end