class Bugsnag::Configuration

Constants

API_KEY_REGEX
DEFAULT_ENDPOINT
DEFAULT_MAX_BREADCRUMBS
DEFAULT_META_DATA_FILTERS
DEFAULT_NOTIFY_ENDPOINT
DEFAULT_SCOPES_TO_FILTER

@api private

DEFAULT_SESSION_ENDPOINT
DEFAULT_VENDOR_PATH

Path to vendored code. Used to mark file paths as out of project.

PROG_NAME
THREAD_LOCAL_NAME

Attributes

api_key[RW]

Your Integration API Key @return [String, nil]

app_version[RW]

The current version of your application @return [String, nil]

auto_capture_sessions[RW]

Whether Bugsnag should automatically record sessions @deprecated Use {#auto_track_sessions} instead @return [Boolean]

auto_notify[RW]

Whether notifications should automatically be sent @return [Boolean]

before_breadcrumb_callbacks[RW]

Callables to be run before a breadcrumb is logged @return [Array<#call>]

ca_file[RW]

@return [String, nil]

context[RW]

The default context for all future events Setting this will disable automatic context setting @return [String, nil]

discard_classes[RW]

Exception classes that will be discarded and not sent to Bugsnag @return [Set<String, Regexp>]

enable_events[R]

Whether events will be delivered @api private @return [Boolean]

enable_sessions[R]

Whether sessions will be delivered @api private @return [Boolean]

enabled_automatic_breadcrumb_types[RW]

A list of strings indicating allowable automatic breadcrumb types @deprecated Use {#enabled_breadcrumb_types} instead @see Bugsnag::BreadcrumbType @return [Array<String>]

endpoints[R]

The URLs to send events and sessions to @return [EndpointConfiguration]

hostname[RW]

The name or descriptor of the Ruby server host @return [String]

ignore_classes[RW]

@deprecated Use {#discard_classes} instead @return [Set<Class, Proc>]

internal_middleware[RW]

@api private @return [MiddlewareStack]

logger[RW]

The logger to use for Bugsnag log messages @return [Logger]

max_breadcrumbs[R]

The maximum allowable amount of breadcrumbs per thread @return [Integer]

meta_data_filters[RW]

A list of keys that should be filtered out from the report and breadcrumb metadata before sending them to Bugsnag @deprecated Use {#redacted_keys} instead @return [Set<String, Regexp>]

metadata[R]

Global metadata added to every event @return [Hash]

middleware[RW]

The middleware stack that will run on every notification @return [MiddlewareStack]

notify_release_stages[RW]

A list of which release stages should cause notifications to be sent @deprecated Use {#enabled_release_stages} instead @return [Array<String>, nil]

on_breadcrumb_callbacks[R]

Expose on_breadcrumb_callbacks internally for Bugsnag.leave_breadcrumb @api private @return [Breadcrumbs::OnBreadcrumbCallbackList]

project_root[RW]

Any stacktrace lines that match this path will be marked as ‘in project’ @return [String, nil]

proxy_host[RW]

The host address of the HTTP proxy that should be used when making requests @see parse_proxy @return [String, nil]

proxy_password[RW]

The password for the user that should be used when making requests via a HTTP proxy @see parse_proxy @return [String, nil]

proxy_port[RW]

The port number of the HTTP proxy that should be used when making requests @see parse_proxy @return [Integer, nil]

proxy_user[RW]

The user that should be used when making requests via a HTTP proxy @see parse_proxy @return [String, nil]

redacted_keys[RW]

A set of keys that should be redacted from the report and breadcrumb metadata before sending them to Bugsnag

When adding strings, keys that are equal to the string (ignoring case) will be redacted. When adding regular expressions, any keys which match the regular expression will be redacted

@return [Set<String, Regexp>]

release_stage[RW]

The current stage of the release process, e.g. ‘development’, production’ @return [String, nil]

runtime_versions[RW]

@api private @return [Hash{String => String}]

scopes_to_filter[RW]

@api private @return [Array<String>]

send_code[RW]

Whether code snippets from the exception stacktrace should be sent with notifications @return [Boolean]

send_environment[RW]

Whether to automatically attach the Rack environment to notifications @return [Boolean]

timeout[RW]

The HTTP request timeout, defaults to 15 seconds @return [Integer]

track_sessions[RW]

Whether Bugsnag should automatically record sessions @deprecated Use {#auto_track_sessions} instead @return [Boolean]

track_sessions=[RW]

Whether Bugsnag should automatically record sessions @deprecated Use {#auto_track_sessions} instead @return [Boolean]

vendor_path[RW]

@deprecated Use {vendor_paths} instead @return [Regexp]

vendor_paths[RW]

An array of paths within the {project_root} that should not be considered as “in project”

These paths should be relative to the {project_root} and will only match whole directory names

@return [Array<String>]

Public Class Methods

new() click to toggle source
# File lib/bugsnag/configuration.rb, line 224
def initialize
  @mutex = Mutex.new

  # Set up the defaults
  self.auto_notify = true
  self.send_environment = false
  self.send_code = true
  self.meta_data_filters = Set.new(DEFAULT_META_DATA_FILTERS)
  @redacted_keys = Set.new
  self.scopes_to_filter = DEFAULT_SCOPES_TO_FILTER
  self.hostname = default_hostname
  self.runtime_versions = {}
  self.runtime_versions["ruby"] = RUBY_VERSION
  self.runtime_versions["jruby"] = JRUBY_VERSION if defined?(JRUBY_VERSION)
  self.timeout = 15
  self.release_stage = ENV['BUGSNAG_RELEASE_STAGE']
  self.notify_release_stages = nil
  self.auto_capture_sessions = true

  # All valid breadcrumb types should be allowable initially
  self.enabled_automatic_breadcrumb_types = Bugsnag::Breadcrumbs::VALID_BREADCRUMB_TYPES.dup
  self.before_breadcrumb_callbacks = []
  @on_breadcrumb_callbacks = Breadcrumbs::OnBreadcrumbCallbackList.new(self)

  # Store max_breadcrumbs here instead of outputting breadcrumbs.max_items
  # to avoid infinite recursion when creating breadcrumb buffer
  @max_breadcrumbs = DEFAULT_MAX_BREADCRUMBS

  @endpoints = EndpointConfiguration.new(DEFAULT_NOTIFY_ENDPOINT, DEFAULT_SESSION_ENDPOINT)

  @enable_events = true
  @enable_sessions = true

  @metadata = {}
  @metadata_delegate = Utility::MetadataDelegate.new

  # SystemExit and SignalException are common Exception types seen with
  # successful exits and are not automatically reported to Bugsnag
  # TODO move these defaults into `discard_classes` when `ignore_classes`
  #      is removed
  self.ignore_classes = Set.new([SystemExit, SignalException])
  self.discard_classes = Set.new([])

  # Read the API key from the environment
  self.api_key = ENV["BUGSNAG_API_KEY"]

  # Read NET::HTTP proxy environment variables
  if (proxy_uri = ENV["https_proxy"] || ENV['http_proxy'])
    parse_proxy(proxy_uri)
  end

  # Set up vendor_path regex to mark stacktrace file paths as out of project.
  # Stacktrace lines that matches regex will be marked as "out of project"
  # will only appear in the full trace.
  self.vendor_path = DEFAULT_VENDOR_PATH
  @vendor_paths = []

  # Set up logging
  self.logger = Logger.new(STDOUT)
  self.logger.level = Logger::INFO
  self.logger.formatter = proc do |severity, datetime, progname, msg|
    "** #{progname} #{datetime}: #{msg}\n"
  end

  # Configure the bugsnag middleware stack
  self.internal_middleware = Bugsnag::MiddlewareStack.new
  self.internal_middleware.use Bugsnag::Middleware::ExceptionMetaData
  self.internal_middleware.use Bugsnag::Middleware::DiscardErrorClass
  self.internal_middleware.use Bugsnag::Middleware::IgnoreErrorClass
  self.internal_middleware.use Bugsnag::Middleware::SuggestionData
  self.internal_middleware.use Bugsnag::Middleware::ClassifyError
  self.internal_middleware.use Bugsnag::Middleware::SessionData
  self.internal_middleware.use Bugsnag::Middleware::Breadcrumbs

  self.middleware = Bugsnag::MiddlewareStack.new
  self.middleware.use Bugsnag::Middleware::Callbacks
end

Public Instance Methods

add_metadata(section, key_or_data, *args) click to toggle source

Add values to metadata

@overload add_metadata(section, data)

Merges data into the given section of metadata
@param section [String, Symbol]
@param data [Hash]

@overload add_metadata(section, key, value)

Sets key to value in the given section of metadata. If the value is nil
the key will be deleted
@param section [String, Symbol]
@param key [String, Symbol]
@param value

@return [void]

# File lib/bugsnag/configuration.rb, line 664
def add_metadata(section, key_or_data, *args)
  @mutex.synchronize do
    @metadata_delegate.add_metadata(@metadata, section, key_or_data, *args)
  end
end
add_on_breadcrumb(callback) click to toggle source

Add the given callback to the list of on_breadcrumb callbacks

The on_breadcrumb callbacks will be called when a breadcrumb is left and are passed the {Breadcrumbs::Breadcrumb Breadcrumb} object

Returning false from an on_breadcrumb callback will cause the breadcrumb to be ignored and will prevent any remaining callbacks from being called

@param callback [Proc, Method, call] @return [void]

# File lib/bugsnag/configuration.rb, line 632
def add_on_breadcrumb(callback)
  @on_breadcrumb_callbacks.add(callback)
end
add_on_error(callback) click to toggle source

Add the given callback to the list of on_error callbacks

The on_error callbacks will be called when an error is captured or reported and are passed a {Bugsnag::Report} object

Returning false from an on_error callback will cause the error to be ignored and will prevent any remaining callbacks from being called

@param callback [Proc, Method, call] @return [void]

# File lib/bugsnag/configuration.rb, line 605
def add_on_error(callback)
  middleware.use(callback)
end
app_type() click to toggle source

Get the type of application executing the current code

This is usually used to represent if you are running in a Rails server, Sidekiq job, Rake task etc… Bugsnag will automatically detect most application types for you

@return [String, nil]

# File lib/bugsnag/configuration.rb, line 343
def app_type
  @app_type || @detected_app_type
end
app_type=(app_type) click to toggle source

Set the type of application executing the current code

If an app_type is set, this will be used instead of the automatically detected app_type that Bugsnag would otherwise use

@param app_type [String] @return [void]

# File lib/bugsnag/configuration.rb, line 355
def app_type=(app_type)
  @app_type = app_type
end
auto_track_sessions() click to toggle source

Whether sessions should be tracked automatically @!attribute auto_track_sessions @return [Boolean]

# File lib/bugsnag/configuration.rb, line 736
def auto_track_sessions
  @auto_capture_sessions
end
auto_track_sessions=(track_sessions) click to toggle source

@param track_sessions [Boolean] @return [void]

# File lib/bugsnag/configuration.rb, line 742
def auto_track_sessions=(track_sessions)
  @auto_capture_sessions = track_sessions
end
breadcrumbs() click to toggle source

Returns the current list of breadcrumbs

This is a per-thread circular buffer, containing at most ‘max_breadcrumbs’ breadcrumbs

@return [Bugsnag::Utility::CircularBuffer]

clear_metadata(section, *args) click to toggle source

Clear values from metadata

@overload clear_metadata(section)

Clears the given section of metadata
@param section [String, Symbol]

@overload clear_metadata(section, key)

Clears the key in the given section of metadata
@param section [String, Symbol]
@param key [String, Symbol]

@return [void]

# File lib/bugsnag/configuration.rb, line 683
def clear_metadata(section, *args)
  @mutex.synchronize do
    @metadata_delegate.clear_metadata(@metadata, section, *args)
  end
end
clear_request_data() click to toggle source

Clears the array of data attached to every error notification.

@return [void]

# File lib/bugsnag/configuration.rb, line 432
def clear_request_data
  Thread.current[THREAD_LOCAL_NAME] = nil
end
context_set?() click to toggle source

Has the context been explicitly set?

This is necessary to differentiate between the context not being set and the context being set to ‘nil’ explicitly

@api private @return [Boolean]

# File lib/bugsnag/configuration.rb, line 697
def context_set?
  defined?(@context) != nil
end
debug(message) click to toggle source

Logs a debug level message

@param message [String, to_s] The message to log

# File lib/bugsnag/configuration.rb, line 464
def debug(message)
  logger.debug(PROG_NAME) { message }
end
default_delivery_method=(delivery_method) click to toggle source

Used to set a new default delivery method that will be used if one is not set with delivery_method.

@api private

@param delivery_method [Symbol] @return [void]

# File lib/bugsnag/configuration.rb, line 331
def default_delivery_method=(delivery_method)
  @default_delivery_method = delivery_method
end
delivery_method() click to toggle source

Gets the delivery_method that Bugsnag will use to communicate with the notification endpoint.

@return [Symbol]

# File lib/bugsnag/configuration.rb, line 307
def delivery_method
  @delivery_method || @default_delivery_method || :thread_queue
end
delivery_method=(delivery_method) click to toggle source

Sets the delivery_method that Bugsnag will use to communicate with the notification endpoint.

The default delivery methods are ‘:thread_queue’ and ‘:synchronous’.

@param delivery_method [Symbol] @return [void]

# File lib/bugsnag/configuration.rb, line 319
def delivery_method=(delivery_method)
  @delivery_method = delivery_method
end
detected_app_type() click to toggle source

Get the detected app_type, which is used when one isn’t set explicitly

@api private

@return [String, nil]

# File lib/bugsnag/configuration.rb, line 365
def detected_app_type
  @detected_app_type
end
detected_app_type=(app_type) click to toggle source

Set the detected app_type, which is used when one isn’t set explicitly

This allows Bugsnag’s integrations to say ‘this is a Rails app’ while allowing the user to overwrite this if they wish

@api private

@param app_type [String] @return [void]

# File lib/bugsnag/configuration.rb, line 379
def detected_app_type=(app_type)
  @detected_app_type = app_type
end
disable_sessions() click to toggle source

Disables session tracking and delivery. Cannot be undone

@return [void]

# File lib/bugsnag/configuration.rb, line 575
def disable_sessions
  self.auto_capture_sessions = false
  @enable_sessions = false
end
enabled_breadcrumb_types() click to toggle source

A list of breadcrumb types that Bugsnag will collect automatically @!attribute enabled_breadcrumb_types @see Bugsnag::BreadcrumbType @return [Array<String>]

# File lib/bugsnag/configuration.rb, line 723
def enabled_breadcrumb_types
  @enabled_automatic_breadcrumb_types
end
enabled_breadcrumb_types=(breadcrumb_types) click to toggle source

@param breadcrumb_types [Array<String>] @return [void]

# File lib/bugsnag/configuration.rb, line 729
def enabled_breadcrumb_types=(breadcrumb_types)
  @enabled_automatic_breadcrumb_types = breadcrumb_types
end
enabled_release_stages() click to toggle source

A list of which release stages should cause notifications to be sent @!attribute enabled_release_stages @return [Array<String>, nil]

# File lib/bugsnag/configuration.rb, line 709
def enabled_release_stages
  @notify_release_stages
end
enabled_release_stages=(release_stages) click to toggle source

@param release_stages [Array<String>, nil] @return [void]

# File lib/bugsnag/configuration.rb, line 715
def enabled_release_stages=(release_stages)
  @notify_release_stages = release_stages
end
endpoint()
Alias for: notify_endpoint
endpoint=(new_notify_endpoint) click to toggle source

Sets the notification endpoint

@deprecated Use {#endpoints} instead

@param new_notify_endpoint [String] The URL to deliver error notifications to @return [void]

# File lib/bugsnag/configuration.rb, line 518
def endpoint=(new_notify_endpoint)
  warn("The 'endpoint' configuration option is deprecated. Set both endpoints with the 'endpoints=' method instead")
  set_endpoints(new_notify_endpoint, session_endpoint) # Pass the existing session_endpoint through so it doesn't get overwritten
end
endpoints=(endpoint_configuration) click to toggle source
# File lib/bugsnag/configuration.rb, line 554
def endpoints=(endpoint_configuration)
  result = EndpointValidator.validate(endpoint_configuration)

  if result.valid?
    @enable_events = true
    @enable_sessions = true
  else
    warn(result.reason)

    @enable_events = result.keep_events_enabled_for_backwards_compatibility?
    @enable_sessions = false
  end

  # use the given endpoints even if they are invalid
  @endpoints = endpoint_configuration
end
error(message) click to toggle source

Logs an error level message

@param message [String, to_s] The message to log

# File lib/bugsnag/configuration.rb, line 456
def error(message)
  logger.error(PROG_NAME) { message }
end
info(message) click to toggle source

Logs an info level message

@param message [String, to_s] The message to log

# File lib/bugsnag/configuration.rb, line 440
def info(message)
  logger.info(PROG_NAME) { message }
end
max_breadcrumbs=(new_max_breadcrumbs) click to toggle source

Sets the maximum allowable amount of breadcrumbs

@param new_max_breadcrumbs [Integer] the new maximum breadcrumb limit @return [void]

# File lib/bugsnag/configuration.rb, line 486
def max_breadcrumbs=(new_max_breadcrumbs)
  @max_breadcrumbs = new_max_breadcrumbs
  breadcrumbs.max_items = new_max_breadcrumbs
end
notify_endpoint() click to toggle source

The URL error notifications will be delivered to @!attribute notify_endpoint @return [String] @deprecated Use {#endpoints} instead

# File lib/bugsnag/configuration.rb, line 506
def notify_endpoint
  @endpoints.notify
end
Also aliased as: endpoint
on_error(&block) click to toggle source

Add the given block to the list of on_error callbacks

The on_error callbacks will be called when an error is captured or reported and are passed a {Bugsnag::Report} object

Returning false from an on_error callback will cause the error to be ignored and will prevent any remaining callbacks from being called

@return [void]

# File lib/bugsnag/configuration.rb, line 590
def on_error(&block)
  middleware.use(block)
end
parse_proxy(uri) click to toggle source

Parses and sets proxy from a uri

@param uri [String, to_s] The URI to parse and extract proxy details from @return [void]

# File lib/bugsnag/configuration.rb, line 473
def parse_proxy(uri)
  proxy = URI.parse(uri)
  self.proxy_host = proxy.host
  self.proxy_port = proxy.port
  self.proxy_user = proxy.user
  self.proxy_password = proxy.password
end
remove_on_breadcrumb(callback) click to toggle source

Remove the given callback from the list of on_breadcrumb callbacks

Note that this must be the same instance that was passed to {add_on_breadcrumb}, otherwise it will not be removed

@param callback [Proc, Method, call] @return [void]

# File lib/bugsnag/configuration.rb, line 644
def remove_on_breadcrumb(callback)
  @on_breadcrumb_callbacks.remove(callback)
end
remove_on_error(callback) click to toggle source

Remove the given callback from the list of on_error callbacks

Note that this must be the same instance that was passed to {#add_on_error}, otherwise it will not be removed

@param callback [Proc, Method, call] @return [void]

# File lib/bugsnag/configuration.rb, line 617
def remove_on_error(callback)
  middleware.remove(callback)
end
request_data() click to toggle source

Returns the array of data that will be automatically attached to every error notification.

@return [Hash]

# File lib/bugsnag/configuration.rb, line 405
def request_data
  Thread.current[THREAD_LOCAL_NAME] ||= {}
end
session_endpoint() click to toggle source

The URL session notifications will be delivered to @!attribute session_endpoint @return [String] @deprecated Use {#endpoints} instead

# File lib/bugsnag/configuration.rb, line 527
def session_endpoint
  @endpoints.sessions
end
session_endpoint=(new_session_endpoint) click to toggle source

Sets the sessions endpoint

@deprecated Use {#endpoints} instead

@param new_session_endpoint [String] The URL to deliver session notifications to @return [void]

# File lib/bugsnag/configuration.rb, line 538
def session_endpoint=(new_session_endpoint)
  warn("The 'session_endpoint' configuration option is deprecated. Set both endpoints with the 'endpoints=' method instead")
  set_endpoints(notify_endpoint, new_session_endpoint) # Pass the existing notify_endpoint through so it doesn't get overwritten
end
set_endpoints(new_notify_endpoint, new_session_endpoint) click to toggle source

Sets the notification and session endpoints

@param new_notify_endpoint [String] The URL to deliver error notifications to @param new_session_endpoint [String] The URL to deliver session notifications to @return [void] @deprecated Use {#endpoints} instead

# File lib/bugsnag/configuration.rb, line 550
def set_endpoints(new_notify_endpoint, new_session_endpoint)
  self.endpoints = EndpointConfiguration.new(new_notify_endpoint, new_session_endpoint)
end
set_request_data(key, value) click to toggle source

Sets an entry in the array of data attached to every error notification.

@param key [String, to_s] @param value [Object] @return [void]

# File lib/bugsnag/configuration.rb, line 415
def set_request_data(key, value)
  self.request_data[key] = value
end
should_notify_release_stage?() click to toggle source

Indicates whether the notifier should send a notification based on the configured release stage.

@return [Boolean]

# File lib/bugsnag/configuration.rb, line 388
def should_notify_release_stage?
  @release_stage.nil? || @notify_release_stages.nil? || @notify_release_stages.include?(@release_stage)
end
unset_request_data(key, value) click to toggle source

Unsets an entry in the array of data attached to every error notification.

@param (see set_request_data) @return [void]

# File lib/bugsnag/configuration.rb, line 424
def unset_request_data(key, value)
  self.request_data.delete(key)
end
valid_api_key?() click to toggle source

Tests whether the configured API key is valid.

@return [Boolean]

# File lib/bugsnag/configuration.rb, line 396
def valid_api_key?
  !api_key.nil? && api_key =~ API_KEY_REGEX
end
warn(message) click to toggle source

Logs a warning level message

@param message [String, to_s] The message to log

# File lib/bugsnag/configuration.rb, line 448
def warn(message)
  logger.warn(PROG_NAME) { message }
end

Private Instance Methods

default_hostname() click to toggle source
# File lib/bugsnag/configuration.rb, line 752
def default_hostname
  # Send the heroku dyno name instead of hostname if available
  ENV["DYNO"] || Socket.gethostname;
end