diff options
Diffstat (limited to 'spec/lib/gitlab/metrics')
15 files changed, 276 insertions, 82 deletions
diff --git a/spec/lib/gitlab/metrics/dashboard/defaults_spec.rb b/spec/lib/gitlab/metrics/dashboard/defaults_spec.rb index 1f306753c39..b8556829a59 100644 --- a/spec/lib/gitlab/metrics/dashboard/defaults_spec.rb +++ b/spec/lib/gitlab/metrics/dashboard/defaults_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'spec_helper' +require 'fast_spec_helper' RSpec.describe Gitlab::Metrics::Dashboard::Defaults do it { is_expected.to be_const_defined(:DEFAULT_PANEL_TYPE) } diff --git a/spec/lib/gitlab/metrics/dashboard/importers/prometheus_metrics_spec.rb b/spec/lib/gitlab/metrics/dashboard/importers/prometheus_metrics_spec.rb index c15e717b126..bc6cd383758 100644 --- a/spec/lib/gitlab/metrics/dashboard/importers/prometheus_metrics_spec.rb +++ b/spec/lib/gitlab/metrics/dashboard/importers/prometheus_metrics_spec.rb @@ -24,13 +24,13 @@ RSpec.describe Gitlab::Metrics::Dashboard::Importers::PrometheusMetrics do context 'with existing metrics' do let(:existing_metric_attributes) do { - project: project, - identifier: 'metric_b', - title: 'overwrite', - y_label: 'overwrite', - query: 'overwrite', - unit: 'overwrite', - legend: 'overwrite', + project: project, + identifier: 'metric_b', + title: 'overwrite', + y_label: 'overwrite', + query: 'overwrite', + unit: 'overwrite', + legend: 'overwrite', dashboard_path: dashboard_path } end @@ -43,11 +43,11 @@ RSpec.describe Gitlab::Metrics::Dashboard::Importers::PrometheusMetrics do subject.execute expect(existing_metric.reload.attributes.with_indifferent_access).to include({ - title: 'Super Chart B', + title: 'Super Chart B', y_label: 'y_label', - query: 'query', - unit: 'unit', - legend: 'Legend Label' + query: 'query', + unit: 'unit', + legend: 'Legend Label' }) end @@ -69,11 +69,11 @@ RSpec.describe Gitlab::Metrics::Dashboard::Importers::PrometheusMetrics do subject.execute expect(existing_metric.reload.attributes.with_indifferent_access).to include({ - title: 'Super Chart B', + title: 'Super Chart B', y_label: 'y_label', - query: 'query', - unit: 'unit', - legend: 'Legend Label' + query: 'query', + unit: 'unit', + legend: 'Legend Label' }) end diff --git a/spec/lib/gitlab/metrics/dashboard/validator/errors_spec.rb b/spec/lib/gitlab/metrics/dashboard/validator/errors_spec.rb index fdbba6c31b5..a50c2a506cb 100644 --- a/spec/lib/gitlab/metrics/dashboard/validator/errors_spec.rb +++ b/spec/lib/gitlab/metrics/dashboard/validator/errors_spec.rb @@ -17,11 +17,11 @@ RSpec.describe Gitlab::Metrics::Dashboard::Validator::Errors do let(:error_hash) do { - 'data' => 'property_name', + 'data' => 'property_name', 'data_pointer' => pointer, - 'type' => type, - 'schema' => 'schema', - 'details' => details + 'type' => type, + 'schema' => 'schema', + 'details' => details } end @@ -72,10 +72,10 @@ RSpec.describe Gitlab::Metrics::Dashboard::Validator::Errors do let(:type) { 'pattern' } let(:error_hash) do { - 'data' => 'property_name', + 'data' => 'property_name', 'data_pointer' => pointer, - 'type' => type, - 'schema' => { 'pattern' => 'aa.*' } + 'type' => type, + 'schema' => { 'pattern' => 'aa.*' } } end @@ -86,10 +86,10 @@ RSpec.describe Gitlab::Metrics::Dashboard::Validator::Errors do let(:type) { 'format' } let(:error_hash) do { - 'data' => 'property_name', + 'data' => 'property_name', 'data_pointer' => pointer, - 'type' => type, - 'schema' => { 'format' => 'date-time' } + 'type' => type, + 'schema' => { 'format' => 'date-time' } } end @@ -100,10 +100,10 @@ RSpec.describe Gitlab::Metrics::Dashboard::Validator::Errors do let(:type) { 'const' } let(:error_hash) do { - 'data' => 'property_name', + 'data' => 'property_name', 'data_pointer' => pointer, - 'type' => type, - 'schema' => { 'const' => 'one' } + 'type' => type, + 'schema' => { 'const' => 'one' } } end @@ -114,10 +114,10 @@ RSpec.describe Gitlab::Metrics::Dashboard::Validator::Errors do let(:type) { 'enum' } let(:error_hash) do { - 'data' => 'property_name', + 'data' => 'property_name', 'data_pointer' => pointer, - 'type' => type, - 'schema' => { 'enum' => %w(one two) } + 'type' => type, + 'schema' => { 'enum' => %w(one two) } } end @@ -128,10 +128,10 @@ RSpec.describe Gitlab::Metrics::Dashboard::Validator::Errors do let(:type) { 'unknown' } let(:error_hash) do { - 'data' => 'property_name', + 'data' => 'property_name', 'data_pointer' => pointer, - 'type' => type, - 'schema' => 'schema' + 'type' => type, + 'schema' => 'schema' } end diff --git a/spec/lib/gitlab/metrics/dashboard/validator_spec.rb b/spec/lib/gitlab/metrics/dashboard/validator_spec.rb index eb67ea2b7da..aaa9daf8fee 100644 --- a/spec/lib/gitlab/metrics/dashboard/validator_spec.rb +++ b/spec/lib/gitlab/metrics/dashboard/validator_spec.rb @@ -33,9 +33,9 @@ RSpec.describe Gitlab::Metrics::Dashboard::Validator do context 'with metric identifier present in current dashboard' do before do create(:prometheus_metric, - identifier: 'metric_a1', + identifier: 'metric_a1', dashboard_path: 'test/path.yml', - project: project + project: project ) end @@ -45,9 +45,9 @@ RSpec.describe Gitlab::Metrics::Dashboard::Validator do context 'with metric identifier present in another dashboard' do before do create(:prometheus_metric, - identifier: 'metric_a1', + identifier: 'metric_a1', dashboard_path: 'some/other/dashboard/path.yml', - project: project + project: project ) end @@ -94,9 +94,9 @@ RSpec.describe Gitlab::Metrics::Dashboard::Validator do context 'with metric identifier present in current dashboard' do before do create(:prometheus_metric, - identifier: 'metric_a1', + identifier: 'metric_a1', dashboard_path: 'test/path.yml', - project: project + project: project ) end @@ -106,9 +106,9 @@ RSpec.describe Gitlab::Metrics::Dashboard::Validator do context 'with metric identifier present in another dashboard' do before do create(:prometheus_metric, - identifier: 'metric_a1', + identifier: 'metric_a1', dashboard_path: 'some/other/dashboard/path.yml', - project: project + project: project ) end @@ -166,9 +166,9 @@ RSpec.describe Gitlab::Metrics::Dashboard::Validator do context 'with metric identifier present in current dashboard' do before do create(:prometheus_metric, - identifier: 'metric_a1', + identifier: 'metric_a1', dashboard_path: 'test/path.yml', - project: project + project: project ) end @@ -178,9 +178,9 @@ RSpec.describe Gitlab::Metrics::Dashboard::Validator do context 'with metric identifier present in another dashboard' do before do create(:prometheus_metric, - identifier: 'metric_a1', + identifier: 'metric_a1', dashboard_path: 'some/other/dashboard/path.yml', - project: project + project: project ) end diff --git a/spec/lib/gitlab/metrics/delta_spec.rb b/spec/lib/gitlab/metrics/delta_spec.rb index e768da875c2..fdbb5e4ce4d 100644 --- a/spec/lib/gitlab/metrics/delta_spec.rb +++ b/spec/lib/gitlab/metrics/delta_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'spec_helper' +require 'fast_spec_helper' RSpec.describe Gitlab::Metrics::Delta do let(:delta) { described_class.new } diff --git a/spec/lib/gitlab/metrics/exporter/base_exporter_spec.rb b/spec/lib/gitlab/metrics/exporter/base_exporter_spec.rb index dc5c7eb2e55..fa50adb4e4f 100644 --- a/spec/lib/gitlab/metrics/exporter/base_exporter_spec.rb +++ b/spec/lib/gitlab/metrics/exporter/base_exporter_spec.rb @@ -10,11 +10,12 @@ RSpec.describe Gitlab::Metrics::Exporter::BaseExporter do describe 'when exporter is enabled' do before do allow(::WEBrick::HTTPServer).to receive(:new).with( - Port: anything, - BindAddress: anything, - Logger: anything, - AccessLog: anything - ).and_call_original + { + Port: anything, + BindAddress: anything, + Logger: anything, + AccessLog: anything + }).and_call_original allow(settings).to receive(:enabled).and_return(true) allow(settings).to receive(:port).and_return(0) @@ -45,11 +46,12 @@ RSpec.describe Gitlab::Metrics::Exporter::BaseExporter do it 'starts server with port and address from settings' do expect(::WEBrick::HTTPServer).to receive(:new).with( - Port: port, - BindAddress: address, - Logger: anything, - AccessLog: anything - ).and_wrap_original do |m, *args| + { + Port: port, + BindAddress: address, + Logger: anything, + AccessLog: anything + }).and_wrap_original do |m, *args| m.call(DoNotListen: true, Logger: args.first[:Logger]) end diff --git a/spec/lib/gitlab/metrics/global_search_slis_spec.rb b/spec/lib/gitlab/metrics/global_search_slis_spec.rb new file mode 100644 index 00000000000..28496eff2fc --- /dev/null +++ b/spec/lib/gitlab/metrics/global_search_slis_spec.rb @@ -0,0 +1,173 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Metrics::GlobalSearchSlis do + using RSpec::Parameterized::TableSyntax + + let(:apdex_feature_flag_enabled) { true } + let(:error_rate_feature_flag_enabled) { true } + + before do + stub_feature_flags(global_search_custom_slis: apdex_feature_flag_enabled) + stub_feature_flags(global_search_error_rate_sli: error_rate_feature_flag_enabled) + end + + describe '#initialize_slis!' do + context 'when global_search_custom_slis feature flag is enabled' do + let(:apdex_feature_flag_enabled) { true } + + it 'initializes Apdex SLIs for global_search' do + expect(Gitlab::Metrics::Sli::Apdex).to receive(:initialize_sli).with( + :global_search, + a_kind_of(Array) + ) + + described_class.initialize_slis! + end + end + + context 'when global_search_error_rate_sli feature flag is enabled' do + let(:error_rate_feature_flag_enabled) { true } + + it 'initializes ErrorRate SLIs for global_search' do + expect(Gitlab::Metrics::Sli::ErrorRate).to receive(:initialize_sli).with( + :global_search, + a_kind_of(Array) + ) + + described_class.initialize_slis! + end + end + + context 'when global_search_custom_slis feature flag is disabled' do + let(:apdex_feature_flag_enabled) { false } + + it 'does not initialize the Apdex SLIs for global_search' do + expect(Gitlab::Metrics::Sli::Apdex).not_to receive(:initialize_sli) + + described_class.initialize_slis! + end + end + + context 'when global_search_error_rate_sli feature flag is disabled' do + let(:error_rate_feature_flag_enabled) { false } + + it 'does not initialize the ErrorRate SLIs for global_search' do + expect(Gitlab::Metrics::Sli::ErrorRate).not_to receive(:initialize_sli) + + described_class.initialize_slis! + end + end + end + + describe '#record_apdex' do + context 'when global_search_custom_slis feature flag is enabled' do + let(:apdex_feature_flag_enabled) { true } + + where(:search_type, :code_search, :duration_target) do + 'basic' | false | 7.031 + 'basic' | true | 21.903 + 'advanced' | false | 4.865 + 'advanced' | true | 13.546 + end + + with_them do + before do + allow(::Gitlab::ApplicationContext).to receive(:current_context_attribute).with(:caller_id).and_return('end') + end + + let(:search_scope) { code_search ? 'blobs' : 'issues' } + + it 'increments the global_search SLI as a success if the elapsed time is within the target' do + duration = duration_target - 0.1 + + expect(Gitlab::Metrics::Sli::Apdex[:global_search]).to receive(:increment).with( + labels: { + search_type: search_type, + search_level: 'global', + search_scope: search_scope, + endpoint_id: 'end' + }, + success: true + ) + + described_class.record_apdex( + elapsed: duration, + search_type: search_type, + search_level: 'global', + search_scope: search_scope + ) + end + + it 'increments the global_search SLI as a failure if the elapsed time is not within the target' do + duration = duration_target + 0.1 + + expect(Gitlab::Metrics::Sli::Apdex[:global_search]).to receive(:increment).with( + labels: { + search_type: search_type, + search_level: 'global', + search_scope: search_scope, + endpoint_id: 'end' + }, + success: false + ) + + described_class.record_apdex( + elapsed: duration, + search_type: search_type, + search_level: 'global', + search_scope: search_scope + ) + end + end + end + + context 'when global_search_custom_slis feature flag is disabled' do + let(:apdex_feature_flag_enabled) { false } + + it 'does not call increment on the apdex SLI' do + expect(Gitlab::Metrics::Sli::Apdex[:global_search]).not_to receive(:increment) + + described_class.record_apdex( + elapsed: 1, + search_type: 'basic', + search_level: 'global', + search_scope: 'issues' + ) + end + end + end + + describe '#record_error_rate' do + context 'when global_search_error_rate_sli feature flag is enabled' do + let(:error_rate_feature_flag_enabled) { true } + + it 'calls increment on the error rate SLI' do + expect(Gitlab::Metrics::Sli::ErrorRate[:global_search]).to receive(:increment) + + described_class.record_error_rate( + error: true, + search_type: 'basic', + search_level: 'global', + search_scope: 'issues' + ) + end + end + + context 'when global_search_error_rate_sli feature flag is disabled' do + let(:error_rate_feature_flag_enabled) { false } + + it 'does not call increment on the error rate SLI' do + expect(Gitlab::Metrics::Sli::ErrorRate[:global_search]).not_to receive(:increment) + + described_class.record_error_rate( + error: true, + search_type: 'basic', + search_level: 'global', + search_scope: 'issues' + ) + end + end + end +end diff --git a/spec/lib/gitlab/metrics/rack_middleware_spec.rb b/spec/lib/gitlab/metrics/rack_middleware_spec.rb index ab56f38f0c1..21028d18648 100644 --- a/spec/lib/gitlab/metrics/rack_middleware_spec.rb +++ b/spec/lib/gitlab/metrics/rack_middleware_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'spec_helper' +require 'fast_spec_helper' RSpec.describe Gitlab::Metrics::RackMiddleware do let(:app) { double(:app) } diff --git a/spec/lib/gitlab/metrics/requests_rack_middleware_spec.rb b/spec/lib/gitlab/metrics/requests_rack_middleware_spec.rb index 3396de9b12c..ed78548ef62 100644 --- a/spec/lib/gitlab/metrics/requests_rack_middleware_spec.rb +++ b/spec/lib/gitlab/metrics/requests_rack_middleware_spec.rb @@ -194,9 +194,8 @@ RSpec.describe Gitlab::Metrics::RequestsRackMiddleware, :aggregate_failures do let(:endpoint) do route = double(:route, request_method: 'GET', path: '/:version/projects/:id/archive(.:format)') - double(:endpoint, route: route, - options: { for: api_handler, path: [":id/archive"] }, - namespace: "/projects") + double(:endpoint, + route: route, options: { for: api_handler, path: [":id/archive"] }, namespace: "/projects") end let(:env) { { 'api.endpoint' => endpoint, 'REQUEST_METHOD' => 'GET' } } @@ -256,9 +255,8 @@ RSpec.describe Gitlab::Metrics::RequestsRackMiddleware, :aggregate_failures do context 'Grape API without expected duration' do let(:endpoint) do route = double(:route, request_method: 'GET', path: '/:version/projects/:id/archive(.:format)') - double(:endpoint, route: route, - options: { for: api_handler, path: [":id/archive"] }, - namespace: "/projects") + double(:endpoint, + route: route, options: { for: api_handler, path: [":id/archive"] }, namespace: "/projects") end let(:env) { { 'api.endpoint' => endpoint, 'REQUEST_METHOD' => 'GET' } } diff --git a/spec/lib/gitlab/metrics/subscribers/action_view_spec.rb b/spec/lib/gitlab/metrics/subscribers/action_view_spec.rb index adbc474343f..67cd8630758 100644 --- a/spec/lib/gitlab/metrics/subscribers/action_view_spec.rb +++ b/spec/lib/gitlab/metrics/subscribers/action_view_spec.rb @@ -12,7 +12,7 @@ RSpec.describe Gitlab::Metrics::Subscribers::ActionView do root = Rails.root.to_s double(:event, duration: 2.1, - payload: { identifier: "#{root}/app/views/x.html.haml" }) + payload: { identifier: "#{root}/app/views/x.html.haml" }) end before do diff --git a/spec/lib/gitlab/metrics/subscribers/active_record_spec.rb b/spec/lib/gitlab/metrics/subscribers/active_record_spec.rb index 28c3ef229ab..005c1ae2d0a 100644 --- a/spec/lib/gitlab/metrics/subscribers/active_record_spec.rb +++ b/spec/lib/gitlab/metrics/subscribers/active_record_spec.rb @@ -137,7 +137,7 @@ RSpec.describe Gitlab::Metrics::Subscribers::ActiveRecord do :event, name: 'transaction.active_record', duration: 230, - payload: { connection: connection } + payload: { connection: connection } ) end @@ -213,7 +213,7 @@ RSpec.describe Gitlab::Metrics::Subscribers::ActiveRecord do :event, name: 'sql.active_record', duration: 2, - payload: payload + payload: payload ) end @@ -278,7 +278,7 @@ RSpec.describe Gitlab::Metrics::Subscribers::ActiveRecord do :event, name: 'sql.active_record', duration: 2, - payload: payload + payload: payload ) end diff --git a/spec/lib/gitlab/metrics/subscribers/load_balancing_spec.rb b/spec/lib/gitlab/metrics/subscribers/load_balancing_spec.rb index bc6effd0438..7f7efaffd9e 100644 --- a/spec/lib/gitlab/metrics/subscribers/load_balancing_spec.rb +++ b/spec/lib/gitlab/metrics/subscribers/load_balancing_spec.rb @@ -15,7 +15,7 @@ RSpec.describe Gitlab::Metrics::Subscribers::LoadBalancing, :request_store do double( :event, name: 'load_balancing.caught_up_replica_pick', - payload: payload + payload: payload ) end @@ -37,7 +37,7 @@ RSpec.describe Gitlab::Metrics::Subscribers::LoadBalancing, :request_store do double( :event, name: 'load_balancing.web_transaction_completed', - payload: {} + payload: {} ) end diff --git a/spec/lib/gitlab/metrics/system_spec.rb b/spec/lib/gitlab/metrics/system_spec.rb index ce3caf8cdfe..7739501dd95 100644 --- a/spec/lib/gitlab/metrics/system_spec.rb +++ b/spec/lib/gitlab/metrics/system_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'spec_helper' +require 'fast_spec_helper' RSpec.describe Gitlab::Metrics::System do context 'when /proc files exist' do @@ -72,10 +72,20 @@ RSpec.describe Gitlab::Metrics::System do end describe '.memory_usage_rss' do - it "returns the process' resident set size (RSS) in bytes" do - mock_existing_proc_file('/proc/self/status', proc_status) + context 'without PID' do + it "returns the current process' resident set size (RSS) in bytes" do + mock_existing_proc_file('/proc/self/status', proc_status) + + expect(described_class.memory_usage_rss).to eq(2527232) + end + end + + context 'with PID' do + it "returns the given process' resident set size (RSS) in bytes" do + mock_existing_proc_file('/proc/7/status', proc_status) - expect(described_class.memory_usage_rss).to eq(2527232) + expect(described_class.memory_usage_rss(pid: 7)).to eq(2527232) + end end end @@ -96,11 +106,22 @@ RSpec.describe Gitlab::Metrics::System do end describe '.memory_usage_uss_pss' do - it "returns the process' unique and porportional set size (USS/PSS) in bytes" do - mock_existing_proc_file('/proc/self/smaps_rollup', proc_smaps_rollup) + context 'without PID' do + it "returns the current process' unique and porportional set size (USS/PSS) in bytes" do + mock_existing_proc_file('/proc/self/smaps_rollup', proc_smaps_rollup) + + # (Private_Clean (152 kB) + Private_Dirty (312 kB) + Private_Hugetlb (0 kB)) * 1024 + expect(described_class.memory_usage_uss_pss).to eq(uss: 475136, pss: 515072) + end + end + + context 'with PID' do + it "returns the given process' unique and porportional set size (USS/PSS) in bytes" do + mock_existing_proc_file('/proc/7/smaps_rollup', proc_smaps_rollup) - # (Private_Clean (152 kB) + Private_Dirty (312 kB) + Private_Hugetlb (0 kB)) * 1024 - expect(described_class.memory_usage_uss_pss).to eq(uss: 475136, pss: 515072) + # (Private_Clean (152 kB) + Private_Dirty (312 kB) + Private_Hugetlb (0 kB)) * 1024 + expect(described_class.memory_usage_uss_pss(pid: 7)).to eq(uss: 475136, pss: 515072) + end end end diff --git a/spec/lib/gitlab/metrics/transaction_spec.rb b/spec/lib/gitlab/metrics/transaction_spec.rb index b1c15db5193..1a8538b5d6a 100644 --- a/spec/lib/gitlab/metrics/transaction_spec.rb +++ b/spec/lib/gitlab/metrics/transaction_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'spec_helper' +require 'fast_spec_helper' RSpec.describe Gitlab::Metrics::Transaction do describe '#run' do diff --git a/spec/lib/gitlab/metrics/web_transaction_spec.rb b/spec/lib/gitlab/metrics/web_transaction_spec.rb index d6590efcf4f..dc59fa804c4 100644 --- a/spec/lib/gitlab/metrics/web_transaction_spec.rb +++ b/spec/lib/gitlab/metrics/web_transaction_spec.rb @@ -66,8 +66,8 @@ RSpec.describe Gitlab::Metrics::WebTransaction do before do route = double(:route, request_method: 'GET', path: '/:version/projects/:id/archive(.:format)') endpoint = double(:endpoint, route: route, - options: { for: API::Projects, path: [":id/archive"] }, - namespace: "/projects") + options: { for: API::Projects, path: [":id/archive"] }, + namespace: "/projects") env['api.endpoint'] = endpoint |