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,
}
BACKTRACE_RE =
/^(.+?):(\d+)(?::in `(.+?)')?$/
PLATFORM =
"ruby"

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

def initialize(init = {})
  @configuration = Raven.configuration
  @interfaces    = {}
  @context       = Raven.context
  @id            = generate_event_id
  @message       = nil
  @timestamp     = Time.now.utc
  @time_spent    = nil
  @level         = :error
  @logger        = ''
  @culprit       = nil
  @server_name   = @configuration.server_name || get_hostname
  @release       = @configuration.release
  @modules       = get_modules if @configuration.send_modules
  @user          = {}
  @extra         = {}
  @tags          = {}
  @checksum      = nil
  @fingerprint   = nil

  yield self if block_given?

  if !self[:http] && @context.rack_env
    interface :http do |int|
      int.from_rack(@context.rack_env)
    end
  end

  init.each_pair  { |key, val| instance_variable_set('@' + key.to_s, val) }

  @user = @context.user.merge(@user)
  @extra = @context.extra.merge(@extra)
  @tags = @configuration.tags.merge(@context.tags).merge(@tags)

  # 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

#checksumObject

Returns the value of attribute checksum.



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

def checksum
  @checksum
end

#configurationObject

Returns the value of attribute configuration.



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

def configuration
  @configuration
end

#contextObject

Returns the value of attribute context.



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

def context
  @context
end

#culpritObject

Returns the value of attribute culprit.



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

def culprit
  @culprit
end

#extraObject

Returns the value of attribute extra.



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

def extra
  @extra
end

#fingerprintObject

Returns the value of attribute fingerprint.



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

def fingerprint
  @fingerprint
end

#idObject (readonly)

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.



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

def level
  @level
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#messageObject

Returns the value of attribute message.



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

def message
  @message
end

#modulesObject

Returns the value of attribute modules.



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

def modules
  @modules
end

#projectObject

Returns the value of attribute project.



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

def project
  @project
end

#releaseObject

Returns the value of attribute release.



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

def release
  @release
end

#server_nameObject

Returns the value of attribute server_name.



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

def server_name
  @server_name
end

#tagsObject

Returns the value of attribute tags.



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

def tags
  @tags
end

#time_spentObject

Returns the value of attribute time_spent.



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

def time_spent
  @time_spent
end

#timestampObject

Returns the value of attribute timestamp.



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

def timestamp
  @timestamp
end

Class Method Details

._source_lines(_path, _from, _to) ⇒ Object

Because linecache can go to hell



218
219
# File 'lib/raven/event.rb', line 218

def self._source_lines(_path, _from, _to)
end

.add_exception_interface(evt, exc) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/raven/event.rb', line 166

def self.add_exception_interface(evt, exc)
  evt.interface(:exception) do |exc_int|
    exceptions = [exc]
    context = Set.new [exc.object_id]
    while exc.respond_to?(:cause) && exc.cause
      exceptions << exc.cause
      exc = exc.cause
      # TODO(dcramer): ideally this would log to inform the user
      if context.include?(exc.object_id)
        break
      end
      context.add(exc.object_id)
    end
    exceptions.reverse!

    exc_int.values = exceptions.map do |exc|
      SingleExceptionInterface.new do |int|
        int.type = exc.class.to_s
        int.value = exc.to_s
        int.module = exc.class.to_s.split('::')[0...-1].join('::')

        int.stacktrace = if exc.backtrace
          StacktraceInterface.new do |stacktrace|
            stacktrace_interface_from(stacktrace, evt, exc.backtrace)
          end
        end
      end
    end
  end
end

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



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/raven/event.rb', line 123

def self.from_exception(exc, options = {}, &block)
  notes = exc.instance_variable_get(:@__raven_context) || {}
  options = notes.merge(options)

  configuration = options[:configuration] || Raven.configuration
  if exc.is_a?(Raven::Error)
    # Try to prevent error reporting loops
    Raven.logger.info "Refusing to capture Raven error: #{exc.inspect}"
    return nil
  end
  if configuration[:excluded_exceptions].any? { |x| (x === exc rescue false) || x == exc.class.name }
    Raven.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)

    block.call(evt) if block
  end
end

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



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/raven/event.rb', line 149

def self.from_message(message, options = {})
  configuration = options[:configuration] || Raven.configuration
  new(options) do |evt|
    evt.configuration = configuration
    evt.message = message
    evt.level = options[:level] || :error
    evt.interface :message do |int|
      int.message = message
    end
    if options[:backtrace]
      evt.interface(:stacktrace) do |int|
        stacktrace_interface_from(int, evt, options[:backtrace])
      end
    end
  end
end

.stacktrace_interface_from(int, evt, backtrace) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/raven/event.rb', line 197

def self.stacktrace_interface_from(int, evt, backtrace)
  backtrace = Backtrace.parse(backtrace)
  int.frames = backtrace.lines.reverse.map do |line|
    StacktraceInterface::Frame.new.tap do |frame|
      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 evt.configuration[:context_lines] && frame.abs_path
        frame.pre_context, frame.context_line, frame.post_context = \
          evt.get_file_context(frame.abs_path, frame.lineno, evt.configuration[:context_lines])
      end
    end
  end.select { |f| f.filename }

  evt.culprit = evt.get_culprit(int.frames)
end

Instance Method Details

#[](key) ⇒ Object



89
90
91
# File 'lib/raven/event.rb', line 89

def [](key)
  interface(key)
end

#[]=(key, value) ⇒ Object



93
94
95
# File 'lib/raven/event.rb', line 93

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

#get_culprit(frames) ⇒ Object



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

def get_culprit(frames)
  lastframe = frames.reverse.find { |f| f.in_app } || frames.last
  "#{lastframe.filename} in #{lastframe.function} at line #{lastframe.lineno}" if lastframe
end

#get_file_context(filename, lineno, context) ⇒ Object



221
222
223
224
225
226
227
# File 'lib/raven/event.rb', line 221

def get_file_context(filename, lineno, context)
  return nil, nil, nil unless Raven::LineCache.is_valid_file(filename)
  lines = (2 * context + 1).times.map do |i|
    Raven::LineCache.getline(filename, lineno - context + i)
  end
  [lines[0..(context - 1)], lines[context], lines[(context + 1)..-1]]
end

#get_hostnameObject



71
72
73
74
75
# File 'lib/raven/event.rb', line 71

def get_hostname
  # Try to resolve the hostname to an FQDN, but fall back to whatever the load name is
  hostname = Socket.gethostname
  Socket.gethostbyname(hostname).first rescue hostname
end

#get_modulesObject



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

def get_modules
  # 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

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

Raises:



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

def interface(name, value = nil, &block)
  int = Raven.find_interface(name)
  raise Error.new("Unknown interface: #{name}") unless int
  @interfaces[int.name] = int.new(value, &block) if value || block
  @interfaces[int.name]
end

#to_hashObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/raven/event.rb', line 97

def to_hash
  data = {
    :event_id => @id,
    :message => @message,
    :timestamp => @timestamp,
    :time_spent => @time_spent,
    :level => @level,
    :project => @project,
    :platform => PLATFORM,
  }
  data[:logger] = @logger if @logger
  data[:culprit] = @culprit if @culprit
  data[:server_name] = @server_name if @server_name
  data[:release] = @release if @release
  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[:checksum] = @checksum if @checksum
  @interfaces.each_pair do |name, int_data|
    data[name.to_sym] = int_data.to_hash
  end
  data
end