2019-04-16 12:08:04 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-04-16 03:01:43 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2019-04-24 23:03:50 -04:00
|
|
|
describe Gitlab::Metrics::Dashboard::Processor do
|
2019-04-16 03:15:57 -04:00
|
|
|
let(:project) { build(:project) }
|
2019-06-05 09:43:50 -04:00
|
|
|
let(:environment) { create(:environment, project: project) }
|
2019-04-24 23:03:50 -04:00
|
|
|
let(:dashboard_yml) { YAML.load_file('spec/fixtures/lib/gitlab/metrics/dashboard/sample_dashboard.yml') }
|
2019-04-16 03:01:43 -04:00
|
|
|
|
|
|
|
describe 'process' do
|
2019-09-18 10:02:45 -04:00
|
|
|
let(:sequence) do
|
|
|
|
[
|
|
|
|
Gitlab::Metrics::Dashboard::Stages::CommonMetricsInserter,
|
|
|
|
Gitlab::Metrics::Dashboard::Stages::ProjectMetricsInserter,
|
|
|
|
Gitlab::Metrics::Dashboard::Stages::EndpointInserter,
|
|
|
|
Gitlab::Metrics::Dashboard::Stages::Sorter
|
|
|
|
]
|
|
|
|
end
|
|
|
|
let(:process_params) { [project, dashboard_yml, sequence, { environment: environment }] }
|
|
|
|
let(:dashboard) { described_class.new(*process_params).process }
|
2019-04-16 03:01:43 -04:00
|
|
|
|
2019-06-05 09:43:50 -04:00
|
|
|
it 'includes a path for the prometheus endpoint with each metric' do
|
2019-06-06 12:49:40 -04:00
|
|
|
expect(all_metrics).to satisfy_all do |metric|
|
|
|
|
metric[:prometheus_endpoint_path] == prometheus_path(metric[:query_range])
|
2019-06-05 09:43:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-16 03:01:43 -04:00
|
|
|
context 'when dashboard config corresponds to common metrics' do
|
|
|
|
let!(:common_metric) { create(:prometheus_metric, :common, identifier: 'metric_a1') }
|
|
|
|
|
|
|
|
it 'inserts metric ids into the config' do
|
|
|
|
target_metric = all_metrics.find { |metric| metric[:id] == 'metric_a1' }
|
|
|
|
|
|
|
|
expect(target_metric).to include(:metric_id)
|
2019-04-17 07:37:42 -04:00
|
|
|
expect(target_metric[:metric_id]).to eq(common_metric.id)
|
2019-04-16 03:01:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the project has associated metrics' do
|
2019-04-16 11:37:47 -04:00
|
|
|
let!(:project_response_metric) { create(:prometheus_metric, project: project, group: :response) }
|
|
|
|
let!(:project_system_metric) { create(:prometheus_metric, project: project, group: :system) }
|
|
|
|
let!(:project_business_metric) { create(:prometheus_metric, project: project, group: :business) }
|
2019-04-16 03:01:43 -04:00
|
|
|
|
|
|
|
it 'includes project-specific metrics' do
|
2019-04-16 11:37:47 -04:00
|
|
|
expect(all_metrics).to include get_metric_details(project_system_metric)
|
|
|
|
expect(all_metrics).to include get_metric_details(project_response_metric)
|
|
|
|
expect(all_metrics).to include get_metric_details(project_business_metric)
|
2019-04-16 03:01:43 -04:00
|
|
|
end
|
|
|
|
|
2019-04-16 11:37:47 -04:00
|
|
|
it 'orders groups by priority and panels by weight' do
|
2019-04-17 02:47:50 -04:00
|
|
|
expected_metrics_order = [
|
2019-05-01 06:16:03 -04:00
|
|
|
'metric_b', # group priority 10, panel weight 1
|
|
|
|
'metric_a2', # group priority 1, panel weight 2
|
|
|
|
'metric_a1', # group priority 1, panel weight 1
|
2019-04-17 02:47:50 -04:00
|
|
|
project_business_metric.id, # group priority 0, panel weight nil (0)
|
|
|
|
project_response_metric.id, # group priority -5, panel weight nil (0)
|
|
|
|
project_system_metric.id, # group priority -10, panel weight nil (0)
|
|
|
|
]
|
2019-04-16 11:37:47 -04:00
|
|
|
actual_metrics_order = all_metrics.map { |m| m[:id] || m[:metric_id] }
|
2019-04-16 03:01:43 -04:00
|
|
|
|
|
|
|
expect(actual_metrics_order).to eq expected_metrics_order
|
|
|
|
end
|
2019-05-01 06:16:03 -04:00
|
|
|
|
|
|
|
context 'when the dashboard should not include project metrics' do
|
2019-09-18 10:02:45 -04:00
|
|
|
let(:sequence) do
|
|
|
|
[
|
|
|
|
Gitlab::Metrics::Dashboard::Stages::CommonMetricsInserter,
|
|
|
|
Gitlab::Metrics::Dashboard::Stages::EndpointInserter,
|
|
|
|
Gitlab::Metrics::Dashboard::Stages::Sorter
|
|
|
|
]
|
|
|
|
end
|
|
|
|
let(:dashboard) { described_class.new(*process_params).process }
|
2019-05-01 06:16:03 -04:00
|
|
|
|
|
|
|
it 'includes only dashboard metrics' do
|
|
|
|
metrics = all_metrics.map { |m| m[:id] }
|
|
|
|
|
|
|
|
expect(metrics.length).to be(3)
|
|
|
|
expect(metrics).to eq %w(metric_b metric_a2 metric_a1)
|
|
|
|
end
|
|
|
|
end
|
2019-04-16 03:01:43 -04:00
|
|
|
end
|
2019-04-22 02:23:35 -04:00
|
|
|
|
|
|
|
shared_examples_for 'errors with message' do |expected_message|
|
|
|
|
it 'raises a DashboardLayoutError' do
|
2019-09-18 10:02:45 -04:00
|
|
|
error_class = Gitlab::Metrics::Dashboard::Errors::DashboardProcessingError
|
2019-04-22 02:23:35 -04:00
|
|
|
|
|
|
|
expect { dashboard }.to raise_error(error_class, expected_message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the dashboard is missing panel_groups' do
|
|
|
|
let(:dashboard_yml) { {} }
|
|
|
|
|
|
|
|
it_behaves_like 'errors with message', 'Top-level key :panel_groups must be an array'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the dashboard contains a panel_group which is missing panels' do
|
|
|
|
let(:dashboard_yml) { { panel_groups: [{}] } }
|
|
|
|
|
|
|
|
it_behaves_like 'errors with message', 'Each "panel_group" must define an array :panels'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the dashboard contains a panel which is missing metrics' do
|
|
|
|
let(:dashboard_yml) { { panel_groups: [{ panels: [{}] }] } }
|
|
|
|
|
|
|
|
it_behaves_like 'errors with message', 'Each "panel" must define an array :metrics'
|
|
|
|
end
|
2019-06-06 12:49:40 -04:00
|
|
|
|
|
|
|
context 'when the dashboard contains a metric which is missing a query' do
|
|
|
|
let(:dashboard_yml) { { panel_groups: [{ panels: [{ metrics: [{}] }] }] } }
|
|
|
|
|
|
|
|
it_behaves_like 'errors with message', 'Each "metric" must define one of :query or :query_range'
|
|
|
|
end
|
2019-04-16 03:01:43 -04:00
|
|
|
end
|
|
|
|
|
2019-04-16 11:37:47 -04:00
|
|
|
private
|
|
|
|
|
2019-04-16 03:01:43 -04:00
|
|
|
def all_metrics
|
2019-07-24 04:19:15 -04:00
|
|
|
dashboard[:panel_groups].flat_map do |group|
|
|
|
|
group[:panels].flat_map { |panel| panel[:metrics] }
|
|
|
|
end
|
2019-04-16 03:01:43 -04:00
|
|
|
end
|
2019-04-16 11:37:47 -04:00
|
|
|
|
|
|
|
def get_metric_details(metric)
|
|
|
|
{
|
|
|
|
query_range: metric.query,
|
|
|
|
unit: metric.unit,
|
|
|
|
label: metric.legend,
|
2019-06-05 09:43:50 -04:00
|
|
|
metric_id: metric.id,
|
|
|
|
prometheus_endpoint_path: prometheus_path(metric.query)
|
2019-04-16 11:37:47 -04:00
|
|
|
}
|
|
|
|
end
|
2019-06-05 09:43:50 -04:00
|
|
|
|
|
|
|
def prometheus_path(query)
|
2019-06-06 12:49:40 -04:00
|
|
|
Gitlab::Routing.url_helpers.prometheus_api_project_environment_path(
|
|
|
|
project,
|
|
|
|
environment,
|
|
|
|
proxy_path: :query_range,
|
|
|
|
query: query
|
|
|
|
)
|
2019-06-05 09:43:50 -04:00
|
|
|
end
|
2019-04-16 03:01:43 -04:00
|
|
|
end
|