2016-05-25 10:37:18 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2016-05-26 11:53:21 -04:00
|
|
|
describe Gitlab::Middleware::RailsQueueDuration do
|
2016-05-25 10:37:18 -04:00
|
|
|
let(:app) { double(:app) }
|
|
|
|
let(:middleware) { described_class.new(app) }
|
|
|
|
let(:env) { {} }
|
2017-10-17 10:06:52 -04:00
|
|
|
let(:transaction) { Gitlab::Metrics::WebTransaction.new(env) }
|
2016-05-25 10:37:18 -04:00
|
|
|
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
expect(app).to receive(:call).with(env).and_return('yay')
|
|
|
|
end
|
2016-05-25 10:37:18 -04:00
|
|
|
|
|
|
|
describe '#call' do
|
|
|
|
it 'calls the app when metrics are disabled' do
|
|
|
|
expect(Gitlab::Metrics).to receive(:current_transaction).and_return(nil)
|
|
|
|
expect(middleware.call(env)).to eq('yay')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when metrics are enabled' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
allow(Gitlab::Metrics).to receive(:current_transaction).and_return(transaction)
|
|
|
|
end
|
2016-05-25 10:37:18 -04:00
|
|
|
|
|
|
|
it 'calls the app when metrics are enabled but no timing header is found' do
|
|
|
|
expect(middleware.call(env)).to eq('yay')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets proxy_flight_time and calls the app when the header is present' do
|
2016-08-19 06:25:52 -04:00
|
|
|
env['HTTP_GITLAB_WORKHORSE_PROXY_START'] = '123'
|
2016-05-26 11:53:21 -04:00
|
|
|
expect(transaction).to receive(:set).with(:rails_queue_duration, an_instance_of(Float))
|
2016-05-25 10:37:18 -04:00
|
|
|
expect(middleware.call(env)).to eq('yay')
|
|
|
|
end
|
2017-09-07 15:36:51 -04:00
|
|
|
|
|
|
|
it 'observes rails queue duration metrics and calls the app when the header is present' do
|
|
|
|
env['HTTP_GITLAB_WORKHORSE_PROXY_START'] = '2000000000'
|
|
|
|
|
2017-10-24 04:57:29 -04:00
|
|
|
expect(middleware.send(:metric_rails_queue_duration_seconds)).to receive(:observe).with(transaction.labels, 1)
|
2017-09-07 15:36:51 -04:00
|
|
|
|
|
|
|
Timecop.freeze(Time.at(3)) do
|
|
|
|
expect(middleware.call(env)).to eq('yay')
|
|
|
|
end
|
|
|
|
end
|
2016-05-25 10:37:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|