Module: Raven

Defined in:
lib/raven/okjson.rb,
lib/raven/cli.rb,
lib/raven/base.rb,
lib/raven/rack.rb,
lib/raven/error.rb,
lib/raven/event.rb,
lib/raven/client.rb,
lib/raven/logger.rb,
lib/raven/context.rb,
lib/raven/railtie.rb,
lib/raven/sidekiq.rb,
lib/raven/version.rb,
lib/raven/backtrace.rb,
lib/raven/linecache.rb,
lib/raven/processor.rb,
lib/raven/interfaces.rb,
lib/raven/transports.rb,
lib/raven/configuration.rb,
lib/raven/transports/udp.rb,
lib/raven/interfaces/http.rb,
lib/raven/transports/http.rb,
lib/raven/interfaces/message.rb,
lib/raven/interfaces/exception.rb,
lib/raven/interfaces/stack_trace.rb,
lib/raven/processors/sanitizedata.rb,
lib/raven/rails/controller_methods.rb,
lib/raven/rails/middleware/debug_exceptions_catcher.rb

Overview

A much simpler source line cacher because linecache sucks at platform compat

Defined Under Namespace

Modules: OkJson, Processor, Rails, Transports Classes: Backtrace, CLI, Client, Configuration, Context, Error, Event, ExceptionInterface, HttpInterface, Interface, LineCache, Logger, MessageInterface, Rack, Railtie, Sidekiq, StacktraceInterface

Constant Summary collapse

VERSION =
"0.9.3"
INTERFACES =
{}

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.clientObject

The client object is responsible for delivering formatted data to the Sentry server.



40
41
42
# File 'lib/raven/base.rb', line 40

def client
  @client ||= Client.new(configuration)
end

.configurationObject

The configuration object.

See Also:



35
36
37
# File 'lib/raven/base.rb', line 35

def configuration
  @configuration ||= Configuration.new
end

Class Method Details

.annotate_exception(exc, options = {}) ⇒ Object Also known as: annotateException, annotate

Provides extra context to the exception prior to it being handled by Raven. An exception can have multiple annotations, which are merged together.

The options (annotation) is treated the same as the “options“ parameter to “capture_exception“ or “Event.from_exception“, and can contain the same “:user“, “:tags“, etc. options as these methods.

These will be merged with the “options“ parameter to “Event.from_exception“ at the top of execution.

Examples:

begin
  raise "Hello"
rescue => exc
  Raven.annotate_exception(exc, :user => { 'id' => 1,
                           'email' => '[email protected]' })
end


153
154
155
156
157
158
# File 'lib/raven/base.rb', line 153

def annotate_exception(exc, options = {})
  notes = exc.instance_variable_get(:@__raven_context) || {}
  notes.merge!(options)
  exc.instance_variable_set(:@__raven_context, notes)
  exc
end

.capture(options = {}, &block) ⇒ Object

Capture and process any exceptions from the given block, or globally if no block is given

Examples:

Raven.capture do
  MyApp.run
end


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/raven/base.rb', line 79

def capture(options = {}, &block)
  if block
    begin
      block.call
    rescue Error
      raise # Don't capture Raven errors
    rescue Exception => e
      capture_exception(e, options)
      raise
    end
  else
    # Install at_exit hook
    at_exit do
      if $ERROR_INFO
        logger.debug "Caught a post-mortem exception: #{$ERROR_INFO.inspect}"
        capture_exception($ERROR_INFO, options)
      end
    end
  end
end

.capture_exception(exception, options = {}) ⇒ Object Also known as: captureException



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/raven/base.rb', line 100

def capture_exception(exception, options = {})
  send_or_skip do
    if evt = Event.from_exception(exception, options)
      yield evt if block_given?
      if configuration.async?
        configuration.async.call(evt)
      else
        send(evt)
      end
    end
  end
end

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



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/raven/base.rb', line 113

