Module: Honeybadger

Extended by:
Honeybadger
Includes:
Forwardable
Included in:
Honeybadger
Defined in:
lib/honeybadger/init/rake.rb,
lib/honeybadger.rb,
lib/honeybadger/cli.rb,
lib/honeybadger/agent.rb,
lib/honeybadger/const.rb,
lib/honeybadger/trace.rb,
lib/honeybadger/config.rb,
lib/honeybadger/notice.rb,
lib/honeybadger/plugin.rb,
lib/honeybadger/backend.rb,
lib/honeybadger/logging.rb,
lib/honeybadger/version.rb,
lib/honeybadger/cli/main.rb,
lib/honeybadger/backtrace.rb,
lib/honeybadger/util/http.rb,
lib/honeybadger/cli/heroku.rb,
lib/honeybadger/config/env.rb,
lib/honeybadger/init/rails.rb,
lib/honeybadger/util/stats.rb,
lib/honeybadger/agent/batch.rb,
lib/honeybadger/cli/helpers.rb,
lib/honeybadger/config/yaml.rb,
lib/honeybadger/agent/worker.rb,
lib/honeybadger/backend/base.rb,
lib/honeybadger/backend/null.rb,
lib/honeybadger/backend/test.rb,
lib/honeybadger/init/sinatra.rb,
lib/honeybadger/plugins/thor.rb,
lib/honeybadger/backend/debug.rb,
lib/honeybadger/plugins/rails.rb,
lib/honeybadger/backend/server.rb,
lib/honeybadger/plugins/resque.rb,
lib/honeybadger/plugins/warden.rb,
lib/honeybadger/util/sanitizer.rb,
lib/honeybadger/config/defaults.rb,
lib/honeybadger/plugins/sidekiq.rb,
lib/honeybadger/plugins/unicorn.rb,
lib/honeybadger/config/callbacks.rb,
lib/honeybadger/plugins/net_http.rb,
lib/honeybadger/agent/null_worker.rb,
lib/honeybadger/plugins/passenger.rb,
lib/honeybadger/rack/request_hash.rb,
lib/honeybadger/rack/user_feedback.rb,
lib/honeybadger/rack/user_informer.rb,
lib/honeybadger/agent/metered_queue.rb,
lib/honeybadger/plugins/delayed_job.rb,
lib/honeybadger/rack/error_notifier.rb,
lib/honeybadger/util/request_payload.rb,
lib/honeybadger/rack/metrics_reporter.rb,
lib/honeybadger/agent/trace_collection.rb,
lib/honeybadger/agent/metrics_collector.rb,
lib/honeybadger/plugins/local_variables.rb,
lib/honeybadger/agent/metrics_collection.rb,
lib/honeybadger/plugins/delayed_job/plugin.rb

Overview

Internal: A collection for de-duping traces. Not currently thread-safe (so make sure access is synchronized.)

Defined Under Namespace

Modules: Backend, CLI, Init, Logging, Plugins, Rack, RakeHandler, TraceCleaner, Util Classes: Agent, Backtrace, Config, Notice, Plugin, Trace

Constant Summary collapse

NOTIFIER =
{
  name: 'honeybadger-ruby'.freeze,
  url: 'https://github.com/honeybadger-io/honeybadger-ruby'.freeze,
  version: VERSION,
  language: 'ruby'.freeze
}.freeze
GEM_ROOT =

Internal: Substitution for gem root in backtrace lines.

'[GEM_ROOT]'.freeze
PROJECT_ROOT =

Internal: Substitution for project root in backtrace lines.

'[PROJECT_ROOT]'.freeze
STRING_EMPTY =

Internal: Empty String (used for equality comparisons and assignment)

''.freeze
NOT_BLANK =

Internal: A Regexp which matches non-blank characters.

/\S/.freeze
RELATIVE_ROOT =

Internal: Matches lines beginning with ./

Regexp.new('^\.\/').freeze
MAX_EXCEPTION_CAUSES =
5
VERSION =

Public: The current String Honeybadger version.

'2.1.1'.freeze

Instance Method Summary collapse

Instance Method Details

#backtrace_filter(&block) ⇒ Object

Public: Callback to filter backtrace lines. One use for this is to make additional [PROJECT_ROOT] or [GEM_ROOT] substitutions, which are used by Honeybadger when grouping errors and displaying application traces.

block - A block which can be used to modify the Backtrace lines sent to

Honeybadger. The block expects one argument (line) which is the String line
from the Backtrace, and must return the String new line.

Examples:

Honeybadger.backtrace_filter do |line|
  line.gsub(/^\/my\/unknown\/bundle\/path/, "[GEM_ROOT]")
end

Returns nothing.



143
144
145
# File 'lib/honeybadger.rb', line 143

def backtrace_filter(&block)
  Agent.backtrace_filter(&block)
end

#clear!Object

Internal: Clears the global context



175
176
177
# File 'lib/honeybadger.rb', line 175

def clear!
  Thread.current[:__honeybadger_context] = nil
end

#configure(*args) ⇒ Object



213
214
215
216
# File 'lib/honeybadger.rb', line 213

def configure(*args)
  warn('UPGRADE WARNING: Honeybadger.configure was removed in v2.0 and has no effect. Please upgrade: https://www.honeybadger.io/s/gem-upgrade')
  nil
