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