debian-mirror-gitlab/spec/lib/gitlab/metrics/requests_rack_middleware_spec.rb

150 lines
5.1 KiB
Ruby
Raw Normal View History

2019-12-04 20:38:33 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::Metrics::RequestsRackMiddleware do
2017-09-10 17:25:29 +05:30
let(:app) { double('app') }
2020-01-01 13:55:28 +05:30
2017-09-10 17:25:29 +05:30
subject { described_class.new(app) }
describe '#call' do
let(:status) { 100 }
let(:env) { { 'REQUEST_METHOD' => 'GET' } }
let(:stack_result) { [status, {}, 'body'] }
before do
allow(app).to receive(:call).and_return(stack_result)
end
context '@app.call succeeds with 200' do
before do
allow(app).to receive(:call).and_return([200, nil, nil])
end
it 'increments requests count' do
2021-01-03 14:25:43 +05:30
expect(described_class).to receive_message_chain(:http_request_total, :increment).with(method: 'get', status: 200, feature_category: 'unknown')
2017-09-10 17:25:29 +05:30
subject.call(env)
end
2018-03-17 18:26:18 +05:30
RSpec::Matchers.define :a_positive_execution_time do
match { |actual| actual > 0 }
end
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
it 'measures execution time' do
2021-01-03 14:25:43 +05:30
expect(described_class).to receive_message_chain(:http_request_duration_seconds, :observe).with({ method: 'get' }, a_positive_execution_time)
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
Timecop.scale(3600) { subject.call(env) }
2017-09-10 17:25:29 +05:30
end
2020-04-22 19:07:51 +05:30
context 'request is a health check endpoint' do
2021-01-03 14:25:43 +05:30
['/-/liveness', '/-/liveness/', '/-/%6D%65%74%72%69%63%73'].each do |path|
context "when path is #{path}" do
before do
env['PATH_INFO'] = path
end
2020-04-22 19:07:51 +05:30
2021-01-03 14:25:43 +05:30
it 'increments health endpoint counter rather than overall counter' do
expect(described_class).to receive_message_chain(:http_health_requests_total, :increment).with(method: 'get', status: 200)
expect(described_class).not_to receive(:http_request_total)
2020-04-22 19:07:51 +05:30
2021-01-03 14:25:43 +05:30
subject.call(env)
end
2020-04-22 19:07:51 +05:30
2021-01-03 14:25:43 +05:30
it 'does not record the request duration' do
expect(described_class).not_to receive(:http_request_duration_seconds)
2020-04-22 19:07:51 +05:30
2021-01-03 14:25:43 +05:30
subject.call(env)
end
2020-04-22 19:07:51 +05:30
end
end
end
context 'request is not a health check endpoint' do
2021-01-03 14:25:43 +05:30
['/-/ordinary-requests', '/-/', '/-/health/subpath'].each do |path|
context "when path is #{path}" do
before do
env['PATH_INFO'] = path
end
it 'increments overall counter rather than health endpoint counter' do
expect(described_class).to receive_message_chain(:http_request_total, :increment).with(method: 'get', status: 200, feature_category: 'unknown')
expect(described_class).not_to receive(:http_health_requests_total)
subject.call(env)
end
it 'records the request duration' do
expect(described_class)
.to receive_message_chain(:http_request_duration_seconds, :observe)
.with({ method: 'get' }, a_positive_execution_time)
subject.call(env)
end
2020-04-22 19:07:51 +05:30
end
end
end
2017-09-10 17:25:29 +05:30
end
context '@app.call throws exception' do
let(:http_request_duration_seconds) { double('http_request_duration_seconds') }
before do
allow(app).to receive(:call).and_raise(StandardError)
allow(described_class).to receive(:http_request_duration_seconds).and_return(http_request_duration_seconds)
end
it 'increments exceptions count' do
expect(described_class).to receive_message_chain(:rack_uncaught_errors_count, :increment)
expect { subject.call(env) }.to raise_error(StandardError)
end
it 'increments requests count' do
2021-01-03 14:25:43 +05:30
expect(described_class).to receive_message_chain(:http_request_total, :increment).with(method: 'get', status: 'undefined', feature_category: 'unknown')
2017-09-10 17:25:29 +05:30
expect { subject.call(env) }.to raise_error(StandardError)
end
it "does't measure request execution time" do
expect(described_class.http_request_duration_seconds).not_to receive(:increment)
expect { subject.call(env) }.to raise_error(StandardError)
end
end
2019-12-21 20:55:43 +05:30
2021-01-03 14:25:43 +05:30
context 'when a feature category header is present' do
before do
allow(app).to receive(:call).and_return([200, { described_class::FEATURE_CATEGORY_HEADER => 'issue_tracking' }, nil])
end
it 'adds the feature category to the labels for http_request_total' do
expect(described_class).to receive_message_chain(:http_request_total, :increment).with(method: 'get', status: 200, feature_category: 'issue_tracking')
subject.call(env)
end
it 'does not record a feature category for health check endpoints' do
env['PATH_INFO'] = '/-/liveness'
expect(described_class).to receive_message_chain(:http_health_requests_total, :increment).with(method: 'get', status: 200)
expect(described_class).not_to receive(:http_request_total)
subject.call(env)
end
end
2019-12-21 20:55:43 +05:30
describe '.initialize_http_request_duration_seconds' do
it "sets labels" do
expected_labels = []
2021-01-03 14:25:43 +05:30
described_class::HTTP_METHODS.each do |method|
expected_labels << { method: method }
2019-12-21 20:55:43 +05:30
end
described_class.initialize_http_request_duration_seconds
expect(described_class.http_request_duration_seconds.values.keys).to include(*expected_labels)
end
end
2017-09-10 17:25:29 +05:30
end
end