2019-08-22 06:57:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-04 11:28:34 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-12 18:46:17 -04:00
|
|
|
describe Gitlab::Metrics::RequestsRackMiddleware do
|
2017-07-04 11:28:34 -04:00
|
|
|
let(:app) { double('app') }
|
2019-12-12 07:07:33 -05:00
|
|
|
|
2017-07-04 11:28:34 -04:00
|
|
|
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
|
2017-07-12 18:46:17 -04:00
|
|
|
expect(described_class).to receive_message_chain(:http_request_total, :increment).with(method: 'get')
|
2017-07-04 11:28:34 -04:00
|
|
|
|
|
|
|
subject.call(env)
|
|
|
|
end
|
|
|
|
|
2017-08-11 06:36:03 -04:00
|
|
|
RSpec::Matchers.define :a_positive_execution_time do
|
|
|
|
match { |actual| actual > 0 }
|
|
|
|
end
|
2017-07-04 11:28:34 -04:00
|
|
|
|
2017-08-11 06:36:03 -04:00
|
|
|
it 'measures execution time' do
|
2019-11-13 16:06:45 -05:00
|
|
|
expect(described_class).to receive_message_chain(:http_request_duration_seconds, :observe).with({ status: '200', method: 'get' }, a_positive_execution_time)
|
2017-07-04 11:28:34 -04:00
|
|
|
|
2017-08-11 06:36:03 -04:00
|
|
|
Timecop.scale(3600) { subject.call(env) }
|
2017-07-04 11:28:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context '@app.call throws exception' do
|
2017-07-12 18:46:17 -04:00
|
|
|
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
|
2017-07-04 11:28:34 -04:00
|
|
|
|
|
|
|
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
|
2017-07-12 18:46:17 -04:00
|
|
|
expect(described_class).to receive_message_chain(:http_request_total, :increment).with(method: 'get')
|
2017-07-04 11:28:34 -04:00
|
|
|
|
|
|
|
expect { subject.call(env) }.to raise_error(StandardError)
|
|
|
|
end
|
|
|
|
|
2017-07-12 18:46:17 -04:00
|
|
|
it "does't measure request execution time" do
|
|
|
|
expect(described_class.http_request_duration_seconds).not_to receive(:increment)
|
2017-07-04 11:28:34 -04:00
|
|
|
|
|
|
|
expect { subject.call(env) }.to raise_error(StandardError)
|
|
|
|
end
|
|
|
|
end
|
2019-10-02 17:06:22 -04:00
|
|
|
|
|
|
|
describe '.initialize_http_request_duration_seconds' do
|
|
|
|
it "sets labels" do
|
|
|
|
expected_labels = []
|
|
|
|
described_class::HTTP_METHODS.each do |method, statuses|
|
|
|
|
statuses.each do |status|
|
2019-11-13 16:06:45 -05:00
|
|
|
expected_labels << { method: method, status: status.to_s }
|
2019-10-02 17:06:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
described_class.initialize_http_request_duration_seconds
|
|
|
|
expect(described_class.http_request_duration_seconds.values.keys).to include(*expected_labels)
|
|
|
|
end
|
|
|
|
end
|
2017-07-04 11:28:34 -04:00
|
|
|
end
|
|
|
|
end
|