gitlab-org--gitlab-foss/spec/lib/gitlab/metrics/rack_middleware_spec.rb
Yorick Peterse d345591fc8
Tracking of custom events
GitLab Performance Monitoring is now able to track custom events not
directly related to application performance. These events include the
number of tags pushed, repositories created, builds registered, etc.

The use of these events is to get a better overview of how a GitLab
instance is used and how that may affect performance. For example, a
large number of Git pushes may have a negative impact on the underlying
storage engine.

Events are stored in the "events" measurement and are not prefixed with
"rails_" or "sidekiq_", this makes it easier to query events with the
same name triggered from different parts of the application. All events
being stored in the same measurement also makes it easier to downsample
data.

Currently the following events are tracked:

* Creating repositories
* Removing repositories
* Changing the default branch of a repository
* Pushing a new tag
* Removing an existing tag
* Pushing a commit (along with the branch being pushed to)
* Pushing a new branch
* Removing an existing branch
* Importing a repository (along with the URL we're importing)
* Forking a repository (along with the source/target path)
* CI builds registered (and when no build could be found)
* CI builds being updated
* Rails and Sidekiq exceptions

Fixes gitlab-org/gitlab-ce#13720
2016-08-17 10:04:04 +02:00

117 lines
3.6 KiB
Ruby

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
klass = double(:klass, name: 'TestController')
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
it 'tags a transaction with the method andpath of the route in the grape endpoint' do
route = double(:route, route_method: "GET", route_path: "/:version/projects/:id/archive(.:format)")
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
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
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
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
end
describe '#tag_controller' do
let(:transaction) { middleware.transaction_from_env(env) }
it 'tags a transaction with the name and action of a controller' do
klass = double(:klass, name: 'TestController')
controller = double(:controller, class: klass, action_name: 'show')
env['action_controller.instance'] = controller
middleware.tag_controller(transaction, env)
expect(transaction.action).to eq('TestController#show')
end
end
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
route = double(:route, route_method: "GET", route_path: "/:version/projects/:id/archive(.:format)")
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
end
end