2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
module Gitlab
|
|
|
|
module Sentry
|
|
|
|
def self.enabled?
|
2019-02-15 15:39:39 +05:30
|
|
|
(Rails.env.production? || Rails.env.development?) &&
|
|
|
|
Gitlab::CurrentSettings.sentry_enabled?
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def self.context(current_user = nil)
|
2019-02-15 15:39:39 +05:30
|
|
|
return unless enabled?
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
Raven.tags_context(locale: I18n.locale)
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
if current_user
|
|
|
|
Raven.user_context(
|
|
|
|
id: current_user.id,
|
|
|
|
email: current_user.email,
|
2017-09-10 17:25:29 +05:30
|
|
|
username: current_user.username
|
2016-09-29 09:46:39 +05:30
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
# 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: {})
|
2019-02-15 15:39:39 +05:30
|
|
|
track_acceptable_exception(exception, issue_url: issue_url, extra: extra)
|
|
|
|
|
|
|
|
raise exception if should_raise_for_dev?
|
|
|
|
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-10-15 14:42:47 +05:30
|
|
|
if enabled?
|
|
|
|
extra[:issue_url] = issue_url if issue_url
|
|
|
|
context # Make sure we've set everything we know in the context
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
tags = {
|
|
|
|
Gitlab::CorrelationId::LOG_KEY.to_sym => Gitlab::CorrelationId.current_id
|
|
|
|
}
|
2019-01-03 12:48:30 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
Raven.capture_exception(exception, tags: tags, extra: extra)
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
def self.should_raise_for_dev?
|
2018-10-15 14:42:47 +05:30
|
|
|
Rails.env.development? || Rails.env.test?
|
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
end
|