2018-10-22 07:00:50 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-25 03:06:16 +00:00
|
|
|
module Gitlab
|
2019-12-16 12:07:43 +00:00
|
|
|
module ErrorTracking
|
2020-04-15 00:09:27 +00:00
|
|
|
# Exceptions in this group will receive custom Sentry fingerprinting
|
|
|
|
CUSTOM_FINGERPRINTING = %w[
|
|
|
|
Acme::Client::Error::BadNonce
|
|
|
|
Acme::Client::Error::NotFound
|
|
|
|
Acme::Client::Error::RateLimited
|
|
|
|
Acme::Client::Error::Timeout
|
|
|
|
Acme::Client::Error::UnsupportedOperation
|
|
|
|
ActiveRecord::ConnectionTimeoutError
|
|
|
|
Gitlab::RequestContext::RequestDeadlineExceeded
|
|
|
|
GRPC::DeadlineExceeded
|
|
|
|
JIRA::HTTPError
|
|
|
|
Rack::Timeout::RequestTimeoutException
|
|
|
|
].freeze
|
|
|
|
|
2019-12-13 12:07:41 +00:00
|
|
|
class << self
|
|
|
|
def configure
|
|
|
|
Raven.configure do |config|
|
|
|
|
config.dsn = sentry_dsn
|
|
|
|
config.release = Gitlab.revision
|
|
|
|
config.current_environment = Gitlab.config.sentry.environment
|
|
|
|
|
|
|
|
# Sanitize fields based on those sanitized from Rails.
|
|
|
|
config.sanitize_fields = Rails.application.config.filter_parameters.map(&:to_s)
|
2020-06-12 12:08:56 +00:00
|
|
|
config.processors << ::Gitlab::ErrorTracking::Processor::SidekiqProcessor
|
2019-12-13 12:07:41 +00:00
|
|
|
# Sanitize authentication headers
|
|
|
|
config.sanitize_http_headers = %w[Authorization Private-Token]
|
|
|
|
config.tags = { program: Gitlab.process_name }
|
2020-04-15 00:09:27 +00:00
|
|
|
config.before_send = method(:before_send)
|
2020-06-12 12:08:56 +00:00
|
|
|
|
|
|
|
yield config if block_given?
|
2019-12-13 12:07:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_context(current_user = nil)
|
|
|
|
last_user_context = Raven.context.user
|
2016-08-25 03:06:16 +00:00
|
|
|
|
2019-12-13 12:07:41 +00:00
|
|
|
user_context = {
|
|
|
|
id: current_user&.id,
|
|
|
|
email: current_user&.email,
|
|
|
|
username: current_user&.username
|
|
|
|
}.compact
|
2016-08-25 03:06:16 +00:00
|
|
|
|
2019-12-13 12:07:41 +00:00
|
|
|
Raven.tags_context(default_tags)
|
|
|
|
Raven.user_context(user_context)
|
2017-07-20 06:53:57 +00:00
|
|
|
|
2019-12-13 12:07:41 +00:00
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
Raven.user_context(last_user_context)
|
2016-08-25 03:06:16 +00:00
|
|
|
end
|
|
|
|
|
2019-12-13 12:07:41 +00:00
|
|
|
# This should be used when you want to passthrough exception handling:
|
|
|
|
# rescue and raise to be catched in upper layers of the application.
|
|
|
|
#
|
|
|
|
# If the exception implements the method `sentry_extra_data` and that method
|
|
|
|
# returns a Hash, then the return value of that method will be merged into
|
|
|
|
# `extra`. Exceptions can use this mechanism to provide structured data
|
|
|
|
# to sentry in addition to their message and back-trace.
|
|
|
|
def track_and_raise_exception(exception, extra = {})
|
|
|
|
process_exception(exception, sentry: true, extra: extra)
|
2018-11-14 11:10:36 +00:00
|
|
|
|
2019-12-13 12:07:41 +00:00
|
|
|
raise exception
|
2018-04-13 16:32:54 +00:00
|
|
|
end
|
|
|
|
|
2019-12-13 12:07:41 +00:00
|
|
|
# This can be used for investigating exceptions that can be recovered from in
|
|
|
|
# code. The exception will still be raised in development and test
|
|
|
|
# environments.
|
|
|
|
#
|
|
|
|
# That way we can track down these exceptions with as much information as we
|
|
|
|
# need to resolve them.
|
|
|
|
#
|
|
|
|
# If the exception implements the method `sentry_extra_data` and that method
|
|
|
|
# returns a Hash, then the return value of that method will be merged into
|
|
|
|
# `extra`. Exceptions can use this mechanism to provide structured data
|
|
|
|
# to sentry in addition to their message and back-trace.
|
|
|
|
#
|
|
|
|
# Provide an issue URL for follow up.
|
|
|
|
# as `issue_url: 'http://gitlab.com/gitlab-org/gitlab/issues/111'`
|
|
|
|
def track_and_raise_for_dev_exception(exception, extra = {})
|
|
|
|
process_exception(exception, sentry: true, extra: extra)
|
2019-05-10 21:12:33 +00:00
|
|
|
|
2019-12-13 12:07:41 +00:00
|
|
|
raise exception if should_raise_for_dev?
|
|
|
|
end
|
2019-08-19 23:06:21 +00:00
|
|
|
|
2019-12-13 12:07:41 +00:00
|
|
|
# This should be used when you only want to track the exception.
|
|
|
|
#
|
|
|
|
# If the exception implements the method `sentry_extra_data` and that method
|
|
|
|
# returns a Hash, then the return value of that method will be merged into
|
|
|
|
# `extra`. Exceptions can use this mechanism to provide structured data
|
|
|
|
# to sentry in addition to their message and back-trace.
|
|
|
|
def track_exception(exception, extra = {})
|
|
|
|
process_exception(exception, sentry: true, extra: extra)
|
2019-08-19 23:06:21 +00:00
|
|
|
end
|
|
|
|
|
2019-12-13 12:07:41 +00:00
|
|
|
# This should be used when you only want to log the exception,
|
|
|
|
# but not send it to Sentry.
|
|
|
|
#
|
|
|
|
# If the exception implements the method `sentry_extra_data` and that method
|
|
|
|
# returns a Hash, then the return value of that method will be merged into
|
|
|
|
# `extra`. Exceptions can use this mechanism to provide structured data
|
|
|
|
# to sentry in addition to their message and back-trace.
|
|
|
|
def log_exception(exception, extra = {})
|
|
|
|
process_exception(exception, extra: extra)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-04-15 00:09:27 +00:00
|
|
|
def before_send(event, hint)
|
|
|
|
event = add_context_from_exception_type(event, hint)
|
|
|
|
event = custom_fingerprinting(event, hint)
|
|
|
|
|
|
|
|
event
|
|
|
|
end
|
|
|
|
|
2019-12-13 12:07:41 +00:00
|
|
|
def process_exception(exception, sentry: false, logging: true, extra:)
|
|
|
|
exception.try(:sentry_extra_data)&.tap do |data|
|
|
|
|
extra = extra.merge(data) if data.is_a?(Hash)
|
|
|
|
end
|
|
|
|
|
2020-02-07 15:09:52 +00:00
|
|
|
extra = sanitize_request_parameters(extra)
|
|
|
|
|
2019-12-13 12:07:41 +00:00
|
|
|
if sentry && Raven.configuration.server
|
|
|
|
Raven.capture_exception(exception, tags: default_tags, extra: extra)
|
|
|
|
end
|
2019-08-19 23:06:21 +00:00
|
|
|
|
2019-12-13 12:07:41 +00:00
|
|
|
if logging
|
|
|
|
# TODO: this logic could migrate into `Gitlab::ExceptionLogFormatter`
|
|
|
|
# and we could also flatten deep nested hashes if required for search
|
|
|
|
# (e.g. if `extra` includes hash of hashes).
|
|
|
|
# In the current implementation, we don't flatten multi-level folded hashes.
|
|
|
|
log_hash = {}
|
|
|
|
Raven.context.tags.each { |name, value| log_hash["tags.#{name}"] = value }
|
|
|
|
Raven.context.user.each { |name, value| log_hash["user.#{name}"] = value }
|
|
|
|
Raven.context.extra.merge(extra).each { |name, value| log_hash["extra.#{name}"] = value }
|
|
|
|
|
|
|
|
Gitlab::ExceptionLogFormatter.format!(exception, log_hash)
|
|
|
|
|
2019-12-16 12:07:43 +00:00
|
|
|
Gitlab::ErrorTracking::Logger.error(log_hash)
|
2019-12-13 12:07:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-07 15:09:52 +00:00
|
|
|
def sanitize_request_parameters(parameters)
|
|
|
|
filter = ActiveSupport::ParameterFilter.new(::Rails.application.config.filter_parameters)
|
|
|
|
filter.filter(parameters)
|
|
|
|
end
|
|
|
|
|
2019-12-13 12:07:41 +00:00
|
|
|
def sentry_dsn
|
|
|
|
return unless Rails.env.production? || Rails.env.development?
|
|
|
|
return unless Gitlab.config.sentry.enabled
|
|
|
|
|
|
|
|
Gitlab.config.sentry.dsn
|
|
|
|
end
|
|
|
|
|
|
|
|
def should_raise_for_dev?
|
|
|
|
Rails.env.development? || Rails.env.test?
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_tags
|
|
|
|
{
|
|
|
|
Labkit::Correlation::CorrelationId::LOG_KEY.to_sym => Labkit::Correlation::CorrelationId.current_id,
|
|
|
|
locale: I18n.locale
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-04-15 00:09:27 +00:00
|
|
|
# Debugging for https://gitlab.com/gitlab-org/gitlab-foss/issues/57727
|
2019-12-13 12:07:41 +00:00
|
|
|
def add_context_from_exception_type(event, hint)
|
|
|
|
if ActiveModel::MissingAttributeError === hint[:exception]
|
|
|
|
columns_hash = ActiveRecord::Base
|
|
|
|
.connection
|
|
|
|
.schema_cache
|
|
|
|
.instance_variable_get(:@columns_hash)
|
2020-06-22 00:08:57 +00:00
|
|
|
.transform_values { |v| v.map(&:first) }
|
2019-12-13 12:07:41 +00:00
|
|
|
|
|
|
|
event.extra.merge!(columns_hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
event
|
|
|
|
end
|
2020-04-15 00:09:27 +00:00
|
|
|
|
|
|
|
# Group common, mostly non-actionable exceptions by type and message,
|
|
|
|
# rather than cause
|
|
|
|
def custom_fingerprinting(event, hint)
|
|
|
|
ex = hint[:exception]
|
|
|
|
|
|
|
|
return event unless CUSTOM_FINGERPRINTING.include?(ex.class.name)
|
|
|
|
|
2020-04-16 00:09:22 +00:00
|
|
|
event.fingerprint = [ex.class.name, ex.message]
|
2020-04-15 00:09:27 +00:00
|
|
|
|
|
|
|
event
|
|
|
|
end
|
2019-12-13 12:07:41 +00:00
|
|
|
end
|
2016-08-25 03:06:16 +00:00
|
|
|
end
|
|
|
|
end
|