summaryrefslogtreecommitdiff
path: root/spec/features/markdown/metrics_spec.rb
blob: 0d8858a7afd9e5abe0cf934b4605cd38002986f0 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# frozen_string_literal: true

require 'spec_helper'

describe 'Metrics rendering', :js, :use_clean_rails_memory_store_caching, :sidekiq_inline do
  include PrometheusHelpers
  include GrafanaApiHelpers
  include MetricsDashboardUrlHelpers

  let_it_be(:user) { create(:user) }
  let_it_be(:project) { create(:prometheus_project) }
  let_it_be(:environment) { create(:environment, project: project) }

  let(:issue) { create(:issue, project: project, description: description) }
  let(:description) { "See [metrics dashboard](#{metrics_url}) for info." }
  let(:metrics_url) { urls.metrics_project_environment_url(project, environment) }

  before do
    clear_host_from_memoized_variables

    allow(::Gitlab.config.gitlab)
      .to receive(:url)
      .and_return(urls.root_url.chomp('/'))

    project.add_developer(user)
    sign_in(user)
  end

  after do
    clear_host_from_memoized_variables
  end

  context 'internal metrics embeds' do
    before do
      import_common_metrics
      stub_any_prometheus_request_with_response

      allow(Prometheus::ProxyService).to receive(:new).and_call_original
    end

    it 'shows embedded metrics' do
      visit project_issue_path(project, issue)

      expect(page).to have_css('div.prometheus-graph')
      expect(page).to have_text('Memory Usage (Total)')
      expect(page).to have_text('Core Usage (Total)')

      # Ensure that the FE is calling the BE with expected params
      expect(Prometheus::ProxyService)
        .to have_received(:new)
        .with(environment, 'GET', 'query_range', hash_including('start', 'end', 'step'))
        .at_least(:once)
    end

    context 'when dashboard params are in included the url' do
      let(:metrics_url) { urls.metrics_project_environment_url(project, environment, **chart_params) }

      let(:chart_params) do
        {
          group: 'System metrics (Kubernetes)',
          title: 'Memory Usage (Pod average)',
          y_label: 'Memory Used per Pod (MB)'
        }
      end

      it 'shows embedded metrics for the specific chart' do
        visit project_issue_path(project, issue)

        expect(page).to have_css('div.prometheus-graph')
        expect(page).to have_text(chart_params[:title])
        expect(page).to have_text(chart_params[:y_label])

        # Ensure that the FE is calling the BE with expected params
        expect(Prometheus::ProxyService)
          .to have_received(:new)
          .with(environment, 'GET', 'query_range', hash_including('start', 'end', 'step'))
          .at_least(:once)
      end

      context 'when two dashboard urls are included' do
        let(:chart_params_2) do
          {
            group: 'System metrics (Kubernetes)',
            title: 'Core Usage (Total)',
            y_label: 'Total Cores'
          }
        end
        let(:metrics_url_2) { urls.metrics_project_environment_url(project, environment, **chart_params_2) }
        let(:description) { "See [metrics dashboard](#{metrics_url}) for info. \n See [metrics dashboard](#{metrics_url_2}) for info." }
        let(:issue) { create(:issue, project: project, description: description) }

        it 'shows embedded metrics for both urls' do
          visit project_issue_path(project, issue)

          expect(page).to have_css('div.prometheus-graph')
          expect(page).to have_text(chart_params[:title])
          expect(page).to have_text(chart_params[:y_label])
          expect(page).to have_text(chart_params_2[:title])
          expect(page).to have_text(chart_params_2[:y_label])

          # Ensure that the FE is calling the BE with expected params
          expect(Prometheus::ProxyService)
            .to have_received(:new)
            .with(environment, 'GET', 'query_range', hash_including('start', 'end', 'step'))
            .at_least(:once)
        end
      end
    end
  end

  context 'grafana metrics embeds' do
    let(:grafana_integration) { create(:grafana_integration, project: project) }
    let(:grafana_base_url) { grafana_integration.grafana_url }
    let(:metrics_url) { valid_grafana_dashboard_link(grafana_base_url) }

    before do
      stub_dashboard_request(grafana_base_url)
      stub_datasource_request(grafana_base_url)
      stub_all_grafana_proxy_requests(grafana_base_url)

      allow(Grafana::ProxyService).to receive(:new).and_call_original
    end

    it 'shows embedded metrics' do
      visit project_issue_path(project, issue)

      expect(page).to have_css('div.prometheus-graph')
      expect(page).to have_text('Expired / Evicted')
      expect(page).to have_text('expired - test-attribute-value')

      # Ensure that the FE is calling the BE with expected params
      expect(Grafana::ProxyService)
        .to have_received(:new)
        .with(project, anything, anything, hash_including('query', 'start', 'end', 'step'))
        .at_least(:once)
    end
  end

  def import_common_metrics
    ::Gitlab::DatabaseImporters::CommonMetrics::Importer.new.execute
  end
end