gitlab-org--gitlab-foss/lib/gitlab/prometheus_client.rb

91 lines
2.7 KiB
Ruby
Raw Normal View History

module Gitlab
# Helper methods to interact with Prometheus network services & resources
class PrometheusClient
2018-02-23 17:58:40 +00:00
Error = Class.new(StandardError)
QueryError = Class.new(Gitlab::PrometheusClient::Error)
attr_reader :rest_client, :headers
def initialize(rest_client)
@rest_client = rest_client
end
def ping
json_api_get('query', query: '1')
end
2017-05-09 04:15:34 +00:00
def query(query, time: Time.now)
get_result('vector') do
json_api_get('query', query: query, time: time.to_f)
end
end
2017-05-09 04:15:34 +00:00
def query_range(query, start: 8.hours.ago, stop: Time.now)
get_result('matrix') do
json_api_get('query_range',
2018-02-23 17:58:40 +00:00
query: query,
start: start.to_f,
end: stop.to_f,
step: 1.minute.to_i)
end
end
2017-06-05 19:00:57 +00:00
def label_values(name = '__name__')
json_api_get("label/#{name}/values")
end
def series(*matches, start: 8.hours.ago, stop: Time.now)
json_api_get('series', 'match': matches, start: start.to_f, end: stop.to_f)
end
private
def json_api_get(type, args = {})
2018-01-02 19:24:12 +00:00
path = ['api', 'v1', type].join('/')
get(path, args)
rescue JSON::ParserError
2018-02-23 17:58:40 +00:00
raise PrometheusClient::Error, 'Parsing response failed'
rescue Errno::ECONNREFUSED
2018-02-23 17:58:40 +00:00
raise PrometheusClient::Error, 'Connection refused'
end
2018-01-02 19:24:12 +00:00
def get(path, args)
response = rest_client[path].get(params: args)
2018-01-02 19:24:12 +00:00
handle_response(response)
2017-05-02 16:16:59 +00:00
rescue SocketError
2018-02-23 17:58:40 +00:00
raise PrometheusClient::Error, "Can't connect to #{rest_client.url}"
2017-05-02 16:16:59 +00:00
rescue OpenSSL::SSL::SSLError
2018-02-23 17:58:40 +00:00
raise PrometheusClient::Error, "#{rest_client.url} contains invalid SSL data"
2018-01-04 02:13:54 +00:00
rescue RestClient::ExceptionWithResponse => ex
handle_exception_response(ex.response)
rescue RestClient::Exception
2018-02-23 17:58:40 +00:00
raise PrometheusClient::Error, "Network connection error"
end
def handle_response(response)
json_data = JSON.parse(response.body)
2018-01-02 19:24:12 +00:00
if response.code == 200 && json_data['status'] == 'success'
json_data['data'] || {}
2018-01-04 02:13:54 +00:00
else
2018-02-23 17:58:40 +00:00
raise PrometheusClient::Error, "#{response.code} - #{response.body}"
2018-01-04 02:13:54 +00:00
end
end
def handle_exception_response(response)
2018-02-23 17:58:40 +00:00
if response.code == 200 && response['status'] == 'success'
response['data'] || {}
elsif response.code == 400
2018-01-04 02:13:54 +00:00
json_data = JSON.parse(response.body)
2018-02-23 17:58:40 +00:00
raise PrometheusClient::QueryError, json_data['error'] || 'Bad data received'
else
2018-02-23 17:58:40 +00:00
raise PrometheusClient::Error, "#{response.code} - #{response.body}"
end
end
def get_result(expected_type)
data = yield
data['result'] if data['resultType'] == expected_type
end
end
end