2022-01-26 12:08:38 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
require 'spec_helper'
|
2022-01-26 12:08:38 +05:30
|
|
|
|
|
|
|
require_relative '../../metrics_server/metrics_server'
|
|
|
|
|
|
|
|
RSpec.describe MetricsServer do # rubocop:disable RSpec/FilePath
|
2022-03-02 08:16:31 +05:30
|
|
|
let(:prometheus_config) { ::Prometheus::Client.configuration }
|
|
|
|
let(:metrics_dir) { Dir.mktmpdir }
|
|
|
|
|
|
|
|
# Prometheus::Client is a singleton, i.e. shared global state, so
|
|
|
|
# we need to reset it after testing.
|
|
|
|
let!(:old_multiprocess_files_dir) { prometheus_config.multiprocess_files_dir }
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
let(:ruby_sampler_double) { double(Gitlab::Metrics::Samplers::RubySampler) }
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
before do
|
|
|
|
# We do not want this to have knock-on effects on the test process.
|
|
|
|
allow(Gitlab::ProcessManagement).to receive(:modify_signals)
|
2022-04-04 11:22:00 +05:30
|
|
|
|
|
|
|
# This being a singleton, we stub it out because only one instance is allowed
|
|
|
|
# to exist per process.
|
|
|
|
allow(Gitlab::Metrics::Samplers::RubySampler).to receive(:initialize_instance).and_return(ruby_sampler_double)
|
|
|
|
allow(ruby_sampler_double).to receive(:start)
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
after do
|
|
|
|
Gitlab::Metrics.reset_registry!
|
|
|
|
prometheus_config.multiprocess_files_dir = old_multiprocess_files_dir
|
|
|
|
|
|
|
|
FileUtils.rm_rf(metrics_dir, secure: true)
|
|
|
|
end
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
%w(puma sidekiq).each do |target|
|
|
|
|
context "when targeting #{target}" do
|
|
|
|
describe '.fork' do
|
|
|
|
context 'when in parent process' do
|
|
|
|
it 'forks into a new process and detaches it' do
|
|
|
|
expect(Process).to receive(:fork).and_return(99)
|
|
|
|
expect(Process).to receive(:detach).with(99)
|
2022-01-26 12:08:38 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
described_class.fork(target, metrics_dir: metrics_dir)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when in child process' do
|
|
|
|
before do
|
|
|
|
# This signals the process that it's "inside" the fork
|
|
|
|
expect(Process).to receive(:fork).and_return(nil)
|
|
|
|
expect(Process).not_to receive(:detach)
|
|
|
|
end
|
2022-01-26 12:08:38 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
it 'starts the metrics server with the given arguments' do
|
|
|
|
expect_next_instance_of(MetricsServer) do |server|
|
|
|
|
expect(server).to receive(:start)
|
|
|
|
end
|
|
|
|
|
|
|
|
described_class.fork(target, metrics_dir: metrics_dir)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'resets signal handlers from parent process' do
|
|
|
|
expect(Gitlab::ProcessManagement).to receive(:modify_signals).with(%i[A B], 'DEFAULT')
|
|
|
|
|
|
|
|
described_class.fork(target, metrics_dir: metrics_dir, reset_signals: %i[A B])
|
|
|
|
end
|
|
|
|
end
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
describe '.spawn' do
|
|
|
|
let(:expected_env) do
|
|
|
|
{
|
|
|
|
'METRICS_SERVER_TARGET' => target,
|
|
|
|
'WIPE_METRICS_DIR' => '0'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'spawns a new server process and returns its PID' do
|
|
|
|
expect(Process).to receive(:spawn).with(
|
|
|
|
expected_env,
|
|
|
|
end_with('bin/metrics-server'),
|
|
|
|
hash_including(pgroup: true)
|
|
|
|
).and_return(99)
|
|
|
|
expect(Process).to receive(:detach).with(99)
|
|
|
|
|
|
|
|
pid = described_class.spawn(target, metrics_dir: metrics_dir)
|
|
|
|
|
|
|
|
expect(pid).to eq(99)
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
context 'when path to gitlab.yml is passed' do
|
|
|
|
it 'sets the GITLAB_CONFIG environment variable' do
|
|
|
|
expect(Process).to receive(:spawn).with(
|
|
|
|
expected_env.merge('GITLAB_CONFIG' => 'path/to/config/gitlab.yml'),
|
|
|
|
end_with('bin/metrics-server'),
|
|
|
|
hash_including(pgroup: true)
|
|
|
|
).and_return(99)
|
|
|
|
|
|
|
|
described_class.spawn(target, metrics_dir: metrics_dir, gitlab_config: 'path/to/config/gitlab.yml')
|
|
|
|
end
|
|
|
|
end
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
2022-04-04 11:22:00 +05:30
|
|
|
end
|
|
|
|
end
|
2022-01-26 12:08:38 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
context 'when targeting invalid target' do
|
|
|
|
describe '.fork' do
|
|
|
|
it 'raises an error' do
|
|
|
|
expect { described_class.fork('unsupported', metrics_dir: metrics_dir) }.to(
|
|
|
|
raise_error('Target must be one of [puma,sidekiq]')
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2022-01-26 12:08:38 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
describe '.spawn' do
|
|
|
|
it 'raises an error' do
|
|
|
|
expect { described_class.spawn('unsupported', metrics_dir: metrics_dir) }.to(
|
|
|
|
raise_error('Target must be one of [puma,sidekiq]')
|
|
|
|
)
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
shared_examples 'a metrics exporter' do |target, expected_name|
|
|
|
|
describe '#start' do
|
|
|
|
let(:exporter_double) { double('exporter', start: true) }
|
|
|
|
let(:wipe_metrics_dir) { true }
|
2022-01-26 12:08:38 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
subject(:metrics_server) { described_class.new(target, metrics_dir, wipe_metrics_dir) }
|
2022-01-26 12:08:38 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
it 'configures ::Prometheus::Client' do
|
|
|
|
metrics_server.start
|
2022-01-26 12:08:38 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
expect(prometheus_config.multiprocess_files_dir).to eq metrics_dir
|
|
|
|
expect(::Prometheus::Client.configuration.pid_provider.call).to eq expected_name
|
|
|
|
end
|
2022-01-26 12:08:38 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
it 'ensures that metrics directory exists in correct mode (0700)' do
|
|
|
|
expect(FileUtils).to receive(:mkdir_p).with(metrics_dir, mode: 0700)
|
2022-01-26 12:08:38 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
metrics_server.start
|
|
|
|
end
|
2022-01-26 12:08:38 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
context 'when wipe_metrics_dir is true' do
|
|
|
|
it 'removes any old metrics files' do
|
|
|
|
FileUtils.touch("#{metrics_dir}/remove_this.db")
|
2022-01-26 12:08:38 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
expect { metrics_server.start }.to change { Dir.empty?(metrics_dir) }.from(false).to(true)
|
|
|
|
end
|
|
|
|
end
|
2022-01-26 12:08:38 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
context 'when wipe_metrics_dir is false' do
|
|
|
|
let(:wipe_metrics_dir) { false }
|
2022-01-26 12:08:38 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
it 'does not remove any old metrics files' do
|
|
|
|
FileUtils.touch("#{metrics_dir}/remove_this.db")
|
2022-01-26 12:08:38 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
expect { metrics_server.start }.not_to change { Dir.empty?(metrics_dir) }.from(false)
|
|
|
|
end
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
it 'starts a metrics server' do
|
|
|
|
expect(exporter_double).to receive(:start)
|
|
|
|
|
|
|
|
metrics_server.start
|
|
|
|
end
|
2022-01-26 12:08:38 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
it 'starts a RubySampler instance' do
|
|
|
|
expect(ruby_sampler_double).to receive(:start)
|
2022-01-26 12:08:38 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
subject.start
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
describe '#name' do
|
|
|
|
let(:exporter_double) { double('exporter', start: true) }
|
2022-01-26 12:08:38 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
subject(:name) { described_class.new(target, metrics_dir, true).name }
|
|
|
|
|
|
|
|
it { is_expected.to eq(expected_name) }
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
2022-04-04 11:22:00 +05:30
|
|
|
end
|
2022-03-02 08:16:31 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
context 'for puma' do
|
|
|
|
before do
|
|
|
|
allow(Gitlab::Metrics::Exporter::WebExporter).to receive(:instance).with(
|
|
|
|
gc_requests: true, synchronous: true
|
|
|
|
).and_return(exporter_double)
|
|
|
|
end
|
2022-03-02 08:16:31 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
it_behaves_like 'a metrics exporter', 'puma', 'web_exporter'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for sidekiq' do
|
|
|
|
let(:settings) { { "sidekiq_exporter" => { "enabled" => true } } }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(::Settings).to receive(:monitoring).and_return(settings)
|
|
|
|
allow(Gitlab::Metrics::Exporter::SidekiqExporter).to receive(:instance).with(
|
|
|
|
settings['sidekiq_exporter'], gc_requests: true, synchronous: true
|
|
|
|
).and_return(exporter_double)
|
2022-03-02 08:16:31 +05:30
|
|
|
end
|
2022-04-04 11:22:00 +05:30
|
|
|
|
|
|
|
it_behaves_like 'a metrics exporter', 'sidekiq', 'sidekiq_exporter'
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
2022-05-07 20:08:51 +05:30
|
|
|
|
|
|
|
describe '.start_for_puma' do
|
|
|
|
let(:supervisor) { instance_double(Gitlab::ProcessSupervisor) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Gitlab::ProcessSupervisor).to receive(:instance).and_return(supervisor)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'spawns a server process and supervises it' do
|
|
|
|
expect(Process).to receive(:spawn).with(
|
|
|
|
include('METRICS_SERVER_TARGET' => 'puma'), end_with('bin/metrics-server'), anything
|
|
|
|
).once.and_return(42)
|
|
|
|
expect(supervisor).to receive(:supervise).with(42)
|
|
|
|
|
|
|
|
described_class.start_for_puma
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the supervisor callback is invoked' do
|
|
|
|
context 'and the supervisor is alive' do
|
|
|
|
it 'restarts the metrics server' do
|
|
|
|
expect(supervisor).to receive(:alive).and_return(true)
|
|
|
|
expect(supervisor).to receive(:supervise).and_yield
|
|
|
|
expect(Process).to receive(:spawn).with(
|
|
|
|
include('METRICS_SERVER_TARGET' => 'puma'), end_with('bin/metrics-server'), anything
|
|
|
|
).twice.and_return(42)
|
|
|
|
|
|
|
|
described_class.start_for_puma
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and the supervisor is not alive' do
|
|
|
|
it 'does not restart the server' do
|
|
|
|
expect(supervisor).to receive(:alive).and_return(false)
|
|
|
|
expect(supervisor).to receive(:supervise).and_yield
|
|
|
|
expect(Process).to receive(:spawn).with(
|
|
|
|
include('METRICS_SERVER_TARGET' => 'puma'), end_with('bin/metrics-server'), anything
|
|
|
|
).once.and_return(42)
|
|
|
|
|
|
|
|
described_class.start_for_puma
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|