summaryrefslogtreecommitdiff
path: root/spec/controllers/metrics_controller_spec.rb
blob: 4f74af295c6b85aded302f015dd9ef556b4ce88e (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
119
120
121
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe MetricsController, :request_store do
  include StubENV

  let(:metrics_multiproc_dir) { @metrics_multiproc_dir }
  let(:whitelisted_ip) { '127.0.0.1' }
  let(:whitelisted_ip_range) { '10.0.0.0/24' }
  let(:ip_in_whitelisted_range) { '10.0.0.1' }
  let(:not_whitelisted_ip) { '10.0.1.1' }

  around do |example|
    Dir.mktmpdir do |path|
      @metrics_multiproc_dir = path
      example.run
    end
  end

  before do
    stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
    allow(Prometheus::Client.configuration).to receive(:multiprocess_files_dir).and_return(metrics_multiproc_dir)
    allow(Gitlab::Metrics).to receive(:prometheus_metrics_enabled?).and_return(true)
    allow(Settings.monitoring).to receive(:ip_whitelist).and_return([whitelisted_ip, whitelisted_ip_range])
    allow_next_instance_of(MetricsService) do |instance|
      allow(instance).to receive(:metrics_text).and_return("prometheus_counter 1")
    end
  end

  shared_examples_for 'protected metrics endpoint' do |examples|
    context 'accessed from whitelisted ip' do
      before do
        allow(Gitlab::RequestContext.instance).to receive(:client_ip).and_return(whitelisted_ip)
      end

      it_behaves_like examples
    end

    context 'accessed from ip in whitelisted range' do
      before do
        allow(Gitlab::RequestContext.instance).to receive(:client_ip).and_return(ip_in_whitelisted_range)
      end

      it_behaves_like examples
    end

    context 'accessed from not whitelisted ip' do
      before do
        allow(Gitlab::RequestContext.instance).to receive(:client_ip).and_return(not_whitelisted_ip)
      end

      it 'returns the expected error response' do
        get :index

        expect(response).to have_gitlab_http_status(:not_found)
      end
    end
  end

  describe '#index' do
    shared_examples_for 'providing metrics' do
      it 'returns prometheus metrics' do
        get :index

        expect(response).to have_gitlab_http_status(:ok)
        expect(response.body).to match(/^prometheus_counter 1$/)
      end

      it 'initializes the rails request SLIs' do
        expect(Gitlab::Metrics::RailsSlis).to receive(:initialize_request_slis_if_needed!).and_call_original

        get :index
      end

      context 'prometheus metrics are disabled' do
        before do
          allow(Gitlab::Metrics).to receive(:prometheus_metrics_enabled?).and_return(false)
        end

        it 'returns proper response' do
          get :index

          expect(response).to have_gitlab_http_status(:ok)
          expect(response.body).to eq("# Metrics are disabled, see: http://test.host/help/administration/monitoring/prometheus/gitlab_metrics#gitlab-prometheus-metrics\n")
        end
      end
    end

    include_examples 'protected metrics endpoint', 'providing metrics'
  end

  describe '#system' do
    shared_examples_for 'providing system stats' do
      let(:summary) do
        {
          version: 'ruby-3.0-patch1',
          memory_rss: 1024
        }
      end

      it 'renders system stats JSON' do
        expect(Prometheus::PidProvider).to receive(:worker_id).and_return('worker-0')
        expect(Gitlab::Metrics::System).to receive(:summary).and_return(summary)

        get :system

        expect(response).to have_gitlab_http_status(:ok)
        expect(response_json['version']).to eq('ruby-3.0-patch1')
        expect(response_json['worker_id']).to eq('worker-0')
        expect(response_json['memory_rss']).to eq(1024)
      end
    end

    include_examples 'protected metrics endpoint', 'providing system stats'
  end

  def response_json
    Gitlab::Json.parse(response.body)
  end
end