class Raven::Event

Constants

MAX_MESSAGE_SIZE_IN_BYTES

See Sentry server default limits at github.com/getsentry/sentry/blob/master/src/sentry/conf/server.py

SDK

Attributes

backtrace[RW]
breadcrumbs[RW]
checksum[RW]
configuration[RW]
context[RW]
environment[RW]
event_id[RW]
extra[RW]
fingerprint[RW]
id[RW]
level[R]
logger[RW]
modules[RW]
platform[RW]
release[RW]
runtime[RW]
sdk[RW]
server_name[RW]
server_os[RW]
tags[RW]
time_spent[R]
timestamp[R]
transaction[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 47
def self.from_exception(exc, options = {}, &block)
  exception_context = if exc.instance_variable_defined?(:@__raven_context)
                        exc.instance_variable_get(:@__raven_context)
                      elsif exc.respond_to?(:raven_context)
                        exc.raven_context
                      else
                        {}
                      end
  options = Raven::Utils::DeepMergeHash.deep_merge(exception_context, options)

  configuration = options[:configuration] || Raven.configuration
  return unless configuration.exception_class_allowed?(exc)

  new(options) do |evt|
    evt.message = "#{exc.class}: #{exc.message}"

    evt.add_exception_interface(exc)

    yield evt if block
  end
end
from_message(message, options = {}) click to toggle source
# File lib/raven/event.rb, line 69
def self.from_message(message, options = {})
  new(options) do |evt|
    evt.message = message, options[:message_params] || []
    if options[:backtrace]
      evt.interface(:stacktrace) do |int|
        int.frames = evt.stacktrace_interface_from(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 20
def initialize(init = {})
  # Set some simple default values
  self.id            = SecureRandom.uuid.delete("-")
  self.timestamp     = Time.now.utc
  self.level         = :error
  self.logger        = :ruby
  self.platform      = :ruby
  self.sdk           = SDK

  # Set some attributes with empty hashes to allow merging
  @interfaces        = {}
  self.user          = {} # TODO: contexts
  self.extra         = {} # TODO: contexts
  self.server_os     = {} # TODO: contexts
  self.runtime       = {} # TODO: contexts
  self.tags          = {} # TODO: contexts

  copy_initial_state

  # Allow attributes to be set on the event at initialization
  yield self if block_given?
  init.each_pair { |key, val| public_send("#{key}=", val) unless val.nil? }

  set_core_attributes_from_configuration
  set_core_attributes_from_context
end

Public Instance Methods

[](key) click to toggle source
# File lib/raven/event.rb, line 111
def [](key)
  interface(key)
end
[]=(key, value) click to toggle source
# File lib/raven/event.rb, line 115
def []=(key, value)
  interface(key, value)
end
add_exception_interface(exc) click to toggle source
# File lib/raven/event.rb, line 139
def add_exception_interface(exc)
  interface(:exception) do |exc_int|
    exceptions = Raven::Utils::ExceptionCauseChain.exception_to_array(exc).reverse
    backtraces = Set.new
    exc_int.values = exceptions.map do |e|
      SingleExceptionInterface.new do |int|
        int.type = e.class.to_s
        int.value = e.to_s
        int.module = e.class.to_s.split('::')[0...-1].join('::')

        int.stacktrace =
          if e.backtrace && !backtraces.include?(e.backtrace.object_id)
            backtraces << e.backtrace.object_id
            StacktraceInterface.new do |stacktrace|
              stacktrace.frames = stacktrace_interface_from(e.backtrace)
            end
          end
      end
    end
  end
end
interface(name, value = nil, &block) click to toggle source
# File lib/raven/event.rb, line 104
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
level=(new_level) click to toggle source
# File lib/raven/event.rb, line 100
def level=(new_level) # needed to meet the Sentry spec
  @level = new_level == "warn" || new_level == :warn ? :warning : new_level
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.byteslice(0...MAX_MESSAGE_SIZE_IN_BYTES) # Messages limited to 10kb
    int.params = params
  end
end
stacktrace_interface_from(backtrace) click to toggle source
# File lib/raven/event.rb, line 161
def stacktrace_interface_from(backtrace)
  Backtrace.parse(backtrace).lines.reverse.each_with_object([]) do |line, memo|
    frame = StacktraceInterface::Frame.new
    frame.abs_path = line.file if line.file
    frame.function = line.method if line.method
    frame.lineno = line.number
    frame.in_app = line.in_app
    frame.module = line.module_name if line.module_name

    if configuration[:context_lines] && frame.abs_path
      frame.pre_context, frame.context_line, frame.post_context = \
        configuration.linecache.get_file_context(frame.abs_path, frame.lineno, configuration[:context_lines])
    end

    memo << frame if frame.filename
  end
end
time_spent=(time) click to toggle source
# File lib/raven/event.rb, line 96
def time_spent=(time)
  @time_spent = time.is_a?(Float) ? (time * 1000).to_i : time
end
timestamp=(time) click to toggle source
# File lib/raven/event.rb, line 92
def timestamp=(time)
  @timestamp = time.is_a?(Time) ? time.strftime('%Y-%m-%dT%H:%M:%S') : time
end
to_hash() click to toggle source
# File lib/raven/event.rb, line 119
def to_hash
  data = [:checksum, :environment, :event_id, :extra, :fingerprint, :level,
          :logger, :message, :modules, :platform, :release, :sdk, :server_name,
          :tags, :time_spent, :timestamp, :transaction, :user].each_with_object({}) do |att, memo|
    memo[att] = public_send(att) if public_send(att)
  end

  data[:breadcrumbs] = @breadcrumbs.to_hash unless @breadcrumbs.empty?

  @interfaces.each_pair do |name, int_data|
    data[name.to_sym] = int_data.to_hash
  end
  data
end
to_json_compatible() click to toggle source
# File lib/raven/event.rb, line 134
def to_json_compatible
  cleaned_hash = async_json_processors.reduce(to_hash) { |a, e| e.process(a) }
  JSON.parse(JSON.generate(cleaned_hash))
end