Class: Raven::Event

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

Constant Summary collapse

LOG_LEVELS =
{
  "debug" => 10,
  "info" => 20,
  "warn" => 30,
  "warning" => 30,
  "error" => 40,
  "fatal" => 50
}.freeze
BACKTRACE_RE =
/^(.+?):(\d+)(?::in `(.+?)')?$/
PLATFORM =
"ruby".freeze
SDK =
{ "name" => "sentry-raven", "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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/raven/event.rb', line 31

def initialize(init = {})
  @configuration = init[:configuration] || Raven.configuration
  @interfaces    = {}
  @breadcrumbs   = init[:breadcrumbs] || Raven.breadcrumbs
  @context       = init[:context] || Raven.context
  @linecache     = @configuration.linecache
  @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
  @server_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

  # 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

Instance Attribute Details

#backtraceObject

Returns the value of attribute backtrace.



26
27
28
# File 'lib/raven/event.rb', line 26

def backtrace
  @backtrace
end

Returns the value of attribute breadcrumbs.



26
27
28
# File 'lib/raven/event.rb', line 26

def breadcrumbs
  @breadcrumbs
end

#checksumObject

Returns the value of attribute checksum.



26
27
28
# File 'lib/raven/event.rb', line 26

def checksum
  @checksum
end

#configurationObject

Returns the value of attribute configuration.



26
27
28
# File 'lib/raven/event.rb', line 26

def configuration
  @configuration
end

#contextObject

Returns the value of attribute context.



26
27
28
# File 'lib/raven/event.rb', line 26

def context
  @context
end

#culpritObject

Returns the value of attribute culprit.



26
27
28
# File 'lib/raven/event.rb', line 26

def culprit
  @culprit
end

#environmentObject

Returns the value of attribute environment.



26
27
28
# File 'lib/raven/event.rb', line 26

def environment
  @environment
end

#extraObject

Returns the value of attribute extra.



26
27
28
# File 'lib/raven/event.rb', line 26

def extra
  @extra
end

#fingerprintObject

Returns the value of attribute fingerprint.



26
27
28
# File 'lib/raven/event.rb', line 26

def fingerprint
  @fingerprint
end

#idObject

Returns the value of attribute id.



26
27
28
# File 'lib/raven/event.rb', line 26

def id
  @id
end

#levelObject

Returns the value of attribute level.



26
27
28
# File 'lib/raven/event.rb', line 26

def level
  @level
end

#linecacheObject

Returns the value of attribute linecache.



26
27
28
# File 'lib/raven/event.rb', line 26

def linecache
  @linecache
end

#loggerObject

Returns the value of attribute logger.



26
27
28
# File 'lib/raven/event.rb', line 26

def logger
  @logger
end

#modulesObject

Returns the value of attribute modules.



26
27
28
# File 'lib/raven/event.rb', line 26

def modules
  @modules
end

#releaseObject

Returns the value of attribute release.



26
27
28
# File 'lib/raven/event.rb', line 26

def release
  @release
end

#runtimeObject

Returns the value of attribute runtime.



26
27
28
# File 'lib/raven/event.rb', line 26

def runtime
  @runtime
end

#server_nameObject

Returns the value of attribute server_name.



26
27
28
# File 'lib/raven/event.rb', line 26

def server_name
  @server_name
end

#server_osObject

Returns the value of attribute server_os.



26
27
28
# File 'lib/raven/event.rb', line 26

def server_os
  @server_os
end

#tagsObject

Returns the value of attribute tags.



26
27
28
# File 'lib/raven/event.rb', line 26

def tags
  @tags
end

#time_spentObject

Returns the value of attribute time_spent.



26
27
28
# File 'lib/raven/event.rb', line 26

def time_spent
  @time_spent
end

#timestampObject

Returns the value of attribute timestamp.



26
27
28
# File 'lib/raven/event.rb', line 26

def timestamp
  @timestamp
end

#userObject

Returns the value of attribute user.



26
27
28
# File 'lib/raven/event.rb', line 26

def user
  @user
end

Class Method Details

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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/raven/event.rb', line 92

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
    configuration.logger.info "Refusing to capture Raven error: #{exc.inspect}"
    return nil
  end
  if configuration[:excluded_exceptions].any? { |x| get_exception_class(x) === exc }
    configuration.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 = {}) ⇒ Object Also known as: captureMessage, capture_message



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

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

Instance Method Details

#[](key) ⇒ Object



231
232
233
# File 'lib/raven/event.rb', line 231

def [](key)
  interface(key)
end

#[]=(key, value) ⇒ Object



235
236
237
# File 'lib/raven/event.rb', line 235

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

#get_culprit(frames) ⇒ Object



273
274
275
276
# File 'lib/raven/event.rb', line 273

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



269
270
271
# File 'lib/raven/event.rb', line 269

def get_file_context(filename, lineno, context)
  linecache.get_file_context(filename, lineno, context)
end

#interface(name, value = nil, &block) ⇒ Object

Raises:



224
225
226
227
228
229
# File 'lib/raven/event.rb', line 224

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_specsObject



219
220
221
222
# File 'lib/raven/event.rb', line 219

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

#messageObject



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

def message
  @interfaces[:logentry] && @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
    int.params = params
  end
end

#to_hashObject



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/raven/event.rb', line 239

def to_hash
  data = {
    :event_id => @id,
    :timestamp => @timestamp,
    :time_spent => @time_spent,
    :level => @level,
    :platform => PLATFORM,
    :sdk => SDK
  }

  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_compatibleObject



278
279
280
# File 'lib/raven/event.rb', line 278

def to_json_compatible
  JSON.parse(JSON.generate(to_hash))
end