summaryrefslogtreecommitdiff
path: root/spec/commands/metrics_server/metrics_server_spec.rb
blob: 310e31da04501c7a7d4c6966393407082e89cb2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# frozen_string_literal: true

require 'spec_helper'
require 'rake_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 'GitLab metrics server', :aggregate_failures do
  let(:config_file) { Tempfile.new('gitlab.yml') }
  let(:address) { '127.0.0.1' }
  let(:port) { 3807 }
  let(:config) do
    {
      'test' => {
        'monitoring' => {
          'web_exporter' => {
            'address' => address,
            'enabled' => true,
            'port' => port
          },
          'sidekiq_exporter' => {
            'address' => address,
            'enabled' => true,
            'port' => port
          }
        }
      }
    }
  end

  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
      let(:metrics_dir) { Dir.mktmpdir }

      subject(:start_server!) do
        @pid = MetricsServer.spawn(target, metrics_dir: metrics_dir, path: @exporter_path.join('bin'))
      end

      before do
        if use_golang_server
          stub_env('GITLAB_GOLANG_METRICS_SERVER', '1')
          allow(Settings).to receive(:monitoring).and_return(
            Settingslogic.new(config.dig('test', 'monitoring')))
        else
          config_file.write(YAML.dump(config))
          config_file.close
          stub_env('GITLAB_CONFIG', config_file.path)
        end
        # We need to send a request to localhost
        WebMock.allow_net_connect!
      end

      after do
        webmock_enable!

        if @pid
          pgrp = Process.getpgid(@pid)

          Timeout.timeout(10) do
            Process.kill('TERM', -pgrp)
            Process.waitpid(@pid)
          end

          expect(Gitlab::ProcessManagement.process_alive?(@pid)).to be(false)
        end
      rescue Errno::ESRCH, Errno::ECHILD => _
        # 'No such process' or 'No child processes' means the process died before
      ensure
        config_file.unlink
        FileUtils.rm_rf(metrics_dir, secure: true)
      end

      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'
      end
    end
  end

  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
end