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-16 05:09:10 -04:00
|
|
|
describe Gitlab::MetricsDashboard::Processor do
|
2019-04-16 03:15:57 -04:00
|
|
|
let(:project) { build(:project) }
|
2019-04-17 07:37:42 -04:00
|
|
|
let(:environment) { build(:environment) }
|
2019-04-16 05:16:51 -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-04-19 06:12:54 -04:00
|
|
|
let(:process_params) { [project, environment] }
|
|
|
|
let(:dashboard) { described_class.new(*process_params).process(dashboard_yml) }
|
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 = [
|
|
|
|
'metric_a2', # group priority 10, panel weight 2
|
|
|
|
'metric_a1', # group priority 10, panel weight 1
|
|
|
|
'metric_b', # group priority 1, panel weight 1
|
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-16 11:37:47 -04:00
|
|
|
private
|
|
|
|
|
2019-04-16 03:01:43 -04:00
|
|
|
def all_metrics
|
|
|
|
dashboard[:panel_groups].map do |group|
|
|
|
|
group[:panels].map { |panel| panel[:metrics] }
|
|
|
|
end.flatten
|
|
|
|
end
|
2019-04-16 11:37:47 -04:00
|
|
|
|
|
|
|
def get_metric_details(metric)
|
|
|
|
{
|
|
|
|
query_range: metric.query,
|
|
|
|
unit: metric.unit,
|
|
|
|
label: metric.legend,
|
|
|
|
metric_id: metric.id
|
|
|
|
}
|
|
|
|
end
|
2019-04-16 03:01:43 -04:00
|
|
|
end
|