Class: Raven::Event

Inherits:
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



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
47
# File 'lib/raven/event.rb', line 22

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.



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

def backtrace
  @backtrace
end

Returns the value of attribute breadcrumbs.



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

def breadcrumbs
  @breadcrumbs
end

#checksumObject

Returns the value of attribute checksum.



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

def checksum
  @checksum
end

#configurationObject

Returns the value of attribute configuration.



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

def configuration
  @configuration
end

#contextObject

Returns the value of attribute context.



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

def context
  @context
end

#environmentObject

Returns the value of attribute environment.



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

def environment
  @environment
end

#extraObject

Returns the value of attribute extra.



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

def extra
  @extra
end

#fingerprintObject

Returns the value of attribute fingerprint.



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

def fingerprint
  @fingerprint
end

#idObject Also known as: event_id

Returns the value of attribute id.



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

def id
  @id
end

#levelObject

Returns the value of attribute level.



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

def level
  @level
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#modulesObject

Returns the value of attribute modules.



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

def modules
  @modules
end

#platformObject

Returns the value of attribute platform.



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

def platform
  @platform
end

#releaseObject

Returns the value of attribute release.



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

def release
  @release
end

#runtimeObject

Returns the value of attribute runtime.



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

def runtime
  @runtime
end

#sdkObject

Returns the value of attribute sdk.



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

def sdk
  @sdk
end

#server_nameObject

Returns the value of attribute server_name.



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

def server_name
  @server_name
end

#server_osObject

Returns the value of attribute server_os.



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

def server_os
  @server_os
end

#tagsObject

Returns the value of attribute tags.



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

def tags
  @tags
end

#time_spentObject

Returns the value of attribute time_spent.



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

def time_spent
  @time_spent
end

#timestampObject

Returns the value of attribute timestamp.



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

def timestamp
  @timestamp
end

#transactionObject

Returns the value of attribute transaction.



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

def transaction
  @transaction
end

#userObject

Returns the value of attribute user.



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

def user
  @user
end

Class Method Details

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



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

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.add_exception_interface(exc)
    yield evt if block
  end
end

.from_message(message, options = {}) ⇒ Object Also known as: captureMessage, capture_message



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

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



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

def [](key)
  interface(key)
end

#[]=(key, value) ⇒ Object



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

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

#add_exception_interface(exc) ⇒ Object



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

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

Raises:



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

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

#log_messageObject



154
155
156
# File 'lib/raven/event.rb', line 154

def log_message
  message || message_from_exception
end

#messageObject



79
80
81
# File 'lib/raven/event.rb', line 79

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

#message=(args) ⇒ Object



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

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

#message_from_exceptionObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/raven/event.rb', line 139

def message_from_exception
  exception = @interfaces[:exception]

  return unless exception

  exception = exception.to_hash

  type = exception.dig(:values, 0, :type)
  value = exception.dig(:values, 0, :value)

  if type && value
    "#{type}: #{value}"
  end
end

#stacktrace_interface_from(backtrace) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/raven/event.rb', line 180

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



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# 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_compatibleObject



134
135
136
137
# 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