2016-01-14 18:37:52 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Gitlab::Metrics::RackMiddleware do
|
|
|
|
let(:app) { double(:app) }
|
|
|
|
|
|
|
|
let(:middleware) { described_class.new(app) }
|
|
|
|
|
|
|
|
let(:env) { { 'REQUEST_METHOD' => 'GET', 'REQUEST_URI' => '/foo' } }
|
|
|
|
|
|
|
|
describe '#call' do
|
|
|
|
before do
|
|
|
|
expect_any_instance_of(Gitlab::Metrics::Transaction).to receive(:finish)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'tracks a transaction' do
|
|
|
|
expect(app).to receive(:call).with(env).and_return('yay')
|
|
|
|
|
|
|
|
expect(middleware.call(env)).to eq('yay')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'tags a transaction with the name and action of a controller' do
|
2016-09-29 09:46:39 +05:30
|
|
|
klass = double(:klass, name: 'TestController', content_type: 'text/html')
|
2016-01-14 18:37:52 +05:30
|
|
|
controller = double(:controller, class: klass, action_name: 'show')
|
|
|
|
|
|
|
|
env['action_controller.instance'] = controller
|
|
|
|
|
|
|
|
allow(app).to receive(:call).with(env)
|
|
|
|
|
|
|
|
expect(middleware).to receive(:tag_controller).
|
|
|
|
with(an_instance_of(Gitlab::Metrics::Transaction), env)
|
|
|
|
|
|
|
|
middleware.call(env)
|
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
it 'tags a transaction with the method and path of the route in the grape endpoint' do
|
2017-08-17 22:00:37 +05:30
|
|
|
route = double(:route, request_method: "GET", path: "/:version/projects/:id/archive(.:format)")
|
2016-06-16 23:09:34 +05:30
|
|
|
endpoint = double(:endpoint, route: route)
|
|
|
|
|
|
|
|
env['api.endpoint'] = endpoint
|
|
|
|
|
|
|
|
allow(app).to receive(:call).with(env)
|
|
|
|
|
|
|
|
expect(middleware).to receive(:tag_endpoint).
|
|
|
|
with(an_instance_of(Gitlab::Metrics::Transaction), env)
|
|
|
|
|
|
|
|
middleware.call(env)
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
it 'tracks any raised exceptions' do
|
|
|
|
expect(app).to receive(:call).with(env).and_raise(RuntimeError)
|
|
|
|
|
|
|
|
expect_any_instance_of(Gitlab::Metrics::Transaction).
|
|
|
|
to receive(:add_event).with(:rails_exception)
|
|
|
|
|
|
|
|
expect { middleware.call(env) }.to raise_error(RuntimeError)
|
|
|
|
end
|
2016-01-14 18:37:52 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '#transaction_from_env' do
|
|
|
|
let(:transaction) { middleware.transaction_from_env(env) }
|
|
|
|
|
|
|
|
it 'returns a Transaction' do
|
|
|
|
expect(transaction).to be_an_instance_of(Gitlab::Metrics::Transaction)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'stores the request method and URI in the transaction as values' do
|
|
|
|
expect(transaction.values[:request_method]).to eq('GET')
|
|
|
|
expect(transaction.values[:request_uri]).to eq('/foo')
|
|
|
|
end
|
2016-06-22 15:30:34 +05:30
|
|
|
|
|
|
|
context "when URI includes sensitive parameters" do
|
|
|
|
let(:env) do
|
|
|
|
{
|
|
|
|
'REQUEST_METHOD' => 'GET',
|
|
|
|
'REQUEST_URI' => '/foo?private_token=my-token',
|
|
|
|
'PATH_INFO' => '/foo',
|
|
|
|
'QUERY_STRING' => 'private_token=my_token',
|
|
|
|
'action_dispatch.parameter_filter' => [:private_token]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'stores the request URI with the sensitive parameters filtered' do
|
|
|
|
expect(transaction.values[:request_uri]).to eq('/foo?private_token=[FILTERED]')
|
|
|
|
end
|
|
|
|
end
|
2016-01-14 18:37:52 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '#tag_controller' do
|
|
|
|
let(:transaction) { middleware.transaction_from_env(env) }
|
2016-09-29 09:46:39 +05:30
|
|
|
let(:content_type) { 'text/html' }
|
2016-01-14 18:37:52 +05:30
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
before do
|
2016-01-14 18:37:52 +05:30
|
|
|
klass = double(:klass, name: 'TestController')
|
2016-09-29 09:46:39 +05:30
|
|
|
controller = double(:controller, class: klass, action_name: 'show', content_type: content_type)
|
2016-01-14 18:37:52 +05:30
|
|
|
|
|
|
|
env['action_controller.instance'] = controller
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
2016-01-14 18:37:52 +05:30
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
it 'tags a transaction with the name and action of a controller' do
|
2016-01-14 18:37:52 +05:30
|
|
|
middleware.tag_controller(transaction, env)
|
|
|
|
|
|
|
|
expect(transaction.action).to eq('TestController#show')
|
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
|
|
|
|
context 'when the response content type is not :html' do
|
|
|
|
let(:content_type) { 'application/json' }
|
|
|
|
|
|
|
|
it 'appends the mime type to the transaction action' do
|
|
|
|
middleware.tag_controller(transaction, env)
|
|
|
|
|
|
|
|
expect(transaction.action).to eq('TestController#show.json')
|
|
|
|
end
|
|
|
|
end
|
2016-01-14 18:37:52 +05:30
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
|
|
|
|
describe '#tag_endpoint' do
|
|
|
|
let(:transaction) { middleware.transaction_from_env(env) }
|
|
|
|
|
|
|
|
it 'tags a transaction with the method and path of the route in the grape endpount' do
|
2017-08-17 22:00:37 +05:30
|
|
|
route = double(:route, request_method: "GET", path: "/:version/projects/:id/archive(.:format)")
|
2016-06-16 23:09:34 +05:30
|
|
|
endpoint = double(:endpoint, route: route)
|
|
|
|
|
|
|
|
env['api.endpoint'] = endpoint
|
|
|
|
|
|
|
|
middleware.tag_endpoint(transaction, env)
|
|
|
|
|
|
|
|
expect(transaction.action).to eq('Grape#GET /projects/:id/archive')
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
it 'does not tag a transaction if route infos are missing' do
|
|
|
|
endpoint = double(:endpoint)
|
|
|
|
allow(endpoint).to receive(:route).and_raise
|
|
|
|
|
|
|
|
env['api.endpoint'] = endpoint
|
|
|
|
|
|
|
|
middleware.tag_endpoint(transaction, env)
|
|
|
|
|
|
|
|
expect(transaction.action).to be_nil
|
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
2016-01-14 18:37:52 +05:30
|
|
|
end
|