summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/prometheus_client_spec.rb
blob: d0bfc6e5610b2aa570c3df94e775be098022c8b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::PrometheusClient do
  include PrometheusHelpers

  subject { described_class.new('https://prometheus.example.com') }

  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

  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: '')

      expect { subject.healthy? }.to raise_error(Gitlab::PrometheusClient::UnexpectedResponseError)
    end
  end

  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

  # 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
      it 'raises a Gitlab::PrometheusClient::QueryError error' do
        req_stub = stub_prometheus_request(query_url, status: 400, body: { error: 'bar!' })

        expect { execute_query }
          .to raise_error(Gitlab::PrometheusClient::QueryError, 'bar!')
        expect(req_stub).to have_been_requested
      end
    end

    context 'when request returns 400 without an error message' do
      it 'raises a Gitlab::PrometheusClient::QueryError error' do
        req_stub = stub_prometheus_request(query_url, status: 400)

        expect { execute_query }
          .to raise_error(Gitlab::PrometheusClient::QueryError, 'Bad data received')
        expect(req_stub).to have_been_requested
      end
    end

    context 'when request returns 500' do
      it 'raises a Gitlab::PrometheusClient::UnexpectedResponseError error' do
        req_stub = stub_prometheus_request(query_url, status: 500, body: { message: 'FAIL!' })

        expect { execute_query }
          .to raise_error(Gitlab::PrometheusClient::UnexpectedResponseError, '500 - {"message":"FAIL!"}')
        expect(req_stub).to have_been_requested
      end
    end

    context 'when request returns non json data' do
      it 'raises a Gitlab::PrometheusClient::UnexpectedResponseError error' do
        req_stub = stub_prometheus_request(query_url, status: 200, body: 'not json')

        expect { execute_query }
          .to raise_error(Gitlab::PrometheusClient::UnexpectedResponseError, 'Parsing response failed')
        expect(req_stub).to have_been_requested
      end
    end
  end

  describe 'failure to reach a provided prometheus url' do
    let(:prometheus_url) { "https://prometheus.invalid.example.com/api/v1/query?query=1" }

    shared_examples 'exceptions are raised' do
      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
      end
    end

    context 'ping' do
      subject { described_class.new(prometheus_url).ping }

      it_behaves_like 'exceptions are raised'
    end

    context 'proxy' do
      subject { described_class.new(prometheus_url).proxy('query', { query: '1' }) }

      it_behaves_like 'exceptions are raised'
    end
  end

  describe '#query' do
    let(:prometheus_query) { prometheus_cpu_query('env-slug') }
    let(:query_url) { prometheus_query_with_time_url(prometheus_query, Time.now.utc) }

    around do |example|
      freeze_time { example.run }
    end

    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

  describe '#aggregate' do
    let(:query) { 'avg (metric) by (job)' }
    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

    let(:query_url) { prometheus_query_with_time_url(query, Time.now.utc) }

    around do |example|
      freeze_time { example.run }
    end

    context 'when request returns vector results' do
      it 'returns data from the API call grouped by labels' do
        req_stub = stub_prometheus_request(query_url, body: prometheus_response)

        expect(subject.aggregate(query)).to eq({
          { "job" => "gitlab-rails" } => 1,
          { "job" => "gitlab-sidekiq" } => 2
        })
        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.aggregate(query)).to eq({})
        expect(req_stub).to have_been_requested
      end
    end

    it_behaves_like 'failure response' do
      let(:execute_query) { subject.aggregate(query) }
    end
  end

  describe '#series' do
    let(:query_url) { prometheus_series_url('series_name', 'other_service') }

    around do |example|
      freeze_time { example.run }
    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

  describe '#query_range' do
    let(:prometheus_query) { prometheus_memory_query('env-slug') }
    let(:query_url) { prometheus_query_range_url(prometheus_query) }

    around do |example|
      freeze_time { example.run }
    end

    context 'when non utc time is passed' do
      let(:time_stop) { Time.now.in_time_zone("Warsaw") }
      let(:time_start) { time_stop - 8.hours }

      let(:query_url) { prometheus_query_range_url(prometheus_query, start_time: time_start.utc.to_f, end_time: time_stop.utc.to_f) }

      it 'passed dates are properly converted to utc' do
        req_stub = stub_prometheus_request(query_url, body: prometheus_values_body('vector'))

        subject.query_range(prometheus_query, start_time: time_start, end_time: time_stop)
        expect(req_stub).to have_been_requested
      end
    end

    context 'when a start time is passed' do
      let(:query_url) { prometheus_query_range_url(prometheus_query, start_time: 2.hours.ago) }

      it 'passed it in the requested URL' do
        req_stub = stub_prometheus_request(query_url, body: prometheus_values_body('vector'))

        subject.query_range(prometheus_query, start_time: 2.hours.ago)
        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

  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

  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|
        freeze_time { example.run }
      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 })
          json_response = Gitlab::Json.parse(response.body)

          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 })
          json_response = Gitlab::Json.parse(response.body)

          expect(req_stub).to have_been_requested
          expect(response.code).to eq(400)
          expect(json_response).to eq('error' => 'error')
        end
      end

      context 'when Gitlab::HTTP::ResponseError is raised' do
        before do
          stub_prometheus_request_with_exception(query_url, response_error)
        end

        context "without response code" do
          let(:response_error) { Gitlab::HTTP::ResponseError }

          it 'raises PrometheusClient::ConnectionError' do
            expect { subject.proxy('query', { query: prometheus_query }) }.to(
              raise_error(Gitlab::PrometheusClient::ConnectionError, 'Network connection error')
            )
          end
        end

        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
        end
      end
    end
  end
end