2021-12-02 22:14:42 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
2022-04-25 17:09:46 -04:00
|
|
|
require 'rake_helper'
|
2021-12-02 22:14:42 -05:00
|
|
|
|
|
|
|
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.
|
2022-04-25 17:09:46 -04:00
|
|
|
RSpec.describe 'GitLab metrics server', :aggregate_failures do
|
2021-12-02 22:14:42 -05:00
|
|
|
let(:config_file) { Tempfile.new('gitlab.yml') }
|
2022-04-25 17:09:46 -04:00
|
|
|
let(:address) { '127.0.0.1' }
|
|
|
|
let(:port) { 3807 }
|
2021-12-02 22:14:42 -05:00
|
|
|
let(:config) do
|
|
|
|
{
|
|
|
|
'test' => {
|
|
|
|
'monitoring' => {
|
2022-02-10 16:15:20 -05:00
|
|
|
'web_exporter' => {
|
2022-04-25 17:09:46 -04:00
|
|
|
'address' => address,
|
2022-02-10 16:15:20 -05:00
|
|
|
'enabled' => true,
|
2022-04-25 17:09:46 -04:00
|
|
|
'port' => port
|
2022-02-10 16:15:20 -05:00
|
|
|
},
|
2021-12-02 22:14:42 -05:00
|
|
|
'sidekiq_exporter' => {
|
2022-04-25 17:09:46 -04:00
|
|
|
'address' => address,
|
2021-12-02 22:14:42 -05:00
|
|
|
'enabled' => true,
|
2022-04-25 17:09:46 -04:00
|
|
|
'port' => port
|
2021-12-02 22:14:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2022-04-25 17:09:46 -04:00
|
|
|
before(:all) do
|
|
|
|
Rake.application.rake_require 'tasks/gitlab/metrics_exporter'
|
|
|
|
|
|
|
|
@exporter_path = Rails.root.join('tmp', 'test', 'gme')
|
|
|
|
|
|
|
|
run_rake_task('gitlab:metrics_exporter:install', @exporter_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
after(:all) do
|
|
|
|
FileUtils.rm_rf(@exporter_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'serves metrics endpoint' do
|
|
|
|
it 'serves /metrics endpoint' do
|
|
|
|
start_server!
|
|
|
|
|
|
|
|
expect do
|
|
|
|
Timeout.timeout(10) do
|
|
|
|
http_ok = false
|
|
|
|
until http_ok
|
|
|
|
sleep 1
|
|
|
|
response = Gitlab::HTTP.try_get("http://#{address}:#{port}/metrics", allow_local_requests: true)
|
|
|
|
http_ok = response&.success?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'spawns a server' do |target, use_golang_server|
|
|
|
|
context "targeting #{target} when using Golang server is #{use_golang_server}" do
|
2022-02-10 16:15:20 -05:00
|
|
|
let(:metrics_dir) { Dir.mktmpdir }
|
2022-01-10 07:15:34 -05:00
|
|
|
|
2022-04-25 17:09:46 -04:00
|
|
|
subject(:start_server!) do
|
|
|
|
@pid = MetricsServer.spawn(target, metrics_dir: metrics_dir, path: @exporter_path.join('bin'))
|
|
|
|
end
|
|
|
|
|
2022-02-10 16:15:20 -05:00
|
|
|
before do
|
2022-04-25 17:09:46 -04:00
|
|
|
if use_golang_server
|
|
|
|
stub_env('GITLAB_GOLANG_METRICS_SERVER', '1')
|
|
|
|
allow(Settings).to receive(:monitoring).and_return(config.dig('test', 'monitoring'))
|
|
|
|
else
|
|
|
|
config_file.write(YAML.dump(config))
|
|
|
|
config_file.close
|
|
|
|
stub_env('GITLAB_CONFIG', config_file.path)
|
|
|
|
end
|
2022-02-10 16:15:20 -05:00
|
|
|
# We need to send a request to localhost
|
|
|
|
WebMock.allow_net_connect!
|
|
|
|
end
|
2021-12-02 22:14:42 -05:00
|
|
|
|
2022-02-10 16:15:20 -05:00
|
|
|
after do
|
|
|
|
webmock_enable!
|
2021-12-02 22:14:42 -05:00
|
|
|
|
2022-02-10 16:15:20 -05:00
|
|
|
if @pid
|
|
|
|
pgrp = Process.getpgid(@pid)
|
2021-12-15 19:15:50 -05:00
|
|
|
|
2022-02-10 16:15:20 -05:00
|
|
|
Timeout.timeout(10) do
|
|
|
|
Process.kill('TERM', -pgrp)
|
|
|
|
Process.waitpid(@pid)
|
|
|
|
end
|
2021-12-15 19:15:50 -05:00
|
|
|
|
2022-02-10 16:15:20 -05:00
|
|
|
expect(Gitlab::ProcessManagement.process_alive?(@pid)).to be(false)
|
|
|
|
end
|
2022-04-25 17:09:46 -04:00
|
|
|
rescue Errno::ESRCH, Errno::ECHILD => _
|
|
|
|
# 'No such process' or 'No child processes' means the process died before
|
2022-02-10 16:15:20 -05:00
|
|
|
ensure
|
|
|
|
config_file.unlink
|
|
|
|
FileUtils.rm_rf(metrics_dir, secure: true)
|
2021-12-02 22:14:42 -05:00
|
|
|
end
|
|
|
|
|
2022-04-25 17:09:46 -04:00
|
|
|
it_behaves_like 'serves metrics endpoint'
|
|
|
|
|
|
|
|
context 'when using Pathname instance as target directory' do
|
|
|
|
let(:metrics_dir) { Pathname.new(Dir.mktmpdir) }
|
|
|
|
|
|
|
|
it_behaves_like 'serves metrics endpoint'
|
2022-02-10 16:15:20 -05:00
|
|
|
end
|
2021-12-02 22:14:42 -05:00
|
|
|
end
|
|
|
|
end
|
2022-04-25 17:09:46 -04:00
|
|
|
|
|
|
|
it_behaves_like 'spawns a server', 'puma', true
|
|
|
|
it_behaves_like 'spawns a server', 'puma', false
|
|
|
|
it_behaves_like 'spawns a server', 'sidekiq', true
|
|
|
|
it_behaves_like 'spawns a server', 'sidekiq', false
|
2021-12-02 22:14:42 -05:00
|
|
|
end
|