2019-08-22 10:57:44 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-07 16:57:42 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
RSpec.describe Gitlab::PrometheusClient do
|
2017-03-07 16:57:42 +00:00
|
|
|
include PrometheusHelpers
|
|
|
|
|
2019-08-07 02:42:20 +00:00
|
|
|
subject { described_class.new('https://prometheus.example.com') }
|
2017-03-07 16:57:42 +00:00
|
|
|
|
|
|
|
describe '#ping' do
|
|
|
|
it 'issues a "query" request to the API endpoint' do
|
|
|
|
req_stub = stub_prometheus_request(prometheus_query_url('1'), body: prometheus_value_body('vector'))
|
|
|
|
|
|
|
|
expect(subject.ping).to eq({ "resultType" => "vector", "result" => [{ "metric" => {}, "value" => [1488772511.004, "0.000041021495238095323"] }] })
|
|
|
|
expect(req_stub).to have_been_requested
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-26 00:07:58 +00:00
|
|
|
describe '#healthy?' do
|
|
|
|
it 'returns true when status code is 200 and healthy response body' do
|
|
|
|
stub_request(:get, subject.health_url).to_return(status: 200, body: described_class::HEALTHY_RESPONSE)
|
|
|
|
|
|
|
|
expect(subject.healthy?).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when status code is 200 and unhealthy response body' do
|
|
|
|
stub_request(:get, subject.health_url).to_return(status: 200, body: '')
|
|
|
|
|
|
|
|
expect(subject.healthy?).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises error when status code not 200' do
|
|
|
|
stub_request(:get, subject.health_url).to_return(status: 500, body: '')
|
|
|
|
|
2020-07-03 15:09:13 +00:00
|
|
|
expect { subject.healthy? }.to raise_error(Gitlab::PrometheusClient::UnexpectedResponseError)
|
2020-03-26 00:07:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-16 18:09:47 +00:00
|
|
|
describe '#ready?' do
|
|
|
|
it 'returns true when status code is 200' do
|
|
|
|
stub_request(:get, subject.ready_url).to_return(status: 200, body: 'Prometheus is Ready.\n')
|
|
|
|
|
|
|
|
expect(subject.ready?).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when status code is not 200' do
|
|
|
|
[503, 500].each do |code|
|
|
|
|
stub_request(:get, subject.ready_url).to_return(status: code, body: 'Service Unavailable')
|
|
|
|
|
|
|
|
expect(subject.ready?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises error when ready api throws exception' do
|
|
|
|
stub_request(:get, subject.ready_url).to_raise(Net::OpenTimeout)
|
|
|
|
|
|
|
|
expect { subject.ready? }.to raise_error(Gitlab::PrometheusClient::UnexpectedResponseError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-07 16:57:42 +00:00
|
|
|
# This shared examples expect:
|
|
|
|
# - query_url: A query URL
|
|
|
|
# - execute_query: A query call
|
|
|
|
shared_examples 'failure response' do
|
|
|
|
context 'when request returns 400 with an error message' do
|
2020-07-03 15:09:13 +00:00
|
|
|
it 'raises a Gitlab::PrometheusClient::QueryError error' do
|
2017-03-07 16:57:42 +00:00
|
|
|
req_stub = stub_prometheus_request(query_url, status: 400, body: { error: 'bar!' })
|
|
|
|
|
|
|
|
expect { execute_query }
|
2020-07-03 15:09:13 +00:00
|
|
|
.to raise_error(Gitlab::PrometheusClient::QueryError, 'bar!')
|
2017-03-07 16:57:42 +00:00
|
|
|
expect(req_stub).to have_been_requested
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when request returns 400 without an error message' do
|
2020-07-03 15:09:13 +00:00
|
|
|
it 'raises a Gitlab::PrometheusClient::QueryError error' do
|
2017-03-07 16:57:42 +00:00
|
|
|
req_stub = stub_prometheus_request(query_url, status: 400)
|
|
|
|
|
|
|
|
expect { execute_query }
|
2020-07-03 15:09:13 +00:00
|
|
|
.to raise_error(Gitlab::PrometheusClient::QueryError, 'Bad data received')
|
2017-03-07 16:57:42 +00:00
|
|
|
expect(req_stub).to have_been_requested
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when request returns 500' do
|
2020-07-03 15:09:13 +00:00
|
|
|
it 'raises a Gitlab::PrometheusClient::UnexpectedResponseError error' do
|
2017-03-07 16:57:42 +00:00
|
|
|
req_stub = stub_prometheus_request(query_url, status: 500, body: { message: 'FAIL!' })
|
|
|
|
|
|
|
|
expect { execute_query }
|
2020-07-03 15:09:13 +00:00
|
|
|
.to raise_error(Gitlab::PrometheusClient::UnexpectedResponseError, '500 - {"message":"FAIL!"}')
|
2017-03-07 16:57:42 +00:00
|
|
|
expect(req_stub).to have_been_requested
|
|
|
|
end
|
|
|
|
end
|
2018-02-07 01:25:54 +00:00
|
|
|
|
|
|
|
context 'when request returns non json data' do
|
2020-07-03 15:09:13 +00:00
|
|
|
it 'raises a Gitlab::PrometheusClient::UnexpectedResponseError error' do
|
2018-02-07 01:25:54 +00:00
|
|
|
req_stub = stub_prometheus_request(query_url, status: 200, body: 'not json')
|
|
|
|
|
|
|
|
expect { execute_query }
|
2020-07-03 15:09:13 +00:00
|
|
|
.to raise_error(Gitlab::PrometheusClient::UnexpectedResponseError, 'Parsing response failed')
|
2018-02-07 01:25:54 +00:00
|
|
|
expect(req_stub).to have_been_requested
|
|
|
|
end
|
|
|
|
end
|
2017-03-07 16:57:42 +00:00
|
|
|
end
|
|
|
|
|
2017-05-02 21:53:49 +00:00
|
|
|
describe 'failure to reach a provided prometheus url' do
|
2022-08-02 03:09:33 +00:00
|
|
|
let(:prometheus_url) { "https://prometheus.invalid.example.com/api/v1/query?query=1" }
|
2017-05-02 16:16:59 +00:00
|
|
|
|
2019-04-05 08:05:54 +00:00
|
|
|
shared_examples 'exceptions are raised' do
|
2021-11-09 15:12:42 +00:00
|
|
|
Gitlab::HTTP::HTTP_ERRORS.each do |error|
|
|
|
|
it "raises a Gitlab::PrometheusClient::ConnectionError when a #{error} is rescued" do
|
|
|
|
req_stub = stub_prometheus_request_with_exception(prometheus_url, error.new)
|
|
|
|
|
|
|
|
expect { subject }
|
|
|
|
.to raise_error(Gitlab::PrometheusClient::ConnectionError, kind_of(String))
|
|
|
|
expect(req_stub).to have_been_requested
|
|
|
|
end
|
2017-05-02 21:53:49 +00:00
|
|
|
end
|
2017-05-02 16:16:59 +00:00
|
|
|
end
|
2019-04-05 08:05:54 +00:00
|
|
|
|
|
|
|
context 'ping' do
|
2019-08-07 02:42:20 +00:00
|
|
|
subject { described_class.new(prometheus_url).ping }
|
2019-04-05 08:05:54 +00:00
|
|
|
|
|
|
|
it_behaves_like 'exceptions are raised'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'proxy' do
|
2019-08-07 02:42:20 +00:00
|
|
|
subject { described_class.new(prometheus_url).proxy('query', { query: '1' }) }
|
2019-04-05 08:05:54 +00:00
|
|
|
|
|
|
|
it_behaves_like 'exceptions are raised'
|
|
|
|
end
|
2017-05-02 16:16:59 +00:00
|
|
|
end
|
|
|
|
|
2017-03-07 16:57:42 +00:00
|
|
|
describe '#query' do
|
|
|
|
let(:prometheus_query) { prometheus_cpu_query('env-slug') }
|
2017-05-09 04:15:34 +00:00
|
|
|
let(:query_url) { prometheus_query_with_time_url(prometheus_query, Time.now.utc) }
|
|
|
|
|
|
|
|
around do |example|
|
2020-08-31 15:10:41 +00:00
|
|
|
freeze_time { example.run }
|
2017-05-09 04:15:34 +00:00
|
|
|
end
|
2017-03-07 16:57:42 +00:00
|
|
|
|
|
|
|
context 'when request returns vector results' do
|
|
|
|
it 'returns data from the API call' do
|
|
|
|
req_stub = stub_prometheus_request(query_url, body: prometheus_value_body('vector'))
|
|
|
|
|
|
|
|
expect(subject.query(prometheus_query)).to eq [{ "metric" => {}, "value" => [1488772511.004, "0.000041021495238095323"] }]
|
|
|
|
expect(req_stub).to have_been_requested
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when request returns matrix results' do
|
|
|
|
it 'returns nil' do
|
|
|
|
req_stub = stub_prometheus_request(query_url, body: prometheus_value_body('matrix'))
|
|
|
|
|
|
|
|
expect(subject.query(prometheus_query)).to be_nil
|
|
|
|
expect(req_stub).to have_been_requested
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when request returns no data' do
|
|
|
|
it 'returns []' do
|
|
|
|
req_stub = stub_prometheus_request(query_url, body: prometheus_empty_body('vector'))
|
|
|
|
|
|
|
|
expect(subject.query(prometheus_query)).to be_empty
|
|
|
|
expect(req_stub).to have_been_requested
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'failure response' do
|
|
|
|
let(:execute_query) { subject.query(prometheus_query) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-03 12:08:21 +00:00
|
|
|
describe '#aggregate' do
|
2020-06-10 12:08:58 +00:00
|
|
|
let(:query) { 'avg (metric) by (job)' }
|
2020-06-03 12:08:21 +00:00
|
|
|
let(:prometheus_response) do
|
|
|
|
{
|
|
|
|
"status": "success",
|
|
|
|
"data": {
|
|
|
|
"resultType": "vector",
|
|
|
|
"result": [
|
|
|
|
{
|
|
|
|
"metric": { "job" => "gitlab-rails" },
|
|
|
|
"value": [1488758662.506, "1"]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"metric": { "job" => "gitlab-sidekiq" },
|
|
|
|
"value": [1488758662.506, "2"]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
2020-08-11 03:11:00 +00:00
|
|
|
|
2020-06-10 12:08:58 +00:00
|
|
|
let(:query_url) { prometheus_query_with_time_url(query, Time.now.utc) }
|
2020-06-03 12:08:21 +00:00
|
|
|
|
|
|
|
around do |example|
|
2020-08-31 15:10:41 +00:00
|
|
|
freeze_time { example.run }
|
2020-06-03 12:08:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when request returns vector results' do
|
2020-06-10 12:08:58 +00:00
|
|
|
it 'returns data from the API call grouped by labels' do
|
2020-06-03 12:08:21 +00:00
|
|
|
req_stub = stub_prometheus_request(query_url, body: prometheus_response)
|
|
|
|
|
2020-06-10 12:08:58 +00:00
|
|
|
expect(subject.aggregate(query)).to eq({
|
|
|
|
{ "job" => "gitlab-rails" } => 1,
|
|
|
|
{ "job" => "gitlab-sidekiq" } => 2
|
2020-06-03 12:08:21 +00:00
|
|
|
})
|
|
|
|
expect(req_stub).to have_been_requested
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when request returns no data' do
|
|
|
|
it 'returns {}' do
|
|
|
|
req_stub = stub_prometheus_request(query_url, body: prometheus_empty_body('vector'))
|
|
|
|
|
2020-06-10 12:08:58 +00:00
|
|
|
expect(subject.aggregate(query)).to eq({})
|
2020-06-03 12:08:21 +00:00
|
|
|
expect(req_stub).to have_been_requested
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'failure response' do
|
2020-06-10 12:08:58 +00:00
|
|
|
let(:execute_query) { subject.aggregate(query) }
|
2020-06-03 12:08:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-30 13:25:05 +00:00
|
|
|
describe '#series' do
|
|
|
|
let(:query_url) { prometheus_series_url('series_name', 'other_service') }
|
|
|
|
|
|
|
|
around do |example|
|
2020-08-31 15:10:41 +00:00
|
|
|
freeze_time { example.run }
|
2017-05-30 13:25:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls endpoint and returns list of series' do
|
|
|
|
req_stub = stub_prometheus_request(query_url, body: prometheus_series('series_name'))
|
|
|
|
expected = prometheus_series('series_name').deep_stringify_keys['data']
|
|
|
|
|
|
|
|
expect(subject.series('series_name', 'other_service')).to eq(expected)
|
|
|
|
|
|
|
|
expect(req_stub).to have_been_requested
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#label_values' do
|
|
|
|
let(:query_url) { prometheus_label_values_url('__name__') }
|
|
|
|
|
|
|
|
it 'calls endpoint and returns label values' do
|
|
|
|
req_stub = stub_prometheus_request(query_url, body: prometheus_label_values)
|
|
|
|
expected = prometheus_label_values.deep_stringify_keys['data']
|
|
|
|
|
|
|
|
expect(subject.label_values('__name__')).to eq(expected)
|
|
|
|
|
|
|
|
expect(req_stub).to have_been_requested
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-07 16:57:42 +00:00
|
|
|
describe '#query_range' do
|
|
|
|
let(:prometheus_query) { prometheus_memory_query('env-slug') }
|
|
|
|
let(:query_url) { prometheus_query_range_url(prometheus_query) }
|
|
|
|
|
|
|
|
around do |example|
|
2020-08-31 15:10:41 +00:00
|
|
|
freeze_time { example.run }
|
2017-03-07 16:57:42 +00:00
|
|
|
end
|
|
|
|
|
2017-05-09 04:15:34 +00:00
|
|
|
context 'when non utc time is passed' do
|
|
|
|
let(:time_stop) { Time.now.in_time_zone("Warsaw") }
|
|
|
|
let(:time_start) { time_stop - 8.hours }
|
|
|
|
|
2020-03-24 00:09:24 +00:00
|
|
|
let(:query_url) { prometheus_query_range_url(prometheus_query, start_time: time_start.utc.to_f, end_time: time_stop.utc.to_f) }
|
2017-05-09 04:15:34 +00:00
|
|
|
|
|
|
|
it 'passed dates are properly converted to utc' do
|
|
|
|
req_stub = stub_prometheus_request(query_url, body: prometheus_values_body('vector'))
|
|
|
|
|
2020-03-24 00:09:24 +00:00
|
|
|
subject.query_range(prometheus_query, start_time: time_start, end_time: time_stop)
|
2017-05-09 04:15:34 +00:00
|
|
|
expect(req_stub).to have_been_requested
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-07 16:57:42 +00:00
|
|
|
context 'when a start time is passed' do
|
2020-03-24 00:09:24 +00:00
|
|
|
let(:query_url) { prometheus_query_range_url(prometheus_query, start_time: 2.hours.ago) }
|
2017-03-07 16:57:42 +00:00
|
|
|
|
|
|
|
it 'passed it in the requested URL' do
|
|
|
|
req_stub = stub_prometheus_request(query_url, body: prometheus_values_body('vector'))
|
|
|
|
|
2020-03-24 00:09:24 +00:00
|
|
|
subject.query_range(prometheus_query, start_time: 2.hours.ago)
|
2017-03-07 16:57:42 +00:00
|
|
|
expect(req_stub).to have_been_requested
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when request returns vector results' do
|
|
|
|
it 'returns nil' do
|
|
|
|
req_stub = stub_prometheus_request(query_url, body: prometheus_values_body('vector'))
|
|
|
|
|
|
|
|
expect(subject.query_range(prometheus_query)).to be_nil
|
|
|
|
expect(req_stub).to have_been_requested
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when request returns matrix results' do
|
|
|
|
it 'returns data from the API call' do
|
|
|
|
req_stub = stub_prometheus_request(query_url, body: prometheus_values_body('matrix'))
|
|
|
|
|
|
|
|
expect(subject.query_range(prometheus_query)).to eq([
|
|
|
|
{
|
|
|
|
"metric" => {},
|
|
|
|
"values" => [[1488758662.506, "0.00002996364761904785"], [1488758722.506, "0.00003090239047619091"]]
|
|
|
|
}
|
|
|
|
])
|
|
|
|
expect(req_stub).to have_been_requested
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when request returns no data' do
|
|
|
|
it 'returns []' do
|
|
|
|
req_stub = stub_prometheus_request(query_url, body: prometheus_empty_body('matrix'))
|
|
|
|
|
|
|
|
expect(subject.query_range(prometheus_query)).to be_empty
|
|
|
|
expect(req_stub).to have_been_requested
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'failure response' do
|
|
|
|
let(:execute_query) { subject.query_range(prometheus_query) }
|
|
|
|
end
|
|
|
|
end
|
2019-04-04 15:38:37 +00:00
|
|
|
|
|
|
|
describe '.compute_step' do
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
let(:now) { Time.now.utc }
|
|
|
|
|
|
|
|
subject { described_class.compute_step(start, stop) }
|
|
|
|
|
|
|
|
where(:time_interval_in_seconds, :step) do
|
|
|
|
0 | 60
|
|
|
|
10.hours | 60
|
|
|
|
10.hours + 1 | 61
|
|
|
|
# frontend options
|
|
|
|
30.minutes | 60
|
|
|
|
3.hours | 60
|
|
|
|
8.hours | 60
|
|
|
|
1.day | 144
|
|
|
|
3.days | 432
|
|
|
|
1.week | 1008
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
let(:start) { now - time_interval_in_seconds }
|
|
|
|
let(:stop) { now }
|
|
|
|
|
|
|
|
it { is_expected.to eq(step) }
|
|
|
|
end
|
|
|
|
end
|
2019-04-05 08:05:54 +00:00
|
|
|
|
|
|
|
describe 'proxy' do
|
|
|
|
context 'get API' do
|
|
|
|
let(:prometheus_query) { prometheus_cpu_query('env-slug') }
|
|
|
|
let(:query_url) { prometheus_query_url(prometheus_query) }
|
|
|
|
|
|
|
|
around do |example|
|
2020-08-31 15:10:41 +00:00
|
|
|
freeze_time { example.run }
|
2019-04-05 08:05:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when response status code is 200' do
|
|
|
|
it 'returns response object' do
|
|
|
|
req_stub = stub_prometheus_request(query_url, body: prometheus_value_body('vector'))
|
|
|
|
|
|
|
|
response = subject.proxy('query', { query: prometheus_query })
|
2020-05-01 00:09:59 +00:00
|
|
|
json_response = Gitlab::Json.parse(response.body)
|
2019-04-05 08:05:54 +00:00
|
|
|
|
|
|
|
expect(response.code).to eq(200)
|
|
|
|
expect(json_response).to eq({
|
|
|
|
'status' => 'success',
|
|
|
|
'data' => {
|
|
|
|
'resultType' => 'vector',
|
|
|
|
'result' => [{ "metric" => {}, "value" => [1488772511.004, "0.000041021495238095323"] }]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
expect(req_stub).to have_been_requested
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when response status code is not 200' do
|
|
|
|
it 'returns response object' do
|
|
|
|
req_stub = stub_prometheus_request(query_url, status: 400, body: { error: 'error' })
|
|
|
|
|
|
|
|
response = subject.proxy('query', { query: prometheus_query })
|
2020-05-01 00:09:59 +00:00
|
|
|
json_response = Gitlab::Json.parse(response.body)
|
2019-04-05 08:05:54 +00:00
|
|
|
|
|
|
|
expect(req_stub).to have_been_requested
|
|
|
|
expect(response.code).to eq(400)
|
|
|
|
expect(json_response).to eq('error' => 'error')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-07 02:42:20 +00:00
|
|
|
context 'when Gitlab::HTTP::ResponseError is raised' do
|
2019-04-05 08:05:54 +00:00
|
|
|
before do
|
2019-08-07 02:42:20 +00:00
|
|
|
stub_prometheus_request_with_exception(query_url, response_error)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "without response code" do
|
|
|
|
let(:response_error) { Gitlab::HTTP::ResponseError }
|
2019-12-12 12:07:33 +00:00
|
|
|
|
2020-07-03 15:09:13 +00:00
|
|
|
it 'raises PrometheusClient::ConnectionError' do
|
2019-08-07 02:42:20 +00:00
|
|
|
expect { subject.proxy('query', { query: prometheus_query }) }.to(
|
2020-07-03 15:09:13 +00:00
|
|
|
raise_error(Gitlab::PrometheusClient::ConnectionError, 'Network connection error')
|
2019-08-07 02:42:20 +00:00
|
|
|
)
|
|
|
|
end
|
2019-04-05 08:05:54 +00:00
|
|
|
end
|
|
|
|
|
2019-08-07 02:42:20 +00:00
|
|
|
context "with response code" do
|
|
|
|
let(:response_error) do
|
|
|
|
response = Net::HTTPResponse.new(1.1, 400, '{}sumpthin')
|
|
|
|
allow(response).to receive(:body) { '{}' }
|
|
|
|
Gitlab::HTTP::ResponseError.new(response)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises Gitlab::PrometheusClient::QueryError' do
|
|
|
|
expect { subject.proxy('query', { query: prometheus_query }) }.to(
|
|
|
|
raise_error(Gitlab::PrometheusClient::QueryError, 'Bad data received')
|
|
|
|
)
|
|
|
|
end
|
2019-04-05 08:05:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-03-07 16:57:42 +00:00
|
|
|
end
|