Commit 9b322e4f authored by pbair's avatar pbair

Collect database host metrics for all primaries

Update the DatabaseSampler to collect metrics for all primaries defined
in Gitlab::Database.database_base_models, rather than ActiveRecord::Base
only.
parent 7acd6ef4
...@@ -38,16 +38,19 @@ module Gitlab ...@@ -38,16 +38,19 @@ module Gitlab
end end
def host_stats def host_stats
return [] unless ActiveRecord::Base.connected? Gitlab::Database.database_base_models.each_value.with_object([]) do |base_model, stats|
next unless base_model.connected?
[{ labels: labels_for_class(ActiveRecord::Base), stats: ActiveRecord::Base.connection_pool.stat }] stats << { labels: labels_for_class(base_model), stats: base_model.connection_pool.stat }
end
end end
def labels_for_class(klass) def labels_for_class(klass)
{ {
host: klass.connection_db_config.host, host: klass.connection_db_config.host,
port: klass.connection_db_config.configuration_hash[:port], port: klass.connection_db_config.configuration_hash[:port],
class: klass.to_s class: klass.to_s,
db_config_name: klass.connection_db_config.name
} }
end end
end end
......
...@@ -8,43 +8,61 @@ RSpec.describe Gitlab::Metrics::Samplers::DatabaseSampler do ...@@ -8,43 +8,61 @@ RSpec.describe Gitlab::Metrics::Samplers::DatabaseSampler do
it_behaves_like 'metrics sampler', 'DATABASE_SAMPLER' it_behaves_like 'metrics sampler', 'DATABASE_SAMPLER'
describe '#sample' do describe '#sample' do
let(:active_record_labels) do
{
class: 'ActiveRecord::Base',
host: ApplicationRecord.database.config['host'],
port: ApplicationRecord.database.config['port'],
db_config_name: 'main'
}
end
let(:ci_application_record_labels) do
{
class: 'Ci::ApplicationRecord',
host: Ci::ApplicationRecord.database.config['host'],
port: Ci::ApplicationRecord.database.config['port'],
db_config_name: 'ci'
}
end
before do before do
described_class::METRIC_DESCRIPTIONS.each_key do |metric| described_class::METRIC_DESCRIPTIONS.each_key do |metric|
allow(subject.metrics[metric]).to receive(:set) allow(subject.metrics[metric]).to receive(:set)
end end
allow(Gitlab::Database).to receive(:database_base_models)
.and_return({ main: ActiveRecord::Base, ci: Ci::ApplicationRecord })
end end
context 'for ActiveRecord::Base' do context 'when the database is connected', :add_ci_connection do
let(:labels) do it 'samples connection pool statistics' do
{ expect(subject.metrics[:size]).to receive(:set).with(active_record_labels, a_value >= 1)
class: 'ActiveRecord::Base', expect(subject.metrics[:connections]).to receive(:set).with(active_record_labels, a_value >= 1)
host: ApplicationRecord.database.config['host'], expect(subject.metrics[:busy]).to receive(:set).with(active_record_labels, a_value >= 1)
port: ApplicationRecord.database.config['port'] expect(subject.metrics[:dead]).to receive(:set).with(active_record_labels, a_value >= 0)
} expect(subject.metrics[:waiting]).to receive(:set).with(active_record_labels, a_value >= 0)
end
context 'when the database is connected' do expect(subject.metrics[:size]).to receive(:set).with(ci_application_record_labels, a_value >= 1)
it 'samples connection pool statistics' do expect(subject.metrics[:connections]).to receive(:set).with(ci_application_record_labels, a_value >= 1)
expect(subject.metrics[:size]).to receive(:set).with(labels, a_value >= 1) expect(subject.metrics[:busy]).to receive(:set).with(ci_application_record_labels, a_value >= 1)
expect(subject.metrics[:connections]).to receive(:set).with(labels, a_value >= 1) expect(subject.metrics[:dead]).to receive(:set).with(ci_application_record_labels, a_value >= 0)
expect(subject.metrics[:busy]).to receive(:set).with(labels, a_value >= 1) expect(subject.metrics[:waiting]).to receive(:set).with(ci_application_record_labels, a_value >= 0)
expect(subject.metrics[:dead]).to receive(:set).with(labels, a_value >= 0)
expect(subject.metrics[:waiting]).to receive(:set).with(labels, a_value >= 0)
subject.sample subject.sample
end
end end
end
context 'when the database is not connected' do context 'when a database is not connected', :add_ci_connection do
before do before do
allow(ActiveRecord::Base).to receive(:connected?).and_return(false) allow(Ci::ApplicationRecord).to receive(:connected?).and_return(false)
end end
it 'records no samples' do it 'records no samples for that database' do
expect(subject.metrics[:size]).not_to receive(:set).with(labels, anything) expect(subject.metrics[:size]).to receive(:set).with(active_record_labels, anything)
expect(subject.metrics[:size]).not_to receive(:set).with(ci_application_record_labels, anything)
subject.sample subject.sample
end
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment