module Raven
Inspired by Rails' and Airbrake's backtrace parsers.
A much simpler source line cacher because linecache sucks at platform compat
frozen_string_literal: true
Some parts adapted from golang.org/src/pkg/json/decode.go and golang.org/src/pkg/utf8/utf8.go
frozen_string_literal: true
Constants
- AVAILABLE_INTEGRATIONS
- INTERFACES
TODO: a constant isn't appropriate here, refactor
- VERSION
Attributes
The client object is responsible for delivering formatted data to the Sentry server. Must respond to send. See Raven::Client.
A Raven configuration object. Must act like a hash and return sensible values for all Raven configuration options. See Raven::Configuration.
Public Class Methods
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.
@example
begin raise "Hello" rescue => exc Raven.annotate_exception(exc, :user => { 'id' => 1, 'email' => 'foo@example.com' }) end
# File lib/raven/base.rb, line 156 def annotate_exception(exc, options = {}) notes = (exc.instance_variable_defined?(:@__raven_context) && exc.instance_variable_get(:@__raven_context)) || {} notes.merge!(options) exc.instance_variable_set(:@__raven_context, notes) exc end
Capture and process any exceptions from the given block, or globally if no block is given
@example
Raven.capture do MyApp.run end
# File lib/raven/base.rb, line 93 def capture(options = {}) if block_given? begin yield rescue Error raise # Don't capture Raven errors rescue Exception => e capture_exception(e, options) raise end else install_at_exit_hook(options) end end
# File lib/raven/base.rb, line 108 def capture_type(obj, options = {}) return false unless should_capture?(obj) message_or_exc = obj.is_a?(String) ? "message" : "exception" if (evt = Event.send("from_" + message_or_exc, obj, options)) yield evt if block_given? if configuration.async? configuration.async.call(evt) else send_event(evt) end Thread.current[:sentry_last_event_id] = evt.id evt end end
The client object is responsible for delivering formatted data to the Sentry server.
# File lib/raven/base.rb, line 48 def client @client ||= Client.new(configuration) end
The configuration object. @see ::configure
# File lib/raven/base.rb, line 43 def configuration @configuration ||= Configuration.new end
Call this method to modify defaults in your initializers.
@example
Raven.configure do |config| config.server = 'http://...' end
# File lib/raven/base.rb, line 69 def configure yield(configuration) if block_given? self.client = Client.new(configuration) report_status self.client end
# File lib/raven/base.rb, line 33 def context Context.current end
Bind extra context. Merges with existing context (if any).
Extra context shows up as Additional Data within Sentry, and is completely arbitrary.
@example
Raven.extra_context('my_custom_data' => 'value')
# File lib/raven/base.rb, line 191 def extra_context(options = nil) self.context.extra.merge!(options || {}) end
# File lib/raven/interfaces.rb, line 31 def self.find_interface(name) INTERFACES[name.to_s] end
Injects various integrations. Default behavior: inject all available integrations
# File lib/raven/base.rb, line 203 def inject inject_only(*Raven::AVAILABLE_INTEGRATIONS) end
# File lib/raven/base.rb, line 212 def inject_only(*only_integrations) only_integrations = only_integrations.map(&:to_s) integrations_to_load = Raven::AVAILABLE_INTEGRATIONS & only_integrations not_found_integrations = only_integrations - integrations_to_load if not_found_integrations.any? self.logger.warn "Integrations do not exist: #{not_found_integrations.join ', '}" end integrations_to_load &= Gem.loaded_specs.keys # TODO(dcramer): integrations should have some additional checks baked-in # or we should break them out into their own repos. Specifically both the # rails and delayed_job checks are not always valid (i.e. Rails 2.3) and # https://github.com/getsentry/raven-ruby/issues/180 integrations_to_load.each do |integration| load_integration(integration) end end
# File lib/raven/base.rb, line 207 def inject_without(*exclude_integrations) include_integrations = Raven::AVAILABLE_INTEGRATIONS - exclude_integrations.map(&:to_s) inject_only(*include_integrations) end
# File lib/raven/base.rb, line 125 def last_event_id Thread.current[:sentry_last_event_id] end
# File lib/raven/base.rb, line 229 def load_integration(integration) require "raven/integrations/#{integration}" rescue Exception => error self.logger.warn "Unable to load raven/integrations/#{integration}: #{error}" end
# File lib/raven/base.rb, line 37 def logger @logger ||= Logger.new end
# File lib/raven/base.rb, line 195 def rack_context(env) if env.empty? env = nil end self.context.rack_env = env end
# File lib/raven/base.rb, line 235 def rails_safely_prepend(module_name, opts = {}) return if opts[:to].nil? if opts[:to].respond_to?(:prepend, true) opts[:to].send(:prepend, Raven::Rails::Overrides.const_get(module_name)) else opts[:to].send(:include, Raven::Rails::Overrides.const_get("Old" + module_name)) end end
# File lib/raven/interfaces.rb, line 24 def self.register_interface(mapping) mapping.each_pair do |key, klass| INTERFACES[key.to_s] = klass INTERFACES[klass.name] = klass end end
Tell the log that the client is good to go
# File lib/raven/base.rb, line 53 def report_status return if client.configuration.silence_ready if client.configuration.send_in_current_environment? logger.info "Raven #{VERSION} ready to catch errors" else logger.info "Raven #{VERSION} configured not to send errors." end end
Send an event to the configured Sentry server
@example
evt = Raven::Event.new(:message => "An error") Raven.send_event(evt)
# File lib/raven/base.rb, line 82 def send_event(event) client.send_event(event) end
# File lib/raven/base.rb, line 129 def should_capture?(message_or_exc) if configuration.should_capture configuration.should_capture.call(*[message_or_exc]) else true end end
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.
@example
Raven.user_context('id' => 1, 'email' => 'foo@example.com')
# File lib/raven/base.rb, line 170 def user_context(options = nil) self.context.user = options || {} end