Commit f41c98dd authored by Gabriel Mazetto's avatar Gabriel Mazetto

Merge branch '341633-statistics-page-not-loading' into 'master'

Rescue from network errors when querying Prometheus in the background

See merge request gitlab-org/gitlab!73081
parents d6aeb84b 3a52a62d
......@@ -7,7 +7,7 @@ module Gitlab
def query(query)
client_query(query)
{ valid: true }
rescue Gitlab::PrometheusClient::QueryError, Gitlab::HTTP::BlockedUrlError => ex
rescue Gitlab::PrometheusClient::QueryError, Gitlab::PrometheusClient::ConnectionError => ex
{ valid: false, error: ex.message }
end
......
......@@ -151,12 +151,8 @@ module Gitlab
def get(path, args)
Gitlab::HTTP.get(path, { query: args }.merge(http_options) )
rescue SocketError
raise PrometheusClient::ConnectionError, "Can't connect to #{api_url}"
rescue OpenSSL::SSL::SSLError
raise PrometheusClient::ConnectionError, "#{api_url} contains invalid SSL data"
rescue Errno::ECONNREFUSED
raise PrometheusClient::ConnectionError, 'Connection refused'
rescue *Gitlab::HTTP::HTTP_ERRORS => e
raise PrometheusClient::ConnectionError, e.message
end
def handle_management_api_response(response)
......
......@@ -107,36 +107,14 @@ RSpec.describe Gitlab::PrometheusClient do
let(:prometheus_url) {"https://prometheus.invalid.example.com/api/v1/query?query=1"}
shared_examples 'exceptions are raised' do
it 'raises a Gitlab::PrometheusClient::ConnectionError error when a SocketError is rescued' do
req_stub = stub_prometheus_request_with_exception(prometheus_url, SocketError)
Gitlab::HTTP::HTTP_ERRORS.each do |error|
it "raises a Gitlab::PrometheusClient::ConnectionError when a #{error} is rescued" do
req_stub = stub_prometheus_request_with_exception(prometheus_url, error.new)
expect { subject }
.to raise_error(Gitlab::PrometheusClient::ConnectionError, "Can't connect to #{prometheus_url}")
.to raise_error(Gitlab::PrometheusClient::ConnectionError, kind_of(String))
expect(req_stub).to have_been_requested
end
it 'raises a Gitlab::PrometheusClient::ConnectionError error when a SSLError is rescued' do
req_stub = stub_prometheus_request_with_exception(prometheus_url, OpenSSL::SSL::SSLError)
expect { subject }
.to raise_error(Gitlab::PrometheusClient::ConnectionError, "#{prometheus_url} contains invalid SSL data")
expect(req_stub).to have_been_requested
end
it 'raises a Gitlab::PrometheusClient::ConnectionError error when a Gitlab::HTTP::ResponseError is rescued' do
req_stub = stub_prometheus_request_with_exception(prometheus_url, Gitlab::HTTP::ResponseError)
expect { subject }
.to raise_error(Gitlab::PrometheusClient::ConnectionError, "Network connection error")
expect(req_stub).to have_been_requested
end
it 'raises a Gitlab::PrometheusClient::ConnectionError error when a Gitlab::HTTP::ResponseError with a code is rescued' do
req_stub = stub_prometheus_request_with_exception(prometheus_url, Gitlab::HTTP::ResponseError.new(code: 400))
expect { subject }
.to raise_error(Gitlab::PrometheusClient::ConnectionError, "Network connection error")
expect(req_stub).to have_been_requested
end
end
......
......@@ -165,6 +165,14 @@ RSpec.describe PrometheusAdapter, :use_clean_rails_memory_store_caching do
it { is_expected.to eq(success: false, result: %(#{status} - "QUERY FAILED!")) }
end
end
context "when client raises Gitlab::PrometheusClient::ConnectionError" do
before do
stub_any_prometheus_request.to_raise(Gitlab::PrometheusClient::ConnectionError)
end
it { is_expected.to include(success: false, result: kind_of(String)) }
end
end
describe '#build_query_args' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment