2019-07-25 21:41:54 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-13 11:09:08 -04:00
|
|
|
RSpec.describe 'Metrics rendering', :js, :kubeclient, :use_clean_rails_memory_store_caching, :sidekiq_inline do
|
2019-07-25 21:41:54 -04:00
|
|
|
include PrometheusHelpers
|
2020-07-13 11:09:08 -04:00
|
|
|
include KubernetesHelpers
|
2019-11-11 19:06:21 -05:00
|
|
|
include GrafanaApiHelpers
|
2020-02-24 19:09:12 -05:00
|
|
|
include MetricsDashboardUrlHelpers
|
|
|
|
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:project) { create(:prometheus_project) }
|
|
|
|
let_it_be(:environment) { create(:environment, project: project) }
|
2019-07-25 21:41:54 -04:00
|
|
|
|
|
|
|
let(:issue) { create(:issue, project: project, description: description) }
|
|
|
|
let(:description) { "See [metrics dashboard](#{metrics_url}) for info." }
|
2020-02-24 19:09:12 -05:00
|
|
|
let(:metrics_url) { urls.metrics_project_environment_url(project, environment) }
|
2019-07-25 21:41:54 -04:00
|
|
|
|
|
|
|
before do
|
2020-02-24 19:09:12 -05:00
|
|
|
clear_host_from_memoized_variables
|
2020-07-31 20:09:45 -04:00
|
|
|
stub_gitlab_domain
|
2020-02-24 19:09:12 -05:00
|
|
|
|
2019-07-25 21:41:54 -04:00
|
|
|
project.add_developer(user)
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
2020-02-24 19:09:12 -05:00
|
|
|
clear_host_from_memoized_variables
|
2019-07-25 21:41:54 -04:00
|
|
|
end
|
|
|
|
|
2019-11-11 19:06:21 -05:00
|
|
|
context 'internal metrics embeds' do
|
|
|
|
before do
|
|
|
|
import_common_metrics
|
|
|
|
stub_any_prometheus_request_with_response
|
2020-04-03 05:09:31 -04:00
|
|
|
|
|
|
|
allow(Prometheus::ProxyService).to receive(:new).and_call_original
|
2019-11-11 19:06:21 -05:00
|
|
|
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)')
|
2020-04-03 05:09:31 -04:00
|
|
|
|
|
|
|
# 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)
|
2019-11-11 19:06:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when dashboard params are in included the url' do
|
2020-02-24 19:09:12 -05:00
|
|
|
let(:metrics_url) { urls.metrics_project_environment_url(project, environment, **chart_params) }
|
2019-08-15 17:38:29 -04:00
|
|
|
|
2019-11-11 19:06:21 -05:00
|
|
|
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])
|
2020-04-03 05:09:31 -04:00
|
|
|
|
|
|
|
# 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)
|
2019-11-11 19:06:21 -05:00
|
|
|
end
|
2020-03-11 17:09:19 -04:00
|
|
|
|
|
|
|
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])
|
2020-04-03 05:09:31 -04:00
|
|
|
|
|
|
|
# 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)
|
2020-03-11 17:09:19 -04:00
|
|
|
end
|
|
|
|
end
|
2019-11-11 19:06:21 -05:00
|
|
|
end
|
2019-08-15 17:38:29 -04:00
|
|
|
end
|
|
|
|
|
2019-11-11 19:06:21 -05:00
|
|
|
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) }
|
2019-08-15 17:38:29 -04:00
|
|
|
|
2019-11-11 19:06:21 -05:00
|
|
|
before do
|
|
|
|
stub_dashboard_request(grafana_base_url)
|
|
|
|
stub_datasource_request(grafana_base_url)
|
|
|
|
stub_all_grafana_proxy_requests(grafana_base_url)
|
2020-04-03 05:09:31 -04:00
|
|
|
|
|
|
|
allow(Grafana::ProxyService).to receive(:new).and_call_original
|
2019-08-15 17:38:29 -04:00
|
|
|
end
|
|
|
|
|
2019-11-11 19:06:21 -05:00
|
|
|
it 'shows embedded metrics' do
|
2019-07-25 21:41:54 -04:00
|
|
|
visit project_issue_path(project, issue)
|
|
|
|
|
|
|
|
expect(page).to have_css('div.prometheus-graph')
|
2019-11-11 19:06:21 -05:00
|
|
|
expect(page).to have_text('Expired / Evicted')
|
|
|
|
expect(page).to have_text('expired - test-attribute-value')
|
2020-04-03 05:09:31 -04:00
|
|
|
|
|
|
|
# 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)
|
2019-07-25 21:41:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-07 20:09:30 -04:00
|
|
|
context 'transient metrics embeds' do
|
2020-04-22 05:09:36 -04:00
|
|
|
let(:metrics_url) { urls.metrics_dashboard_project_environment_url(project, environment, embed_json: embed_json) }
|
2020-04-07 20:09:30 -04:00
|
|
|
let(:title) { 'Important Metrics' }
|
|
|
|
let(:embed_json) do
|
|
|
|
{
|
|
|
|
panel_groups: [{
|
|
|
|
panels: [{
|
|
|
|
type: "line-graph",
|
|
|
|
title: title,
|
|
|
|
y_label: "metric",
|
|
|
|
metrics: [{
|
|
|
|
query_range: "metric * 0.5 < 1"
|
|
|
|
}]
|
|
|
|
}]
|
|
|
|
}]
|
|
|
|
}.to_json
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_any_prometheus_request_with_response
|
|
|
|
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(title)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-13 11:09:08 -04:00
|
|
|
context 'for GitLab embedded cluster health metrics' do
|
|
|
|
before do
|
|
|
|
project.add_maintainer(user)
|
|
|
|
import_common_metrics
|
|
|
|
stub_any_prometheus_request_with_response
|
|
|
|
|
|
|
|
allow(Prometheus::ProxyService).to receive(:new).and_call_original
|
|
|
|
|
|
|
|
create(:clusters_applications_prometheus, :installed, cluster: cluster)
|
|
|
|
stub_kubeclient_discover(cluster.platform.api_url)
|
|
|
|
stub_prometheus_request(/prometheus-prometheus-server/, body: prometheus_values_body)
|
|
|
|
stub_prometheus_request(/prometheus\/api\/v1/, body: prometheus_values_body)
|
|
|
|
end
|
|
|
|
|
|
|
|
let_it_be(:cluster) { create(:cluster, :provided_by_gcp, :project, projects: [project], user: user) }
|
|
|
|
let(:params) { [project.namespace.path, project.path, cluster.id] }
|
|
|
|
let(:query_params) { { group: 'Cluster Health', title: 'CPU Usage', y_label: 'CPU (cores)' } }
|
|
|
|
let(:metrics_url) { urls.namespace_project_cluster_url(*params, **query_params) }
|
|
|
|
let(:description) { "# Summary \n[](#{metrics_url})" }
|
|
|
|
|
|
|
|
it 'shows embedded metrics' do
|
|
|
|
visit project_issue_path(project, issue)
|
|
|
|
|
|
|
|
expect(page).to have_css('div.prometheus-graph')
|
|
|
|
expect(page).to have_text(query_params[:title])
|
|
|
|
expect(page).to have_text(query_params[:y_label])
|
|
|
|
expect(page).not_to have_text(metrics_url)
|
|
|
|
|
|
|
|
expect(Prometheus::ProxyService)
|
|
|
|
.to have_received(:new)
|
|
|
|
.with(cluster, 'GET', 'query_range', hash_including('start', 'end', 'step'))
|
|
|
|
.at_least(:once)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-25 21:41:54 -04:00
|
|
|
def import_common_metrics
|
|
|
|
::Gitlab::DatabaseImporters::CommonMetrics::Importer.new.execute
|
|
|
|
end
|
|
|
|
end
|