summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorPawel Chojnacki <pawel@chojnacki.ws>2018-01-02 20:24:12 +0100
committerPawel Chojnacki <pawel@chojnacki.ws>2018-01-02 20:24:12 +0100
commitdb2433c36da6410c803163139e41228f9ae3f26b (patch)
treefb89c0c05d64ece22f647331c0d86769e5671945 /lib
parent91864a92b9821f43c4551f96c6af8dff49bbedaa (diff)
downloadgitlab-ce-db2433c36da6410c803163139e41228f9ae3f26b.tar.gz
wip
Diffstat (limited to 'lib')
-rw-r--r--lib/gitlab/prometheus/queries/additional_metrics_deployment_query.rb2
-rw-r--r--lib/gitlab/prometheus/queries/deployment_query.rb2
-rw-r--r--lib/gitlab/prometheus_client.rb36
3 files changed, 19 insertions, 21 deletions
diff --git a/lib/gitlab/prometheus/queries/additional_metrics_deployment_query.rb b/lib/gitlab/prometheus/queries/additional_metrics_deployment_query.rb
index 69d055c901c..294a6ae34ca 100644
--- a/lib/gitlab/prometheus/queries/additional_metrics_deployment_query.rb
+++ b/lib/gitlab/prometheus/queries/additional_metrics_deployment_query.rb
@@ -4,7 +4,7 @@ module Gitlab
class AdditionalMetricsDeploymentQuery < BaseQuery
include QueryAdditionalMetrics
- def query(deployment_id)
+ def query(environment_id, deployment_id)
Deployment.find_by(id: deployment_id).try do |deployment|
query_metrics(
common_query_context(
diff --git a/lib/gitlab/prometheus/queries/deployment_query.rb b/lib/gitlab/prometheus/queries/deployment_query.rb
index 170f483540e..6e6da593178 100644
--- a/lib/gitlab/prometheus/queries/deployment_query.rb
+++ b/lib/gitlab/prometheus/queries/deployment_query.rb
@@ -2,7 +2,7 @@ module Gitlab
module Prometheus
module Queries
class DeploymentQuery < BaseQuery
- def query(deployment_id)
+ def query(environment_id, deployment_id)
Deployment.find_by(id: deployment_id).try do |deployment|
environment_slug = deployment.environment.slug
diff --git a/lib/gitlab/prometheus_client.rb b/lib/gitlab/prometheus_client.rb
index aa94614bf18..8ec4515fb12 100644
--- a/lib/gitlab/prometheus_client.rb
+++ b/lib/gitlab/prometheus_client.rb
@@ -3,10 +3,12 @@ module Gitlab
# Helper methods to interact with Prometheus network services & resources
class PrometheusClient
- attr_reader :api_url
+ attr_reader :api_url, :rest_client, :headers
- def initialize(api_url:)
+ def initialize(api_url:, rest_client: nil, headers: nil)
@api_url = api_url
+ @rest_client = rest_client || RestClient::Resource.new(api_url)
+ @headers = headers || {}
end
def ping
@@ -40,24 +42,15 @@ module Gitlab
private
def json_api_get(type, args = {})
- get(join_api_url(type, args))
+ path = ['api', 'v1', type].join('/')
+ get(path, args)
rescue Errno::ECONNREFUSED
raise PrometheusError, 'Connection refused'
end
- def join_api_url(type, args = {})
- url = URI.parse(api_url)
- rescue URI::Error
- raise PrometheusError, "Invalid API URL: #{api_url}"
- else
- url.path = [url.path.sub(%r{/+\z}, ''), 'api', 'v1', type].join('/')
- url.query = args.to_query
-
- url.to_s
- end
-
- def get(url)
- handle_response(HTTParty.get(url))
+ def get(path, args)
+ response = rest_client[path].get(headers.merge(params: args))
+ handle_response(response)
rescue SocketError
raise PrometheusError, "Can't connect to #{url}"
rescue OpenSSL::SSL::SSLError
@@ -67,15 +60,20 @@ module Gitlab
end
def handle_response(response)
- if response.code == 200 && response['status'] == 'success'
- response['data'] || {}
+ json_data = json_response(response)
+ if response.code == 200 && json_data['status'] == 'success'
+ json_data['data'] || {}
elsif response.code == 400
- raise PrometheusError, response['error'] || 'Bad data received'
+ raise PrometheusError, json_data['error'] || 'Bad data received'
else
raise PrometheusError, "#{response.code} - #{response.body}"
end
end
+ def json_response(response)
+ JSON.parse(response.body)
+ end
+
def get_result(expected_type)
data = yield
data['result'] if data['resultType'] == expected_type