2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-24 23:06:16 -04:00
|
|
|
module Gitlab
|
|
|
|
module Sentry
|
|
|
|
def self.enabled?
|
2018-12-05 15:54:40 -05:00
|
|
|
(Rails.env.production? || Rails.env.development?) &&
|
2019-04-30 16:06:08 -04:00
|
|
|
Gitlab.config.sentry.enabled
|
2016-08-24 23:06:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.context(current_user = nil)
|
2018-11-14 06:10:36 -05:00
|
|
|
return unless enabled?
|
2016-08-24 23:06:16 -04:00
|
|
|
|
2019-05-10 17:12:33 -04:00
|
|
|
Raven.tags_context(default_tags)
|
2017-07-20 02:53:57 -04:00
|
|
|
|
2016-08-24 23:06:16 -04:00
|
|
|
if current_user
|
|
|
|
Raven.user_context(
|
|
|
|
id: current_user.id,
|
|
|
|
email: current_user.email,
|
2017-05-03 07:27:17 -04:00
|
|
|
username: current_user.username
|
2016-08-24 23:06:16 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-13 12:32:54 -04: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.
|
|
|
|
#
|
|
|
|
# Provide an issue URL for follow up.
|
|
|
|
def self.track_exception(exception, issue_url: nil, extra: {})
|
2018-11-14 06:10:36 -05:00
|
|
|
track_acceptable_exception(exception, issue_url: issue_url, extra: extra)
|
|
|
|
|
2018-12-05 15:54:40 -05:00
|
|
|
raise exception if should_raise_for_dev?
|
2018-11-14 06:10:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# This should be used when you do not want to raise an exception in
|
|
|
|
# development and test. If you need development and test to behave
|
|
|
|
# just the same as production you can use this instead of
|
|
|
|
# track_exception.
|
|
|
|
def self.track_acceptable_exception(exception, issue_url: nil, extra: {})
|
2018-04-13 12:32:54 -04:00
|
|
|
if enabled?
|
|
|
|
extra[:issue_url] = issue_url if issue_url
|
|
|
|
context # Make sure we've set everything we know in the context
|
|
|
|
|
2019-05-10 17:12:33 -04:00
|
|
|
Raven.capture_exception(exception, tags: default_tags, extra: extra)
|
2018-04-13 12:32:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-05 15:54:40 -05:00
|
|
|
def self.should_raise_for_dev?
|
2018-04-13 12:32:54 -04:00
|
|
|
Rails.env.development? || Rails.env.test?
|
|
|
|
end
|
2019-05-10 17:12:33 -04:00
|
|
|
|
|
|
|
def self.default_tags
|
|
|
|
{
|
|
|
|
Labkit::Correlation::CorrelationId::LOG_KEY.to_sym => Labkit::Correlation::CorrelationId.current_id,
|
|
|
|
locale: I18n.locale
|
|
|
|
}
|
|
|
|
end
|
2016-08-24 23:06:16 -04:00
|
|
|
end
|
|
|
|
end
|