debian-mirror-gitlab/lib/error_tracking/sentry_client.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

126 lines
3.6 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2021-04-17 20:07:23 +05:30
module ErrorTracking
class SentryClient
include SentryClient::Event
include SentryClient::Projects
include SentryClient::Issue
include SentryClient::Repo
include SentryClient::IssueLink
2020-01-01 13:55:28 +05:30
2019-02-15 15:39:39 +05:30
Error = Class.new(StandardError)
2019-07-07 11:18:12 +05:30
MissingKeysError = Class.new(StandardError)
2022-10-02 17:18:49 +05:30
InvalidFieldValueError = Class.new(StandardError)
2022-10-11 01:57:18 +05:30
ResponseInvalidSizeError = Class.new(StandardError)
RESPONSE_SIZE_LIMIT = 1.megabyte
2019-02-15 15:39:39 +05:30
attr_accessor :url, :token
2022-10-11 01:57:18 +05:30
def initialize(api_url, token, validate_size_guarded_by_feature_flag: false)
2019-02-15 15:39:39 +05:30
@url = api_url
@token = token
2022-10-11 01:57:18 +05:30
@validate_size_guarded_by_feature_flag = validate_size_guarded_by_feature_flag
end
def validate_size_guarded_by_feature_flag?
@validate_size_guarded_by_feature_flag
2019-02-15 15:39:39 +05:30
end
private
2022-10-11 01:57:18 +05:30
def validate_size(response)
return if Gitlab::Utils::DeepSize.new(response, max_size: RESPONSE_SIZE_LIMIT).valid?
limit = ActiveSupport::NumberHelper.number_to_human_size(RESPONSE_SIZE_LIMIT)
message = "Sentry API response is too big. Limit is #{limit}."
raise ResponseInvalidSizeError, message
end
2020-03-13 15:44:24 +05:30
def api_urls
2021-04-17 20:07:23 +05:30
@api_urls ||= SentryClient::ApiUrls.new(@url)
2019-12-26 22:10:19 +05:30
end
2019-07-07 11:18:12 +05:30
def handle_mapping_exceptions(&block)
yield
rescue KeyError => e
2020-01-01 13:55:28 +05:30
Gitlab::ErrorTracking.track_exception(e)
raise MissingKeysError, "Sentry API response is missing keys. #{e.message}"
2019-07-07 11:18:12 +05:30
end
2019-02-15 15:39:39 +05:30
def request_params
{
headers: {
2020-03-13 15:44:24 +05:30
'Content-Type' => 'application/json',
2019-02-15 15:39:39 +05:30
'Authorization' => "Bearer #{@token}"
},
follow_redirects: false
}
end
2019-03-02 22:35:43 +05:30
def http_get(url, params = {})
2020-03-13 15:44:24 +05:30
http_request do
2019-07-07 11:18:12 +05:30
Gitlab::HTTP.get(url, **request_params.merge(params))
end
2019-02-15 15:39:39 +05:30
end
2020-03-13 15:44:24 +05:30
def http_put(url, params = {})
http_request do
Gitlab::HTTP.put(url, **request_params.merge(body: params.to_json))
end
2019-03-02 22:35:43 +05:30
end
2020-03-13 15:44:24 +05:30
def http_post(url, params = {})
http_request do
Gitlab::HTTP.post(url, **request_params.merge(body: params.to_json))
2020-01-01 13:55:28 +05:30
end
2019-12-26 22:10:19 +05:30
end
2022-06-21 17:19:12 +05:30
def http_request(&block)
response = handle_request_exceptions(&block)
2019-12-26 22:10:19 +05:30
2020-03-13 15:44:24 +05:30
handle_response(response)
2019-03-02 22:35:43 +05:30
end
2019-07-07 11:18:12 +05:30
def handle_request_exceptions
yield
2019-10-12 21:52:04 +05:30
rescue Gitlab::HTTP::Error => e
2020-01-01 13:55:28 +05:30
Gitlab::ErrorTracking.track_exception(e)
2019-07-07 11:18:12 +05:30
raise_error 'Error when connecting to Sentry'
rescue Net::OpenTimeout
raise_error 'Connection to Sentry timed out'
rescue SocketError
raise_error 'Received SocketError when trying to connect to Sentry'
rescue OpenSSL::SSL::SSLError
raise_error 'Sentry returned invalid SSL data'
rescue Errno::ECONNREFUSED
raise_error 'Connection refused'
2021-06-08 01:23:25 +05:30
rescue StandardError => e
2020-01-01 13:55:28 +05:30
Gitlab::ErrorTracking.track_exception(e)
2019-07-07 11:18:12 +05:30
raise_error "Sentry request failed due to #{e.class}"
end
2019-02-15 15:39:39 +05:30
def handle_response(response)
2022-06-21 17:19:12 +05:30
raise_error "Sentry response status code: #{response.code}" unless response.code.between?(200, 204)
2019-02-15 15:39:39 +05:30
2022-10-11 01:57:18 +05:30
validate_size(response.parsed_response) if validate_size_guarded_by_feature_flag?
2020-01-01 13:55:28 +05:30
{ body: response.parsed_response, headers: response.headers }
2019-07-07 11:18:12 +05:30
end
def raise_error(message)
2021-04-17 20:07:23 +05:30
raise SentryClient::Error, message
2019-02-15 15:39:39 +05:30
end
2022-10-02 17:18:49 +05:30
def ensure_numeric!(field, value)
return value if /\A\d+\z/.match?(value)
raise_invalid_field_value!(field, "#{value.inspect} is not numeric")
end
def raise_invalid_field_value!(field, message)
raise InvalidFieldValueError, %(Sentry API response contains invalid value for field "#{field}": #{message})
end
2019-02-15 15:39:39 +05:30
end
end