end

#context(hash = nil) ⇒ Object

Public: Save global context for the current request.

hash - A Hash of data which will be sent to Honeybadger when an error

occurs. (default: nil)

Examples:

Honeybadger.context({my_data: 'my value'})

# Inside a Rails controller:
before_action do
  Honeybadger.context({user_id: current_user.id})
end

# Clearing global context:
Honeybadger.context.clear!

Returns self so that method calls can be chained.



165
166
167
168
169
170
171
172
# File 'lib/honeybadger.rb', line 165

def context(hash = nil)
  unless hash.nil?
    Thread.current[:__honeybadger_context] ||= {}
    Thread.current[:__honeybadger_context].merge!(hash)
  end

  self
end

#exception_filter(&block) ⇒ Object

Public: Callback to ignore exceptions.

See public API documentation for Honeybadger::Notice for available attributes.

block - A block returning TrueClass true (to ignore) or FalseClass false

(to send).

Examples:

# Ignoring based on error message:
Honeybadger.exception_filter do |notice|
  notice[:error_message] =~ /sensitive data/
end

# Ignore an entire class of exceptions:
Honeybadger.exception_filter do |notice|
  notice[:exception].class < MyError
end

Returns nothing.



105
106
107
# File 'lib/honeybadger.rb', line 105

def exception_filter(&block)
  Agent.exception_filter(&block)
end

#exception_fingerprint(&block) ⇒ Object

Public: Callback to add a custom grouping strategy for exceptions. The return value is hashed and sent to Honeybadger. Errors with the same fingerprint will be grouped.

See public API documentation for Honeybadger::Notice for available attributes.

block - A block returning any Object responding to #to_s.

Examples:

Honeybadger.exception_fingerprint do |notice|
  [notice[:error_class], notice[:component], notice[:backtrace].to_s].join(':')
end

Returns nothing.



124
125
126
# File 'lib/honeybadger.rb', line 124

def exception_fingerprint(&block)
  Agent.exception_fingerprint(&block)
end

#flush(&block) ⇒ Object

Public: Flushes all data from workers before returning. This is most useful in tests when using the test backend, where normally the asynchronous nature of this library could create race conditions.

block - The optional block to execute (exceptions will propagate after data is flushed).

Examples:

# Without a block:
it "sends a notification to Honeybadger" do
  expect {
    Honeybadger.notify(StandardError.new('test backend'))
    Honeybadger.flush
  }.to change(Honeybadger::Backend::Test.notifications[:notices], :size).by(1)
end

# With a block:
it "sends a notification to Honeybadger" do
  expect {
    Honeybadger.flush do
      50.times do
        Honeybadger.notify(StandardError.new('test backend'))
      end
    end
  }.to change(Honeybadger::Backend::Test.notifications[:notices], :size).by(50)
end

Returns value of block if block is given, otherwise true on success or false if Honeybadger isn’t running.



209
210
211
# File 'lib/honeybadger.rb', line 209

def flush(&block)
  Agent.flush(&block)
end

#notify(exception_or_opts, opts = {}) ⇒ Object Also known as: notify_or_ignore

Public: Send an exception to Honeybadger. Does not report ignored exceptions by default.

exception_or_opts - An Exception object, or a Hash of options which is used

to build the notice.

opts - The options Hash when the first argument is an

Exception. (default: {}):
:error_class   - The String class name of the error.
:error_message - The String error message.
:force         - Always report the exception (even when
                 ignored).

Examples:

# With an exception:
begin
  fail 'oops'
rescue => exception
  Honeybadger.notify(exception, context: {
    my_data: 'value'
  }) # => '0dfb92ae-9b01-42e9-9c13-31205b70744a'
end

# Custom notification:
Honeybadger.notify({
  error_class: 'MyClass',
  error_message: 'Something went wrong.',
  context: {my_data: 'value'}
}) # => '06221c5a-b471-41e5-baeb-de247da45a56'

Returns a String UUID reference to the notice within Honeybadger or false when ignored.



76
77
78
79
80
# File 'lib/honeybadger.rb', line 76

def notify(exception_or_opts, opts = {})
  opts.merge!(exception: exception_or_opts) if exception_or_opts.is_a?(Exception)
  opts.merge!(exception_or_opts.to_hash) if exception_or_opts.respond_to?(:to_hash)
  Agent.instance ? Agent.instance.notice(opts) : false
end

#start(config = {}) ⇒ Object

Public: Starts the Honeybadger service.

opts - The Hash options used to initialize Honeybadger. Accepts config

keys in addition to the listed options. Order of precedence for
config is: 1) ENV, 2) config on disk, 3) opts. (default: {})
:logger - An alternate Logger to use. (optional)

Examples:

ENV['HONEYBADGER_API_KEY'] # => 'asdf'

Honeybadger.start # => true

Honeybadger.start({
  :root          => ::Rails.root,
  :'config.path' => 'config/',
  :logger        => Honeybadger::Logging::FormattedLogger.new(::Rails.logger)
}) # => true

Returns true if started, otherwise false.



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

def start(config = {})
  Agent.start(config)
end

#stopObject

Public: Stops the Honeybadger service.

Examples:

Honeybadger.stop # => nil

Returns nothing



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

def stop
  Agent.stop
end