2021-12-02 22:14:42 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
require_relative '../../../metrics_server/metrics_server'
|
|
|
|
|
|
|
|
# End-to-end tests for the metrics server process we use to serve metrics
|
|
|
|
# from forking applications (Sidekiq, Puma) to the Prometheus scraper.
|
|
|
|
RSpec.describe 'bin/metrics-server', :aggregate_failures do
|
|
|
|
let(:config_file) { Tempfile.new('gitlab.yml') }
|
|
|
|
let(:config) do
|
|
|
|
{
|
|
|
|
'test' => {
|
|
|
|
'monitoring' => {
|
|
|
|
'sidekiq_exporter' => {
|
|
|
|
'address' => 'localhost',
|
|
|
|
'enabled' => true,
|
|
|
|
'port' => 3807
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a running server' do
|
2022-01-10 07:15:34 -05:00
|
|
|
let(:metrics_dir) { Dir.mktmpdir }
|
|
|
|
|
2021-12-02 22:14:42 -05:00
|
|
|
before do
|
|
|
|
# We need to send a request to localhost
|
|
|
|
WebMock.allow_net_connect!
|
|
|
|
|
|
|
|
config_file.write(YAML.dump(config))
|
|
|
|
config_file.close
|
2021-12-15 19:15:50 -05:00
|
|
|
|
|
|
|
env = {
|
|
|
|
'GITLAB_CONFIG' => config_file.path,
|
|
|
|
'METRICS_SERVER_TARGET' => 'sidekiq',
|
2022-01-10 07:15:34 -05:00
|
|
|
'WIPE_METRICS_DIR' => '1',
|
|
|
|
'prometheus_multiproc_dir' => metrics_dir
|
2021-12-15 19:15:50 -05:00
|
|
|
}
|
|
|
|
@pid = Process.spawn(env, 'bin/metrics-server', pgroup: true)
|
2021-12-02 22:14:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
webmock_enable!
|
|
|
|
|
|
|
|
if @pid
|
2021-12-15 19:15:50 -05:00
|
|
|
pgrp = Process.getpgid(@pid)
|
|
|
|
|
2021-12-02 22:14:42 -05:00
|
|
|
Timeout.timeout(5) do
|
2021-12-15 19:15:50 -05:00
|
|
|
Process.kill('TERM', -pgrp)
|
2021-12-02 22:14:42 -05:00
|
|
|
Process.waitpid(@pid)
|
|
|
|
end
|
2021-12-15 19:15:50 -05:00
|
|
|
|
|
|
|
expect(Gitlab::ProcessManagement.process_alive?(@pid)).to be(false)
|
2021-12-02 22:14:42 -05:00
|
|
|
end
|
|
|
|
rescue Errno::ESRCH => _
|
|
|
|
# 'No such process' means the process died before
|
|
|
|
ensure
|
|
|
|
config_file.unlink
|
2022-01-10 07:15:34 -05:00
|
|
|
FileUtils.rm_rf(metrics_dir, secure: true)
|
2021-12-02 22:14:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'serves /metrics endpoint' do
|
|
|
|
expect do
|
|
|
|
Timeout.timeout(5) do
|
|
|
|
http_ok = false
|
|
|
|
until http_ok
|
|
|
|
sleep 1
|
|
|
|
response = Gitlab::HTTP.try_get("http://localhost:3807/metrics", allow_local_requests: true)
|
|
|
|
http_ok = response&.success?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|