diff options
author | Reuben Pereira <rpereira@gitlab.com> | 2019-04-05 11:05:54 +0300 |
---|---|---|
committer | Sean McGivern <sean@gitlab.com> | 2019-04-05 11:05:54 +0300 |
commit | 7f529353e75852e62203eee3b8106b21dca068d9 (patch) | |
tree | d009d7fbd7c8cb6740cf6cdae30407f8b8582aa7 /lib | |
parent | 88bf73a719933c136cc2bd9b791b6d5ca85cd330 (diff) |
Add a proxy method to PrometheusClient
- Also refactor the get and json_api_get methods so that the get method
can be reused by the new proxy method.
- The new proxy method makes no changes to the request to the prometheus
server and response from the prometheus server. This allows it to be
used as a proxy to the Prometheus server, hence the name.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/prometheus_client.rb | 51 |
1 files changed, 36 insertions, 15 deletions
diff --git a/lib/gitlab/prometheus_client.rb b/lib/gitlab/prometheus_client.rb index b4de7cd2bce..f13156f898e 100644 --- a/lib/gitlab/prometheus_client.rb +++ b/lib/gitlab/prometheus_client.rb @@ -24,6 +24,19 @@ module Gitlab json_api_get('query', query: '1') end + def proxy(type, args) + path = api_path(type) + get(path, args) + rescue RestClient::ExceptionWithResponse => ex + if ex.response + ex.response + else + raise PrometheusClient::Error, "Network connection error" + end + rescue RestClient::Exception + raise PrometheusClient::Error, "Network connection error" + end + def query(query, time: Time.now) get_result('vector') do json_api_get('query', query: query, time: time.to_f) @@ -64,22 +77,14 @@ module Gitlab private - def json_api_get(type, args = {}) - path = ['api', 'v1', type].join('/') - get(path, args) - rescue JSON::ParserError - raise PrometheusClient::Error, 'Parsing response failed' - rescue Errno::ECONNREFUSED - raise PrometheusClient::Error, 'Connection refused' + def api_path(type) + ['api', 'v1', type].join('/') end - def get(path, args) - response = rest_client[path].get(params: args) + def json_api_get(type, args = {}) + path = api_path(type) + response = get(path, args) handle_response(response) - rescue SocketError - raise PrometheusClient::Error, "Can't connect to #{rest_client.url}" - rescue OpenSSL::SSL::SSLError - raise PrometheusClient::Error, "#{rest_client.url} contains invalid SSL data" rescue RestClient::ExceptionWithResponse => ex if ex.response handle_exception_response(ex.response) @@ -90,8 +95,18 @@ module Gitlab raise PrometheusClient::Error, "Network connection error" end + def get(path, args) + rest_client[path].get(params: args) + rescue SocketError + raise PrometheusClient::Error, "Can't connect to #{rest_client.url}" + rescue OpenSSL::SSL::SSLError + raise PrometheusClient::Error, "#{rest_client.url} contains invalid SSL data" + rescue Errno::ECONNREFUSED + raise PrometheusClient::Error, 'Connection refused' + end + def handle_response(response) - json_data = JSON.parse(response.body) + json_data = parse_json(response.body) if response.code == 200 && json_data['status'] == 'success' json_data['data'] || {} else @@ -103,7 +118,7 @@ module Gitlab if response.code == 200 && response['status'] == 'success' response['data'] || {} elsif response.code == 400 - json_data = JSON.parse(response.body) + json_data = parse_json(response.body) raise PrometheusClient::QueryError, json_data['error'] || 'Bad data received' else raise PrometheusClient::Error, "#{response.code} - #{response.body}" @@ -114,5 +129,11 @@ module Gitlab data = yield data['result'] if data['resultType'] == expected_type end + + def parse_json(response_body) + JSON.parse(response_body) + rescue JSON::ParserError + raise PrometheusClient::Error, 'Parsing response failed' + end end end |