2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-07 11:57:42 -05:00
|
|
|
module Gitlab
|
|
|
|
# Helper methods to interact with Prometheus network services & resources
|
2017-04-26 16:09:03 -04:00
|
|
|
class PrometheusClient
|
2018-02-23 12:58:40 -05:00
|
|
|
Error = Class.new(StandardError)
|
|
|
|
QueryError = Class.new(Gitlab::PrometheusClient::Error)
|
|
|
|
|
2019-04-04 11:38:37 -04:00
|
|
|
# Target number of data points for `query_range`.
|
|
|
|
# Please don't exceed the limit of 11000 data points
|
|
|
|
# See https://github.com/prometheus/prometheus/blob/91306bdf24f5395e2601773316945a478b4b263d/web/api/v1/api.go#L347
|
|
|
|
QUERY_RANGE_DATA_POINTS = 600
|
|
|
|
|
|
|
|
# Minimal value of the `step` parameter for `query_range` in seconds.
|
|
|
|
QUERY_RANGE_MIN_STEP = 60
|
|
|
|
|
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
|
|
|
|
|
2019-04-05 04:05:54 -04:00
|
|
|
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
|
|
|
|
|
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)
|
2019-04-04 11:38:37 -04:00
|
|
|
start = start.to_f
|
|
|
|
stop = stop.to_f
|
|
|
|
step = self.class.compute_step(start, stop)
|
|
|
|
|
2017-03-07 11:57:42 -05:00
|
|
|
get_result('matrix') do
|
2019-04-04 11:38:37 -04:00
|
|
|
json_api_get(
|
|
|
|
'query_range',
|
|
|
|
query: query,
|
|
|
|
start: start,
|
|
|
|
end: stop,
|
|
|
|
step: step
|
|
|
|
)
|
2017-03-07 11:57:42 -05:00
|
|
|
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
|
|
|
|
|
2019-04-04 11:38:37 -04:00
|
|
|
def self.compute_step(start, stop)
|
|
|
|
diff = stop - start
|
|
|
|
|
|
|
|
step = (diff / QUERY_RANGE_DATA_POINTS).ceil
|
|
|
|
|
|
|
|
[QUERY_RANGE_MIN_STEP, step].max
|
|
|
|
end
|
|
|
|
|
2017-03-07 11:57:42 -05:00
|
|
|
private
|
|
|
|
|
2019-04-05 04:05:54 -04:00
|
|
|
def api_path(type)
|
|
|
|
['api', 'v1', type].join('/')
|
2017-03-07 11:57:42 -05:00
|
|
|
end
|
|
|
|
|
2019-04-05 04:05:54 -04:00
|
|
|
def json_api_get(type, args = {})
|
|
|
|
path = api_path(type)
|
|
|
|
response = get(path, args)
|
2018-01-02 14:24:12 -05:00
|
|
|
handle_response(response)
|
2018-01-03 21:13:54 -05:00
|
|
|
rescue RestClient::ExceptionWithResponse => ex
|
2018-02-23 15:33:33 -05:00
|
|
|
if ex.response
|
|
|
|
handle_exception_response(ex.response)
|
|
|
|
else
|
|
|
|
raise PrometheusClient::Error, "Network connection error"
|
|
|
|
end
|
2018-01-03 21:13:54 -05:00
|
|
|
rescue RestClient::Exception
|
2018-02-23 12:58:40 -05:00
|
|
|
raise PrometheusClient::Error, "Network connection error"
|
2017-03-07 11:57:42 -05:00
|
|
|
end
|
|
|
|
|
2019-04-05 04:05:54 -04:00
|
|
|
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
|
|
|
|
|
2017-03-07 11:57:42 -05:00
|
|
|
def handle_response(response)
|
2019-04-05 04:05:54 -04:00
|
|
|
json_data = parse_json(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
|
2018-02-23 12:58:40 -05:00
|
|
|
raise PrometheusClient::Error, "#{response.code} - #{response.body}"
|
2018-01-03 21:13:54 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_exception_response(response)
|
2018-02-23 12:58:40 -05:00
|
|
|
if response.code == 200 && response['status'] == 'success'
|
|
|
|
response['data'] || {}
|
|
|
|
elsif response.code == 400
|
2019-04-05 04:05:54 -04:00
|
|
|
json_data = parse_json(response.body)
|
2018-02-23 12:58:40 -05:00
|
|
|
raise PrometheusClient::QueryError, json_data['error'] || 'Bad data received'
|
2017-03-07 11:57:42 -05:00
|
|
|
else
|
2018-02-23 12:58:40 -05:00
|
|
|
raise PrometheusClient::Error, "#{response.code} - #{response.body}"
|
2017-03-07 11:57:42 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_result(expected_type)
|
|
|
|
data = yield
|
|
|
|
data['result'] if data['resultType'] == expected_type
|
|
|
|
end
|
2019-04-05 04:05:54 -04:00
|
|
|
|
|
|
|
def parse_json(response_body)
|
|
|
|
JSON.parse(response_body)
|
|
|
|
rescue JSON::ParserError
|
|
|
|
raise PrometheusClient::Error, 'Parsing response failed'
|
|
|
|
end
|
2017-03-07 11:57:42 -05:00
|
|
|
end
|
|
|
|
end
|