Class: Raven::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/raven/event.rb

Constant Summary collapse

MAX_MESSAGE_SIZE_IN_BYTES =
1024 * 8
SDK =
{ "name" => "raven-ruby", "version" => Raven::VERSION }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(init = {}) {|_self| ... } ⇒ Event

Returns a new instance of Event

Yields:

  • (_self)

Yield Parameters:

  • _self (Raven::Event)

    the object that the method was called on


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/raven/event.rb', line 21

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

Instance Attribute Details

#backtraceObject

Returns the value of attribute backtrace


13
14
15
# File 'lib/raven/event.rb', line 13

def backtrace
  @backtrace
end

Returns the value of attribute breadcrumbs


13
14
15
# File 'lib/raven/event.rb', line 13

def breadcrumbs
  @breadcrumbs
end

#checksumObject

Returns the value of attribute checksum


13
14
15
# File 'lib/raven/event.rb', line 13

def checksum
  @checksum
end

#configurationObject

Returns the value of attribute configuration


13
14
15
# File 'lib/raven/event.rb', line 13

def configuration
  @configuration
end

#contextObject

Returns the value of attribute context


13
14
15
# File 'lib/raven/event.rb', line 13

def context
  @context
end

#environmentObject

Returns the value of attribute environment


13
14
15
# File 'lib/raven/event.rb', line 13

def environment
  @environment
end

#extraObject

Returns the value of attribute extra


13
14
15
# File 'lib/raven/event.rb', line 13

def extra
  @extra
end

#fingerprintObject

Returns the value of attribute fingerprint


13
14
15
# File 'lib/raven/event.rb', line 13

def fingerprint
  @fingerprint
end

#idObject Also known as: event_id

Returns the value of attribute id


13
14
15
# File 'lib/raven/event.rb', line 13

def id
  @id
end

#levelObject

Returns the value of attribute level


19
20
21
# File 'lib/raven/event.rb', line 19

def level
  @level
end

#loggerObject

Returns the value of attribute logger


13
14
15
# File 'lib/raven/event.rb', line 13

def logger
  @logger
end

#modulesObject

Returns the value of attribute modules


13
14
15
# File 'lib/raven/event.rb', line 13

def modules
  @modules
end

#platformObject

Returns the value of attribute platform


13
14
15
# File 'lib/raven/event.rb', line 13

def platform
  @platform
end

#releaseObject

Returns the value of attribute release


13
14
15
# File 'lib/raven/event.rb', line 13

def release
  @release
end

#runtimeObject

Returns the value of attribute runtime


13
14
15
# File 'lib/raven/event.rb', line 13

def runtime
  @runtime
end

#sdkObject

Returns the value of attribute sdk


13
14
15
# File 'lib/raven/event.rb', line 13

def sdk
  @sdk
end

#server_nameObject

Returns the value of attribute server_name


13
14
15
# File 'lib/raven/event.rb', line 13

def server_name
  @server_name
end

#server_osObject

Returns the value of attribute server_os


13
14
15
# File 'lib/raven/event.rb', line 13

def server_os
  @server_os
end

#tagsObject

Returns the value of attribute tags


13
14
15
# File 'lib/raven/event.rb', line 13

def tags
  @tags
end

#time_spentObject

Returns the value of attribute time_spent


19
20
21
# File 'lib/raven/event.rb', line 19

def time_spent
  @time_spent
end

#timestampObject

Returns the value of attribute timestamp


19
20
21
# File 'lib/raven/event.rb', line 19

def timestamp
  @timestamp
end

#transactionObject

Returns the value of attribute transaction


13
14
15
# File 'lib/raven/event.rb', line 13

def transaction
  @transaction
end

#userObject

Returns the value of attribute user


13
14
15
# File 'lib/raven/event.rb', line 13

def user
  @user
end

Class Method Details

.from_exception(exc, options = {}, &block) ⇒ Object Also known as: captureException, capture_exception


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/raven/event.rb', line 48

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 = {}) ⇒ Object Also known as: captureMessage, capture_message


70
71
72
73
74
75
76
77
78
79
# File 'lib/raven/event.rb', line 70

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

Instance Method Details

#[](key) ⇒ Object


112
113
114
# File 'lib/raven/event.rb', line 112

def [](key)
  interface(key)
end

#[]=(key, value) ⇒ Object


116
117
118
# File 'lib/raven/event.rb', line 116

def []=(key, value)
  interface(key, value)
end

#add_exception_interface(exc) ⇒ Object


140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/raven/event.rb', line 140

def add_exception_interface(exc)
  interface(:exception) do |exc_int|
    exceptions = exception_chain_to_array(exc)
    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) ⇒ Object

Raises:


105
106
107
108
109
110
# File 'lib/raven/event.rb', line 105

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

#messageObject


81
82
83
# File 'lib/raven/event.rb', line 81

def message
  @interfaces[:logentry] && @interfaces[:logentry].unformatted_message
end

#message=(args) ⇒ Object


85
86
87
88
89
90
91
# File 'lib/raven/event.rb', line 85

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) ⇒ Object


162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/raven/event.rb', line 162

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

#to_hashObject


120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/raven/event.rb', line 120

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_compatibleObject


135
136
137
138
# File 'lib/raven/event.rb', line 135

def to_json_compatible
  cleaned_hash = async_json_processors.reduce(to_hash) { |a, e| e.process(a) }
  JSON.parse(JSON.generate(cleaned_hash))
end