2019-12-04 20:38:33 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-01-14 18:37:52 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::Metrics::WebTransaction do
|
2018-03-17 18:26:18 +05:30
|
|
|
let(:env) { {} }
|
|
|
|
let(:transaction) { described_class.new(env) }
|
2020-05-24 23:13:21 +05:30
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
describe '#run' do
|
|
|
|
let(:prometheus_metric) { instance_double(Prometheus::Client::Metric, base_labels: {}) }
|
2020-10-24 23:57:45 +05:30
|
|
|
|
|
|
|
before do
|
2021-12-11 22:18:48 +05:30
|
|
|
allow(described_class).to receive(:prometheus_metric).and_return(prometheus_metric)
|
2020-10-24 23:57:45 +05:30
|
|
|
allow(transaction).to receive(:observe)
|
|
|
|
end
|
|
|
|
|
2016-01-14 18:37:52 +05:30
|
|
|
it 'yields the supplied block' do
|
|
|
|
expect { |b| transaction.run(&b) }.to yield_control
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'stores the transaction in the current thread' do
|
|
|
|
transaction.run do
|
|
|
|
expect(Thread.current[described_class::THREAD_KEY]).to eq(transaction)
|
2021-09-04 01:27:46 +05:30
|
|
|
expect(described_class.current).to eq(transaction)
|
|
|
|
|
|
|
|
['200', {}, '']
|
2016-01-14 18:37:52 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes the transaction from the current thread upon completion' do
|
|
|
|
transaction.run { }
|
|
|
|
|
|
|
|
expect(Thread.current[described_class::THREAD_KEY]).to be_nil
|
2021-09-04 01:27:46 +05:30
|
|
|
expect(described_class.current).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'records the duration of the transaction if the request was successful' do
|
|
|
|
expect(transaction).to receive(:observe).with(:gitlab_transaction_duration_seconds, instance_of(Float))
|
|
|
|
|
|
|
|
transaction.run { ['200', {}, ''] }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not record the duration of the transaction if the request failed' do
|
|
|
|
expect(transaction).not_to receive(:observe).with(:gitlab_transaction_duration_seconds, instance_of(Float))
|
|
|
|
|
|
|
|
transaction.run { ['500', {}, ''] }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not record the duration of the transaction if it raised' do
|
|
|
|
expect(transaction).not_to receive(:observe).with(:gitlab_transaction_duration_seconds, instance_of(Float))
|
|
|
|
|
|
|
|
expect do
|
|
|
|
transaction.run { raise 'broken' }
|
|
|
|
end.to raise_error('broken')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the rack response' do
|
|
|
|
response = ['500', {}, '']
|
|
|
|
|
|
|
|
expect(transaction.run { response }).to eq(response)
|
2016-01-14 18:37:52 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
describe '#labels' do
|
|
|
|
context 'when request goes to Grape endpoint' do
|
|
|
|
before do
|
|
|
|
route = double(:route, request_method: 'GET', path: '/:version/projects/:id/archive(.:format)')
|
2021-02-22 17:27:13 +05:30
|
|
|
endpoint = double(:endpoint, route: route,
|
|
|
|
options: { for: API::Projects, path: [":id/archive"] },
|
|
|
|
namespace: "/projects")
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
env['api.endpoint'] = endpoint
|
2021-11-18 22:05:49 +05:30
|
|
|
|
|
|
|
# This is needed since we're not actually making a request, which would trigger the controller pushing to the context
|
|
|
|
::Gitlab::ApplicationContext.push(feature_category: 'projects')
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it 'provides labels with the method and path of the route in the grape endpoint' do
|
2021-02-22 17:27:13 +05:30
|
|
|
expect(transaction.labels).to eq({ controller: 'Grape', action: 'GET /projects/:id/archive', feature_category: 'projects' })
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'contains only the labels defined for transactions' do
|
2021-12-11 22:18:48 +05:30
|
|
|
expect(transaction.labels.keys).to contain_exactly(*described_class::BASE_LABEL_KEYS)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not provide labels if route infos are missing' do
|
|
|
|
endpoint = double(:endpoint)
|
|
|
|
allow(endpoint).to receive(:route).and_raise
|
|
|
|
|
|
|
|
env['api.endpoint'] = endpoint
|
|
|
|
|
|
|
|
expect(transaction.labels).to eq({})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when request goes to ActionController' do
|
2021-12-11 22:18:48 +05:30
|
|
|
let(:request) { double(:request, format: double(:format, ref: :html)) }
|
|
|
|
let(:controller_class) { double(:controller_class, name: 'TestController') }
|
|
|
|
|
|
|
|
before do
|
|
|
|
controller = double(:controller, class: controller_class, action_name: 'show', request: request)
|
|
|
|
env['action_controller.instance'] = controller
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
it 'tags a transaction with the name and action of a controller' do
|
2021-11-18 22:05:49 +05:30
|
|
|
expect(transaction.labels).to eq({ controller: 'TestController', action: 'show', feature_category: ::Gitlab::FeatureCategories::FEATURE_CATEGORY_DEFAULT })
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'contains only the labels defined for transactions' do
|
2021-12-11 22:18:48 +05:30
|
|
|
expect(transaction.labels.keys).to contain_exactly(*described_class::BASE_LABEL_KEYS)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
context 'when the request content type is not :html' do
|
2018-10-15 14:42:47 +05:30
|
|
|
let(:request) { double(:request, format: double(:format, ref: :json)) }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
it 'appends the mime type to the transaction action' do
|
2021-11-18 22:05:49 +05:30
|
|
|
expect(transaction.labels).to eq({ controller: 'TestController', action: 'show.json', feature_category: ::Gitlab::FeatureCategories::FEATURE_CATEGORY_DEFAULT })
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
context 'when the request content type is not' do
|
|
|
|
let(:request) { double(:request, format: double(:format, ref: 'http://example.com')) }
|
|
|
|
|
|
|
|
it 'does not append the MIME type to the transaction action' do
|
2021-11-18 22:05:49 +05:30
|
|
|
expect(transaction.labels).to eq({ controller: 'TestController', action: 'show', feature_category: ::Gitlab::FeatureCategories::FEATURE_CATEGORY_DEFAULT })
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the feature category is known' do
|
|
|
|
it 'includes it in the feature category label' do
|
2021-11-18 22:05:49 +05:30
|
|
|
# This is needed since we're not actually making a request, which would trigger the controller pushing to the context
|
|
|
|
::Gitlab::ApplicationContext.push(feature_category: 'source_code_management')
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
expect(transaction.labels).to eq({ controller: 'TestController', action: 'show', feature_category: "source_code_management" })
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns no labels when no route information is present in env' do
|
|
|
|
expect(transaction.labels).to eq({})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
it_behaves_like 'transaction metrics with labels' do
|
|
|
|
let(:request) { double(:request, format: double(:format, ref: :html)) }
|
|
|
|
let(:controller_class) { double(:controller_class, name: 'TestController') }
|
|
|
|
let(:controller) { double(:controller, class: controller_class, action_name: 'show', request: request) }
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
let(:transaction_obj) { described_class.new({ 'action_controller.instance' => controller }) }
|
|
|
|
let(:labels) { { controller: 'TestController', action: 'show', feature_category: 'projects' } }
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
before do
|
|
|
|
::Gitlab::ApplicationContext.push(feature_category: 'projects')
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
2016-01-14 18:37:52 +05:30
|
|
|
end
|
|
|
|
end
|