class Sentry::Configuration

Constants

HEROKU_DYNO_METADATA_MESSAGE
IGNORE_DEFAULT

Most of these errors generate 4XX responses. In general, Sentry clients only automatically report 5xx responses.

LOG_PREFIX
MODULE_SEPARATOR
RACK_ENV_WHITELIST_DEFAULT

Attributes

app_dirs_pattern[RW]

Directories to be recognized as part of your app. e.g. if you have an `engines` dir at the root of your project, you may want to set this to something like /(app|config|engines|lib)/

async[R]

Provide an object that responds to `call` to send events asynchronously. E.g.: lambda { |event| Thread.new { Sentry.send_event(event) } }

background_worker_threads[RW]

to send events in a non-blocking way, sentry-ruby has its own background worker by default, the worker holds a thread pool that has [the number of processors] threads but you can configure it with this configuration option E.g.: config.background_worker_threads = 5

if you want to send events synchronously, set the value to 0 E.g.: config.background_worker_threads = 0

backtrace_cleanup_callback[RW]

a proc/lambda that takes an array of stack traces it'll be used to silence (reduce) backtrace of the exception

for example:

“`ruby Sentry.configuration.backtrace_cleanup_callback = lambda do |backtrace|

Rails.backtrace_cleaner.clean(backtrace)

end “`

before_breadcrumb[R]

Optional Proc, called before adding the breadcrumb to the current scope E.g.: lambda { |breadcrumb, hint| breadcrumb } E.g.: lambda { |breadcrumb, hint| nil } E.g.: lambda { |breadcrumb, hint|

breadcrumb.message = 'a'
breadcrumb

}

before_send[R]

Optional Proc, called before sending an event to the server/ E.g.: lambda { |event, hint| event } E.g.: lambda { |event, hint| nil } E.g.: lambda { |event, hint|

event[:message] = 'a'
event

}

breadcrumbs_logger[R]

An array of breadcrumbs loggers to be used. Available options are:

  • :sentry_logger

  • :active_support_logger

context_lines[RW]

Number of lines of code context to capture, or nil for none

debug[RW]

Whether the SDK should run in the debugging mode. Default is false. If set to true, SDK errors will be logged with backtrace

dsn[R]

the dsn value, whether it's set via `config.dsn=` or `ENV`

enabled_environments[RW]

Whitelist of enabled_environments that will send notifications to Sentry. Array of Strings.

environment[R]

RACK_ENV by default.

errors[R]

these are not config options

exclude_loggers[RW]

Logger 'progname's to exclude from breadcrumbs

excluded_exceptions[RW]

Array of exception classes that should never be sent. See IGNORE_DEFAULT. You should probably append to this rather than overwrite it.

gem_specs[R]

these are not config options

inspect_exception_causes_for_exclusion[RW]

Boolean to check nested exceptions when deciding if to exclude. Defaults to true

inspect_exception_causes_for_exclusion?[RW]

Boolean to check nested exceptions when deciding if to exclude. Defaults to true

linecache[RW]

You may provide your own LineCache for matching paths with source files. This may be useful if you need to get source code from places other than the disk. See Sentry::LineCache for the required interface you must implement.

logger[RW]

Logger used by Sentry. In Rails, this is the Rails logger, otherwise Sentry provides its own Sentry::Logger.

max_breadcrumbs[RW]

Max number of breadcrumbs a breadcrumb buffer can hold

project_root[R]

Project directory root for in_app detection. Could be Rails root, etc. Set automatically for Rails.

propagate_traces[RW]

Insert sentry-trace to outgoing requests' headers

rack_env_whitelist[RW]

Array of rack env parameters to be included in the event sent to sentry.

release[RW]

Release tag to be passed with every event sent to Sentry. We automatically try to set this to a git SHA or Capistrano release.

sample_rate[RW]

The sampling factor to apply to events. A value of 0.0 will not send any events, and a value of 1.0 will send 100% of events.

send_default_pii[RW]

When send_default_pii's value is false (default), sensitive information like

  • user ip

  • user cookie

  • request body

will not be sent to Sentry.

send_modules[RW]

Include module versions in reports - boolean.

server_name[RW]
skip_rake_integration[RW]

Allow to skip Sentry emails within rake tasks

traces_sample_rate[RW]

Take a float between 0.0 and 1.0 as the sample rate for tracing events (transactions).

traces_sampler[RW]

Take a Proc that controls the sample rate for every tracing event, e.g. “` lambda do |tracing_context|

# tracing_context[:transaction_context] contains the information about the transaction
# tracing_context[:parent_sampled] contains the transaction's parent's sample decision
true # return value can be a boolean or a float between 0.0 and 1.0

end “`

transport[R]

Return a Transport::Configuration object for transport-related configurations.

trusted_proxies[RW]

IP ranges for trusted proxies that will be skipped when calculating IP address.

Public Class Methods

