diff options
Diffstat (limited to 'spec/lib/gitlab/metrics/subscribers')
-rw-r--r-- | spec/lib/gitlab/metrics/subscribers/active_record_spec.rb | 122 | ||||
-rw-r--r-- | spec/lib/gitlab/metrics/subscribers/rack_attack_spec.rb | 55 |
2 files changed, 163 insertions, 14 deletions
diff --git a/spec/lib/gitlab/metrics/subscribers/active_record_spec.rb b/spec/lib/gitlab/metrics/subscribers/active_record_spec.rb index 389b0ef1044..28c3ef229ab 100644 --- a/spec/lib/gitlab/metrics/subscribers/active_record_spec.rb +++ b/spec/lib/gitlab/metrics/subscribers/active_record_spec.rb @@ -10,6 +10,124 @@ RSpec.describe Gitlab::Metrics::Subscribers::ActiveRecord do let(:connection) { ActiveRecord::Base.retrieve_connection } let(:db_config_name) { ::Gitlab::Database.db_config_name(connection) } + describe '.load_balancing_metric_counter_keys' do + context 'multiple databases' do + before do + skip_if_multiple_databases_not_setup + end + + it 'has expected keys' do + expect(described_class.load_balancing_metric_counter_keys).to include( + :db_replica_count, + :db_primary_count, + :db_main_count, + :db_main_replica_count, + :db_ci_count, + :db_ci_replica_count, + :db_replica_cached_count, + :db_primary_cached_count, + :db_main_cached_count, + :db_main_replica_cached_count, + :db_ci_cached_count, + :db_ci_replica_cached_count, + :db_replica_wal_count, + :db_primary_wal_count, + :db_main_wal_count, + :db_main_replica_wal_count, + :db_ci_wal_count, + :db_ci_replica_wal_count, + :db_replica_wal_cached_count, + :db_primary_wal_cached_count, + :db_main_wal_cached_count, + :db_main_replica_wal_cached_count, + :db_ci_wal_cached_count, + :db_ci_replica_wal_cached_count + ) + end + end + + context 'single database' do + before do + skip_if_multiple_databases_are_setup + end + + it 'has expected keys' do + expect(described_class.load_balancing_metric_counter_keys).to include( + :db_replica_count, + :db_primary_count, + :db_main_count, + :db_main_replica_count, + :db_replica_cached_count, + :db_primary_cached_count, + :db_main_cached_count, + :db_main_replica_cached_count, + :db_replica_wal_count, + :db_primary_wal_count, + :db_main_wal_count, + :db_main_replica_wal_count, + :db_replica_wal_cached_count, + :db_primary_wal_cached_count, + :db_main_wal_cached_count, + :db_main_replica_wal_cached_count + ) + end + + it 'does not have ci keys' do + expect(described_class.load_balancing_metric_counter_keys).not_to include( + :db_ci_count, + :db_ci_replica_count, + :db_ci_cached_count, + :db_ci_replica_cached_count, + :db_ci_wal_count, + :db_ci_replica_wal_count, + :db_ci_wal_cached_count, + :db_ci_replica_wal_cached_count + ) + end + end + end + + describe '.load_balancing_metric_duration_keys' do + context 'multiple databases' do + before do + skip_if_multiple_databases_not_setup + end + + it 'has expected keys' do + expect(described_class.load_balancing_metric_duration_keys).to include( + :db_replica_duration_s, + :db_primary_duration_s, + :db_main_duration_s, + :db_main_replica_duration_s, + :db_ci_duration_s, + :db_ci_replica_duration_s + ) + end + end + + context 'single database' do + before do + skip_if_multiple_databases_are_setup + end + + it 'has expected keys' do + expect(described_class.load_balancing_metric_duration_keys).to include( + :db_replica_duration_s, + :db_primary_duration_s, + :db_main_duration_s, + :db_main_replica_duration_s + ) + end + + it 'does not have ci keys' do + expect(described_class.load_balancing_metric_duration_keys).not_to include( + :db_ci_duration_s, + :db_ci_replica_duration_s + ) + end + end + end + describe '#transaction' do let(:web_transaction) { double('Gitlab::Metrics::WebTransaction') } let(:background_transaction) { double('Gitlab::Metrics::WebTransaction') } @@ -37,7 +155,7 @@ RSpec.describe Gitlab::Metrics::Subscribers::ActiveRecord do end it 'captures the metrics for web only' do - expect(web_transaction).to receive(:observe).with(:gitlab_database_transaction_seconds, 0.23, db_config_name: db_config_name) + expect(web_transaction).to receive(:observe).with(:gitlab_database_transaction_seconds, 0.23, { db_config_name: db_config_name }) expect(background_transaction).not_to receive(:observe) expect(background_transaction).not_to receive(:increment) @@ -77,7 +195,7 @@ RSpec.describe Gitlab::Metrics::Subscribers::ActiveRecord do end it 'captures the metrics for web only' do - expect(background_transaction).to receive(:observe).with(:gitlab_database_transaction_seconds, 0.23, db_config_name: db_config_name) + expect(background_transaction).to receive(:observe).with(:gitlab_database_transaction_seconds, 0.23, { db_config_name: db_config_name }) expect(web_transaction).not_to receive(:observe) expect(web_transaction).not_to receive(:increment) diff --git a/spec/lib/gitlab/metrics/subscribers/rack_attack_spec.rb b/spec/lib/gitlab/metrics/subscribers/rack_attack_spec.rb index fda4b94bd78..9f939d0d7d6 100644 --- a/spec/lib/gitlab/metrics/subscribers/rack_attack_spec.rb +++ b/spec/lib/gitlab/metrics/subscribers/rack_attack_spec.rb @@ -77,8 +77,8 @@ RSpec.describe Gitlab::Metrics::Subscribers::RackAttack, :request_store do end it 'logs request information' do - expect(Gitlab::AuthLogger).to receive(:error).with( - include( + expect(Gitlab::AuthLogger).to receive(:error) do |arguments| + expect(arguments).to include( message: 'Rack_Attack', env: match_type, remote_ip: '1.2.3.4', @@ -86,7 +86,14 @@ RSpec.describe Gitlab::Metrics::Subscribers::RackAttack, :request_store do path: '/api/v4/internal/authorized_keys', matched: 'throttle_unauthenticated' ) - ) + + if expected_status + expect(arguments).to include(status: expected_status) + else + expect(arguments).not_to have_key(:status) + end + end + subscriber.send(match_type, event) end end @@ -111,8 +118,8 @@ RSpec.describe Gitlab::Metrics::Subscribers::RackAttack, :request_store do end it 'logs request information and user id' do - expect(Gitlab::AuthLogger).to receive(:error).with( - include( + expect(Gitlab::AuthLogger).to receive(:error) do |arguments| + expect(arguments).to include( message: 'Rack_Attack', env: match_type, remote_ip: '1.2.3.4', @@ -121,7 +128,14 @@ RSpec.describe Gitlab::Metrics::Subscribers::RackAttack, :request_store do matched: 'throttle_authenticated_api', user_id: non_existing_record_id ) - ) + + if expected_status + expect(arguments).to include(status: expected_status) + else + expect(arguments).not_to have_key(:status) + end + end + subscriber.send(match_type, event) end end @@ -145,8 +159,8 @@ RSpec.describe Gitlab::Metrics::Subscribers::RackAttack, :request_store do end it 'logs request information and user meta' do - expect(Gitlab::AuthLogger).to receive(:error).with( - include( + expect(Gitlab::AuthLogger).to receive(:error) do |arguments| + expect(arguments).to include( message: 'Rack_Attack', env: match_type, remote_ip: '1.2.3.4', @@ -156,7 +170,14 @@ RSpec.describe Gitlab::Metrics::Subscribers::RackAttack, :request_store do user_id: user.id, 'meta.user' => user.username ) - ) + + if expected_status + expect(arguments).to include(status: expected_status) + else + expect(arguments).not_to have_key(:status) + end + end + subscriber.send(match_type, event) end end @@ -182,8 +203,8 @@ RSpec.describe Gitlab::Metrics::Subscribers::RackAttack, :request_store do end it 'logs request information and user meta' do - expect(Gitlab::AuthLogger).to receive(:error).with( - include( + expect(Gitlab::AuthLogger).to receive(:error) do |arguments| + expect(arguments).to include( message: 'Rack_Attack', env: match_type, remote_ip: '1.2.3.4', @@ -192,7 +213,14 @@ RSpec.describe Gitlab::Metrics::Subscribers::RackAttack, :request_store do matched: 'throttle_authenticated_api', deploy_token_id: deploy_token.id ) - ) + + if expected_status + expect(arguments).to include(status: expected_status) + else + expect(arguments).not_to have_key(:status) + end + end + subscriber.send(match_type, event) end end @@ -202,6 +230,7 @@ RSpec.describe Gitlab::Metrics::Subscribers::RackAttack, :request_store do describe '#throttle' do let(:match_type) { :throttle } + let(:expected_status) { 429 } let(:event_name) { 'throttle.rack_attack' } it_behaves_like 'log into auth logger' @@ -209,6 +238,7 @@ RSpec.describe Gitlab::Metrics::Subscribers::RackAttack, :request_store do describe '#blocklist' do let(:match_type) { :blocklist } + let(:expected_status) { 403 } let(:event_name) { 'blocklist.rack_attack' } it_behaves_like 'log into auth logger' @@ -216,6 +246,7 @@ RSpec.describe Gitlab::Metrics::Subscribers::RackAttack, :request_store do describe '#track' do let(:match_type) { :track } + let(:expected_status) { nil } let(:event_name) { 'track.rack_attack' } it_behaves_like 'log into auth logger' |