def capture_message(message, options = {})
  send_or_skip do
    if evt = Event.from_message(message, options)
      yield evt if block_given?
      if configuration.async?
        configuration.async.call(evt)
      else
        send(evt)
      end
    end
  end
end

.configure(silent = false) {|configuration| ... } ⇒ Object

Call this method to modify defaults in your initializers.

Examples:

Raven.configure do |config|
  config.server = 'http://...'
end

Yields:



55
56
57
58
59
60
61
# File 'lib/raven/base.rb', line 55

def configure(silent = false)
  yield(configuration) if block_given?

  self.client = Client.new(configuration)
  report_ready unless silent
  self.client
end

.contextObject



25
26
27
# File 'lib/raven/base.rb', line 25

def context
  Context.current
end

.extra_context(options = {}) ⇒ Object

Bind extra context. Merges with existing context (if any).

Extra context shows up as Additional Data within Sentry, and is completely arbitrary.

Examples:

Raven.tags_context('my_custom_data' => 'value')


188
189
190
# File 'lib/raven/base.rb', line 188

def extra_context(options = {})
  self.context.extra.merge!(options)
end

.find_interface(name) ⇒ Object



37
38
39
# File 'lib/raven/interfaces.rb', line 37

def self.find_interface(name)
  INTERFACES[name.to_s]
end

.injectObject

Injects various integrations



200
201
202
203
204
205
206
# File 'lib/raven/base.rb', line 200

def inject
  # TODO(dcramer): integrations should have a way to opt-out
  require 'raven/integrations/delayed_job' if defined?(::Delayed::Plugin)
  require 'raven/railtie' if defined?(::Rails::Railtie)
  require 'raven/sidekiq' if defined?(Sidekiq)
  require 'raven/tasks' if defined?(Rake)
end

.loggerObject



29
30
31
# File 'lib/raven/base.rb', line 29

def logger
  @logger ||= Logger.new
end

.rack_context(env) ⇒ Object



192
193
194
195
196
197
# File 'lib/raven/base.rb', line 192

def rack_context(env)
  if env.empty?
    env = nil
  end
  self.context.rack_env = env
end

.register_interface(mapping) ⇒ Object



30
31
32
33
34
35
# File 'lib/raven/interfaces.rb', line 30

def self.register_interface(mapping)
  mapping.each_pair do |key, klass|
    INTERFACES[key.to_s] = klass
    INTERFACES[klass.name] = klass
  end
end

.report_readyObject

Tell the log that the client is good to go



45
46
47
# File 'lib/raven/base.rb', line 45

def report_ready
  self.logger.info "Raven #{VERSION} ready to catch errors"
end

.send(evt) ⇒ Object

Send an event to the configured Sentry server

Examples:

evt = Raven::Event.new(:message => "An error")
Raven.send(evt)


68
69
70
# File 'lib/raven/base.rb', line 68

def send(evt)
  client.send(evt)
end

.send_or_skipObject



126
127
128
129
130
131
132
# File 'lib/raven/base.rb', line 126

def send_or_skip
  if configuration.send_in_current_environment?
    yield if block_given?
  else
    configuration.log_excluded_environment_message
  end
end

.tags_context(options = {}) ⇒ Object

Bind tags context. Merges with existing context (if any).

Tags are key / value pairs which generally represent things like application version, environment, role, and server names.

Examples:

Raven.tags_context('my_custom_tag' => 'tag_value')


178
179
180
# File 'lib/raven/base.rb', line 178

def tags_context(options = {})
  self.context.tags.merge!(options)
end

.user_context(options = {}) ⇒ Object

Bind user context. Merges with existing context (if any).

It is recommending that you send at least the “id“ and “email“ values. All other values are arbitrary.

Examples:

Raven.user_context('id' => 1, 'email' => '[email protected]')


167
168
169
# File 'lib/raven/base.rb', line 167

def user_context(options = {})
  self.context.user = options
end