Class: Raven::Event

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

Constant Summary collapse

MAX_MESSAGE_SIZE_IN_BYTES =
1024 * 8
REQUIRED_OPTION_KEYS =
[:configuration, :context, :breadcrumbs].freeze
SDK =
{ "name" => "raven-ruby", "version" => Raven::VERSION }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) {|_self| ... } ⇒ Event

Returns a new instance of Event.

Yields:

  • (_self)

Yield Parameters:

  • _self (Raven::Event)

    the object that the method was called on



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
48
49
50
51
52
53
54
# File 'lib/raven/event.rb', line 23

def initialize(options)
  # 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

  unless REQUIRED_OPTION_KEYS.all? { |key| options.key?(key) }
    raise "you must provide configuration, context, and breadcrumbs when initializing a Raven::Event"
  end

  self.configuration = options[:configuration]
  self.context       = options[:context]
  self.breadcrumbs   = options[:breadcrumbs]

  # Allow attributes to be set on the event at initialization
  yield self if block_given?
  options.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.



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

def backtrace
  @backtrace
end

Returns the value of attribute breadcrumbs.



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

def breadcrumbs
  @breadcrumbs
end

#checksumObject

Returns the value of attribute checksum.



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

def checksum
  @checksum
end

#configurationObject

Returns the value of attribute configuration.



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

def configuration
  @configuration
end

#contextObject

Returns the value of attribute context.



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

def context
  @context
end

#environmentObject

Returns the value of attribute environment.



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

def environment
  @environment
end

#extraObject

Returns the value of attribute extra.



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

def extra
  @extra
end

#fingerprintObject

Returns the value of attribute fingerprint.



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

def fingerprint
  @fingerprint
end

#idObject Also known as: event_id

Returns the value of attribute id.



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

def id
  @id
end

#levelObject

Returns the value of attribute level.



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

def level
  @level
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#modulesObject

Returns the value of attribute modules.



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

def modules
  @modules
end

#platformObject

Returns the value of attribute platform.



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

def platform
  @platform
end

#releaseObject

Returns the value of attribute release.



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

def release
  @release
end

#runtimeObject

Returns the value of attribute runtime.



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

def runtime
  @runtime
end

#sdkObject

Returns the value of attribute sdk.



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

def sdk
  @sdk
end

#server_nameObject

Returns the value of attribute server_name.



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

def server_name
  @server_name
end

#server_osObject

Returns the value of attribute server_os.



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

def server_os
  @server_os
end

#tagsObject

Returns the value of attribute tags.



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

def tags
  @tags
end

#time_spentObject

Returns the value of attribute time_spent.



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

def time_spent
  @time_spent
end

#timestampObject

Returns the value of attribute timestamp.



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

def timestamp
  @timestamp
end

#transactionObject

Returns the value of attribute transaction.



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

def transaction
  @transaction
end

#userObject

Returns the value of attribute user.



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

def user
  @user
end

Class Method Details

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/raven/event.rb', line 56

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)

  return unless options[: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



74
75
76
77
78
79
80
81
82
83
# File 'lib/raven/event.rb', line 74

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



127
128
129
# File 'lib/raven/event.rb', line 127

def [](key)
  interface(key)
end

#[]=(key, value) ⇒ Object



131
132
133
# File 'lib/raven/event.rb', line 131

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

#add_exception_interface(exc) ⇒ Object



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

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:



119
120
121
122
123
124
125
# File 'lib/raven/event.rb', line 119

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



85
86
87
# File 'lib/raven/event.rb', line 85

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

#message=(args) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/raven/event.rb', line 89

def message=(args)
  if args.is_a?(Array)
    message, params = args[0], args[0..-1]
  else
    message = args
  end

  unless message.is_a?(String)
    configuration.logger.debug("You're passing a non-string message")
    message = message.to_s
  end

  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



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

def stacktrace_interface_from(backtrace)
  Backtrace.parse(backtrace, { configuration: configuration }).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



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/raven/event.rb', line 135

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



150
151
152
153
# File 'lib/raven/event.rb', line 150

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