class Raven::Event

Constants

BACKTRACE_RE
LOG_LEVELS
PLATFORM
SDK

Attributes

backtrace[RW]
breadcrumbs[RW]
checksum[RW]
configuration[RW]
context[RW]
culprit[RW]
environment[RW]
extra[RW]
fingerprint[RW]
id[RW]
level[RW]
logger[RW]
modules[RW]
os[RW]
release[RW]
runtime[RW]
server_name[RW]
tags[RW]
time_spent[RW]
timestamp[RW]
user[RW]

Public Class Methods

captureException(exc, options = {}, &block)
Alias for: from_exception
captureMessage(message, options = {})
Alias for: from_message
capture_exception(exc, options = {}, &block)
Alias for: from_exception
capture_message(message, options = {})
Alias for: from_message
from_exception(exc, options = {}) { |evt| ... } click to toggle source
# File lib/raven/event.rb, line 93
def from_exception(exc, options = {}, &block)
  exception_context = get_exception_context(exc) || {}
  options = Raven::Utils::DeepMergeHash.deep_merge(exception_context, options)

  configuration = options[:configuration] || Raven.configuration
  if exc.is_a?(Raven::Error)
    # Try to prevent error reporting loops
    Raven.logger.info "Refusing to capture Raven error: #{exc.inspect}"
    return nil
  end
  if configuration[:excluded_exceptions].any? { |x| (x === exc rescue false) || x == exc.class.name }
    Raven.logger.info "User excluded error: #{exc.inspect}"
    return nil
  end

  new(options) do |evt|
    evt.configuration = configuration
    evt.message = "#{exc.class}: #{exc.message}"
    evt.level = options[:level] || :error

    add_exception_interface(evt, exc)

    yield evt if block
  end
end
from_message(message, options = {}) click to toggle source
# File lib/raven/event.rb, line 119
def from_message(message, options = {})
  message = message.byteslice(0...10_000) # Messages limited to 10kb
  configuration = options[:configuration] || Raven.configuration

  new(options) do |evt|
    evt.configuration = configuration
    evt.level = options[:level] || :error
    evt.message = message, options[:message_params] || []
    if options[:backtrace]
      evt.interface(:stacktrace) do |int|
        stacktrace_interface_from(int, evt, options[:backtrace])
      end
    end
  end
end
Also aliased as: captureMessage, capture_message
new(init = {}) { |self| ... } click to toggle source
# File lib/raven/event.rb, line 31
def initialize(init = {})
  @configuration = Raven.configuration
  @interfaces    = {}
  @breadcrumbs   = Raven.breadcrumbs
  @context       = Raven.context
  @id            = SecureRandom.uuid.delete("-")
  @timestamp     = Time.now.utc
  @time_spent    = nil
  @level         = :error
  @logger        = ''
  @culprit       = nil
  @server_name   = @configuration.server_name
  @release       = @configuration.release
  @modules       = list_gem_specs if @configuration.send_modules
  @user          = {} # TODO: contexts
  @extra         = {} # TODO: contexts
  @os            = {} # TODO: contexts
  @runtime       = {} # TODO: contexts
  @tags          = {} # TODO: contexts
  @checksum      = nil
  @fingerprint   = nil
  @environment   = @configuration.current_environment

  yield self if block_given?

  if !self[:http] && @context.rack_env
    interface :http do |int|
      int.from_rack(@context.rack_env)
    end
  end

  if @context.rack_env # TODO: contexts
    @context.user[:ip_address] = calculate_real_ip_from_rack
  end

  init.each_pair { |key, val| public_send(key.to_s + "=", val) }

  @user = @context.user.merge(@user) # TODO: contexts
  @extra = @context.extra.merge(@extra) # TODO: contexts
  @tags = @configuration.tags.merge(@context.tags).merge(@tags) # TODO: contexts
  @os = @context.os # TODO: contexts
  @runtime = @context.runtime # TODO: contexts

  # Some type coercion
  @timestamp  = @timestamp.strftime('%Y-%m-%dT%H:%M:%S') if @timestamp.is_a?(Time)
  @time_spent = (@time_spent * 1000).to_i if @time_spent.is_a?(Float)
  @level      = LOG_LEVELS[@level.to_s.downcase] if @level.is_a?(String) || @level.is_a?(Symbol)
end

Public Instance Methods

[](key) click to toggle source
# File lib/raven/event.rb, line 217
def [](key)
  interface(key)
end
[]=(key, value) click to toggle source
# File lib/raven/event.rb, line 221
def []=(key, value)
  interface(key, value)
end
get_culprit(frames) click to toggle source
# File lib/raven/event.rb, line 267
def get_culprit(frames)
  lastframe = frames.reverse.find(&:in_app) || frames.last
  "#{lastframe.filename} in #{lastframe.function} at line #{lastframe.lineno}" if lastframe
end
get_file_context(filename, lineno, context) click to toggle source
# File lib/raven/event.rb, line 259
def get_file_context(filename, lineno, context)
  return nil, nil, nil unless Raven::LineCache.valid_file?(filename)
  lines = Array.new(2 * context + 1) do |i|
    Raven::LineCache.getline(filename, lineno - context + i)
  end
  [lines[0..(context - 1)], lines[context], lines[(context + 1)..-1]]
end
interface(name, value = nil, &block) click to toggle source
# File lib/raven/event.rb, line 210
def interface(name, value = nil, &block)
  int = Interface.registered[name]
  raise(Error, "Unknown interface: #{name}") unless int
  @interfaces[int.sentry_alias] = int.new(value, &block) if value || block
  @interfaces[int.sentry_alias]
end
list_gem_specs() click to toggle source
# File lib/raven/event.rb, line 205
def list_gem_specs
  # Older versions of Rubygems don't support iterating over all specs
  Hash[Gem::Specification.map { |spec| [spec.name, spec.version.to_s] }] if Gem::Specification.respond_to?(:map)
end
message() click to toggle source
# File lib/raven/event.rb, line 80
def message
  @interfaces[:logentry] && @interfaces[:logentry].unformatted_message
end
message=(args) click to toggle source
# File lib/raven/event.rb, line 84
def message=(args)
  message, params = *args
  interface(:message) do |int|
    int.message = message
    int.params = params
  end
end
to_hash() click to toggle source
# File lib/raven/event.rb, line 225
def to_hash
  data = {
    :event_id => @id,
    :timestamp => @timestamp,
    :time_spent => @time_spent,
    :level => @level,
    :platform => PLATFORM,
    :sdk => SDK,
    :contexts => {
      :os => @os,
      :runtime => @runtime
    }
  }

  data[:logger] = @logger if @logger
  data[:culprit] = @culprit if @culprit
  data[:server_name] = @server_name if @server_name
  data[:release] = @release if @release
  data[:environment] = @environment if @environment
  data[:fingerprint] = @fingerprint if @fingerprint
  data[:modules] = @modules if @modules
  data[:extra] = @extra if @extra
  data[:tags] = @tags if @tags
  data[:user] = @user if @user
  data[:breadcrumbs] = @breadcrumbs.to_hash unless @breadcrumbs.empty?
  data[:checksum] = @checksum if @checksum

  @interfaces.each_pair do |name, int_data|
    data[name.to_sym] = int_data.to_hash
  end
  data[:message] = message
  data
end
to_json_compatible() click to toggle source
# File lib/raven/event.rb, line 272
def to_json_compatible
  JSON.parse(JSON.generate(to_hash))
end