2020-05-24 23:13:21 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::Metrics::Samplers::DatabaseSampler do
|
2020-06-23 00:09:42 +05:30
|
|
|
subject { described_class.new }
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
it_behaves_like 'metrics sampler', 'DATABASE_SAMPLER'
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
describe '#sample' do
|
|
|
|
before do
|
|
|
|
described_class::METRIC_DESCRIPTIONS.each_key do |metric|
|
|
|
|
allow(subject.metrics[metric]).to receive(:set)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for ActiveRecord::Base' do
|
|
|
|
let(:labels) do
|
|
|
|
{
|
|
|
|
class: 'ActiveRecord::Base',
|
|
|
|
host: Gitlab::Database.config['host'],
|
|
|
|
port: Gitlab::Database.config['port']
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the database is connected' do
|
|
|
|
it 'samples connection pool statistics' do
|
|
|
|
expect(subject.metrics[:size]).to receive(:set).with(labels, a_value >= 1)
|
|
|
|
expect(subject.metrics[:connections]).to receive(:set).with(labels, a_value >= 1)
|
|
|
|
expect(subject.metrics[:busy]).to receive(:set).with(labels, a_value >= 1)
|
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the database is not connected' do
|
|
|
|
before do
|
|
|
|
allow(ActiveRecord::Base).to receive(:connected?).and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'records no samples' do
|
|
|
|
expect(subject.metrics[:size]).not_to receive(:set).with(labels, anything)
|
|
|
|
|
|
|
|
subject.sample
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|