new() click to toggle source
# File lib/sentry/configuration.rb, line 185
def initialize
  self.debug = false
  self.background_worker_threads = Concurrent.processor_count
  self.max_breadcrumbs = BreadcrumbBuffer::DEFAULT_SIZE
  self.breadcrumbs_logger = []
  self.context_lines = 3
  self.environment = environment_from_env
  self.enabled_environments = []
  self.exclude_loggers = []
  self.excluded_exceptions = IGNORE_DEFAULT.dup
  self.inspect_exception_causes_for_exclusion = true
  self.linecache = ::Sentry::LineCache.new
  self.logger = ::Sentry::Logger.new(STDOUT)
  self.project_root = Dir.pwd
  self.propagate_traces = true

  self.sample_rate = 1.0
  self.send_modules = true
  self.send_default_pii = false
  self.skip_rake_integration = false
  self.trusted_proxies = []
  self.dsn = ENV['SENTRY_DSN']
  self.server_name = server_name_from_env

  self.before_send = false
  self.rack_env_whitelist = RACK_ENV_WHITELIST_DEFAULT

  @transport = Transport::Configuration.new
  @gem_specs = Hash[Gem::Specification.map { |spec| [spec.name, spec.version.to_s] }] if Gem::Specification.respond_to?(:map)

  run_post_initialization_callbacks
end

Protected Class Methods

post_initialization_callbacks() click to toggle source
# File lib/sentry/configuration.rb, line 457
def self.post_initialization_callbacks
  @@post_initialization_callbacks
end

Private Class Methods

add_post_initialization_callback(&block) click to toggle source

allow extensions to add their hooks to the Configuration class

# File lib/sentry/configuration.rb, line 451
def self.add_post_initialization_callback(&block)
  self.post_initialization_callbacks << block
end

Public Instance Methods

async=(value) click to toggle source
# File lib/sentry/configuration.rb, line 227
def async=(value)
  if value && !value.respond_to?(:call)
    raise(ArgumentError, "async must be callable")
  end

  @async = value
end
before_breadcrumb=(value) click to toggle source
# File lib/sentry/configuration.rb, line 256
def before_breadcrumb=(value)
  unless value.nil? || value.respond_to?(:call)
    raise ArgumentError, "before_breadcrumb must be callable (or nil to disable)"
  end

  @before_breadcrumb = value
end
before_send=(value) click to toggle source
# File lib/sentry/configuration.rb, line 248
def before_send=(value)
  unless value == false || value.respond_to?(:call)
    raise ArgumentError, "before_send must be callable (or false to disable)"
  end

  @before_send = value
end
breadcrumbs_logger=(logger) click to toggle source
csp_report_uri() click to toggle source
# File lib/sentry/configuration.rb, line 327
def csp_report_uri
  if dsn && dsn.valid?
    uri = dsn.csp_report_uri
    uri += "&sentry_release=#{CGI.escape(release)}" if release && !release.empty?
    uri += "&sentry_environment=#{CGI.escape(environment)}" if environment && !environment.empty?
    uri
  end
end
detect_release() click to toggle source
# File lib/sentry/configuration.rb, line 316
def detect_release
  return unless sending_allowed?

  self.release ||= detect_release_from_env ||
    detect_release_from_git ||
    detect_release_from_capistrano ||
    detect_release_from_heroku
rescue => e
  log_error("Error detecting release", e, debug: debug)
end
dsn=(value) click to toggle source
# File lib/sentry/configuration.rb, line 218
def dsn=(value)
  return if value.nil? || value.empty?

  @dsn = DSN.new(value)
end
Also aliased as: server=
enabled_in_current_env?() click to toggle source
# File lib/sentry/configuration.rb, line 298
def enabled_in_current_env?
  enabled_environments.empty? || enabled_environments.include?(environment)
end
environment=(environment) click to toggle source
# File lib/sentry/configuration.rb, line 264
def environment=(environment)
  @environment = environment.to_s
end
error_messages() click to toggle source
# File lib/sentry/configuration.rb, line 276
def error_messages
  @errors = [@errors[0]] + @errors[1..-1].map(&:downcase) # fix case of all but first
  @errors.join(", ")
end
exception_class_allowed?(exc) click to toggle source
# File lib/sentry/configuration.rb, line 285
def exception_class_allowed?(exc)
  if exc.is_a?(Sentry::Error)
    # Try to prevent error reporting loops
    log_debug("Refusing to capture Sentry error: #{exc.inspect}")
    false
  elsif excluded_exception?(exc)
    log_debug("User excluded error: #{exc.inspect}")
    false
  else
    true
  end
end
project_root=(root_dir) click to toggle source
# File lib/sentry/configuration.rb, line 281
def project_root=(root_dir)
  @project_root = root_dir
end
sending_allowed?() click to toggle source
# File lib/sentry/configuration.rb, line 268
def sending_allowed?
  @errors = []

  valid? &&
    capture_in_environment? &&
    sample_allowed?
end
server=(value)
Alias for: dsn=
stacktrace_builder() click to toggle source
# File lib/sentry/configuration.rb, line 306
def stacktrace_builder
  @stacktrace_builder ||= StacktraceBuilder.new(
    project_root: @project_root.to_s,
    app_dirs_pattern: @app_dirs_pattern,
    linecache: @linecache,
    context_lines: @context_lines,
    backtrace_cleanup_callback: @backtrace_cleanup_callback
  )
end
tracing_enabled?() click to toggle source
# File lib/sentry/configuration.rb, line 302
def tracing_enabled?
  !!((@traces_sample_rate && @traces_sample_rate >= 0.0 && @traces_sample_rate <= 1.0) || @traces_sampler) && sending_allowed?
end

Private Instance Methods

capture_in_environment?() click to toggle source
# File lib/sentry/configuration.rb, line 398
def capture_in_environment?
  return true if enabled_in_current_env?

  @errors << "Not configured to send/capture in environment '#{environment}'"
  false
end
detect_release_from_capistrano() click to toggle source
# File lib/sentry/configuration.rb, line 379
def detect_release_from_capistrano
  revision_file = File.join(project_root, 'REVISION')
  revision_log = File.join(project_root, '..', 'revisions.log')

  if File.exist?(revision_file)
    File.read(revision_file).strip
  elsif File.exist?(revision_log)
    File.open(revision_log).to_a.last.strip.sub(/.*as release ([0-9]+).*/, '\1')
  end
end
detect_release_from_env() click to toggle source
# File lib/sentry/configuration.rb, line 394
def detect_release_from_env
  ENV['SENTRY_RELEASE']
end
detect_release_from_git() click to toggle source
# File lib/sentry/configuration.rb, line 390
def detect_release_from_git
  Sentry.sys_command("git rev-parse --short HEAD") if File.directory?(".git")
end
detect_release_from_heroku() click to toggle source
# File lib/sentry/configuration.rb, line 367
def detect_release_from_heroku
  return unless running_on_heroku?
  return if ENV['CI']
  log_warn(HEROKU_DYNO_METADATA_MESSAGE) && return unless ENV['HEROKU_SLUG_COMMIT']

  ENV['HEROKU_SLUG_COMMIT']
end
environment_from_env() click to toggle source
# File lib/sentry/configuration.rb, line 432
def environment_from_env
  ENV['SENTRY_CURRENT_ENV'] || ENV['SENTRY_ENVIRONMENT'] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
end
excluded_exception?(incoming_exception) click to toggle source
# File lib/sentry/configuration.rb, line 338
def excluded_exception?(incoming_exception)
  excluded_exception_classes.any? do |excluded_exception|
    matches_exception?(excluded_exception, incoming_exception)
  end
end
excluded_exception_classes() click to toggle source
# File lib/sentry/configuration.rb, line 344
def excluded_exception_classes
  @excluded_exception_classes ||= excluded_exceptions.map { |e| get_exception_class(e) }
end
get_exception_class(x) click to toggle source
# File lib/sentry/configuration.rb, line 348
def get_exception_class(x)
  x.is_a?(Module) ? x : safe_const_get(x)
end
matches_exception?(excluded_exception_class, incoming_exception) click to toggle source
# File lib/sentry/configuration.rb, line 352
def matches_exception?(excluded_exception_class, incoming_exception)
  if inspect_exception_causes_for_exclusion?
    Sentry::Utils::ExceptionCauseChain.exception_to_array(incoming_exception).any? { |cause| excluded_exception_class === cause }
  else
    excluded_exception_class === incoming_exception
  end
end
resolve_hostname() click to toggle source

Try to resolve the hostname to an FQDN, but fall back to whatever the load name is.

# File lib/sentry/configuration.rb, line 427
def resolve_hostname
  Socket.gethostname ||
    Socket.gethostbyname(hostname).first rescue server_name
end
run_post_initialization_callbacks() click to toggle source
# File lib/sentry/configuration.rb, line 444
def run_post_initialization_callbacks
  self.class.post_initialization_callbacks.each do |hook|
    instance_eval(&hook)
  end
end
running_on_heroku?() click to toggle source
# File lib/sentry/configuration.rb, line 375
def running_on_heroku?
  File.directory?("/etc/heroku")
end
safe_const_get(x) click to toggle source
# File lib/sentry/configuration.rb, line 360
def safe_const_get(x)
  x = x.to_s unless x.is_a?(String)
  Object.const_get(x)
rescue NameError # There's no way to safely ask if a constant exist for an unknown string
  nil
end
sample_allowed?() click to toggle source
# File lib/sentry/configuration.rb, line 414
def sample_allowed?
  return true if sample_rate == 1.0

  if Random.rand >= sample_rate
    @errors << "Excluded by random sample"
    false
  else
    true
  end
end
server_name_from_env() click to toggle source
# File lib/sentry/configuration.rb, line 436
def server_name_from_env
  if running_on_heroku?
    ENV['DYNO']
  else
    resolve_hostname
  end
end
valid?() click to toggle source
# File lib/sentry/configuration.rb, line 405
def valid?
  if @dsn&.valid?
    true
  else
    @errors << "DSN not set or not valid"
    false
  end
end