debian-mirror-gitlab/spec/lib/gitlab/metrics/web_transaction_spec.rb

160 lines
5 KiB
Ruby
Raw Normal View History

2019-12-04 20:38:33 +05:30
# frozen_string_literal: true
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
let(:prometheus_metric) { double("prometheus metric") }
before do
allow(described_class).to receive(:transaction_metric).and_return(prometheus_metric)
end
describe '#duration' do
it 'returns the duration of a transaction in seconds' do
transaction.run { sleep(0.5) }
expect(transaction.duration).to be >= 0.5
end
end
describe '#allocated_memory' do
it 'returns the allocated memory in bytes' do
transaction.run { 'a' * 32 }
expect(transaction.allocated_memory).to be_a_kind_of(Numeric)
end
end
describe '#run' do
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)
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
end
end
2016-09-13 17:45:13 +05:30
describe '#method_call_for' do
it 'returns a MethodCall' do
2018-03-17 18:26:18 +05:30
method = transaction.method_call_for('Foo#bar', :Foo, '#bar')
2016-06-22 15:30:34 +05:30
2016-09-13 17:45:13 +05:30
expect(method).to be_an_instance_of(Gitlab::Metrics::MethodCall)
2016-06-22 15:30:34 +05:30
end
end
describe '#increment' do
it 'increments a counter' do
2020-05-24 23:13:21 +05:30
expect(prometheus_metric).to receive(:increment).with({}, 1)
2020-05-24 23:13:21 +05:30
transaction.increment(:time, 1)
end
end
describe '#set' do
it 'sets a value' do
2020-05-24 23:13:21 +05:30
expect(prometheus_metric).to receive(:set).with({}, 10)
2020-05-24 23:13:21 +05:30
transaction.set(:number, 10)
2016-09-13 17:45:13 +05:30
end
end
2018-03-17 18:26:18 +05:30
describe '#labels' do
2020-07-28 23:09:34 +05:30
let(:request) { double(:request, format: double(:format, ref: :html)) }
let(:controller_class) { double(:controller_class, name: 'TestController') }
2018-03-17 18:26:18 +05:30
context 'when request goes to Grape endpoint' do
before do
route = double(:route, request_method: 'GET', path: '/:version/projects/:id/archive(.:format)')
endpoint = double(:endpoint, route: route)
env['api.endpoint'] = endpoint
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
2020-07-28 23:09:34 +05:30
expect(transaction.labels).to eq({ controller: 'Grape', action: 'GET /projects/:id/archive', feature_category: '' })
end
it 'contains only the labels defined for transactions' do
expect(transaction.labels.keys).to contain_exactly(*described_class.superclass::BASE_LABELS.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
before do
2020-07-28 23:09:34 +05:30
controller = double(:controller, class: controller_class, action_name: 'show', request: request)
2018-03-17 18:26:18 +05:30
env['action_controller.instance'] = controller
end
it 'tags a transaction with the name and action of a controller' do
2020-07-28 23:09:34 +05:30
expect(transaction.labels).to eq({ controller: 'TestController', action: 'show', feature_category: '' })
end
it 'contains only the labels defined for transactions' do
expect(transaction.labels.keys).to contain_exactly(*described_class.superclass::BASE_LABELS.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
2020-07-28 23:09:34 +05:30
expect(transaction.labels).to eq({ controller: 'TestController', action: 'show.json', feature_category: '' })
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
2020-07-28 23:09:34 +05:30
expect(transaction.labels).to eq({ controller: 'TestController', action: 'show', feature_category: '' })
end
end
context 'when the feature category is known' do
it 'includes it in the feature category label' do
expect(controller_class).to receive(:feature_category_for_action).with('show').and_return(:source_code_management)
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
2016-09-13 17:45:13 +05:30
describe '#add_event' do
it 'adds a metric' do
2020-05-24 23:13:21 +05:30
expect(prometheus_metric).to receive(:increment)
2016-09-13 17:45:13 +05:30
transaction.add_event(:meow)
end
it 'allows tracking of custom tags' do
2020-05-24 23:13:21 +05:30
expect(prometheus_metric).to receive(:increment).with(animal: "dog")
2016-09-13 17:45:13 +05:30
2020-05-24 23:13:21 +05:30
transaction.add_event(:bau, animal: 'dog')
2016-09-13 17:45:13 +05:30
end
end
end