Commit 5ca140df authored by Kirstie Cook's avatar Kirstie Cook Committed by Imre Farkas

Create SelfMonitoringDashboardService and add specs

Set the default dashboard for self monitoring project
parent 5b5b4d87
......@@ -2335,6 +2335,10 @@ class Project < ApplicationRecord
false
end
def self_monitoring?
Gitlab::CurrentSettings.self_monitoring_project_id == id
end
private
def closest_namespace_setting(name)
......
# frozen_string_literal: true
# Fetches the self monitoring metrics dashboard and formats the output.
# Use Gitlab::Metrics::Dashboard::Finder to retrieve dashboards.
module Metrics
module Dashboard
class SelfMonitoringDashboardService < ::Metrics::Dashboard::PredefinedDashboardService
DASHBOARD_PATH = 'config/prometheus/self_monitoring_default.yml'
DASHBOARD_NAME = 'Default'
SEQUENCE = [
STAGES::ProjectMetricsInserter,
STAGES::EndpointInserter,
STAGES::Sorter
].freeze
class << self
def valid_params?(params)
matching_dashboard?(params[:dashboard_path]) || self_monitoring_project?(params)
end
def all_dashboard_paths(_project)
[{
path: DASHBOARD_PATH,
display_name: DASHBOARD_NAME,
default: true,
system_dashboard: false
}]
end
def self_monitoring_project?(params)
params[:dashboard_path].nil? && params[:environment]&.project&.self_monitoring?
end
end
end
end
end
---
title: Set default dashboard for self monitoring project
merge_request: 24814
author:
type: added
......@@ -65,7 +65,7 @@ module Gitlab
def find_all_paths_from_source(project)
Gitlab::Metrics::Dashboard::Cache.delete_all!
system_service.all_dashboard_paths(project)
default_dashboard_path(project)
.+ project_service.all_dashboard_paths(project)
end
......@@ -79,6 +79,18 @@ module Gitlab
::Metrics::Dashboard::ProjectDashboardService
end
def self_monitoring_service
::Metrics::Dashboard::SelfMonitoringDashboardService
end
def default_dashboard_path(project)
if project.self_monitoring?
self_monitoring_service.all_dashboard_paths(project)
else
system_service.all_dashboard_paths(project)
end
end
def service_for(options)
Gitlab::Metrics::Dashboard::ServiceSelector.call(options)
end
......
......@@ -18,6 +18,7 @@ module Gitlab
::Metrics::Dashboard::DefaultEmbedService,
::Metrics::Dashboard::SystemDashboardService,
::Metrics::Dashboard::PodDashboardService,
::Metrics::Dashboard::SelfMonitoringDashboardService,
::Metrics::Dashboard::ProjectDashboardService
].freeze
......
......@@ -44,6 +44,12 @@ describe Gitlab::Metrics::Dashboard::Finder, :use_clean_rails_memory_store_cachi
it_behaves_like 'valid dashboard service response'
end
context 'when the self monitoring dashboard is specified' do
let(:dashboard_path) { self_monitoring_dashboard_path }
it_behaves_like 'valid dashboard service response'
end
context 'when no dashboard is specified' do
let(:service_call) { described_class.find(project, user, environment: environment) }
......@@ -152,5 +158,33 @@ describe Gitlab::Metrics::Dashboard::Finder, :use_clean_rails_memory_store_cachi
expect(all_dashboard_paths).to contain_exactly(system_dashboard, project_dashboard)
end
end
context 'when the project is self monitoring' do
let(:self_monitoring_dashboard) do
{
path: self_monitoring_dashboard_path,
display_name: 'Default',
default: true,
system_dashboard: false
}
end
let(:dashboard_path) { '.gitlab/dashboards/test.yml' }
let(:project) { project_with_dashboard(dashboard_path) }
before do
stub_application_setting(self_monitoring_project_id: project.id)
end
it 'includes self monitoring and project dashboards' do
project_dashboard = {
path: dashboard_path,
display_name: 'test.yml',
default: false,
system_dashboard: false
}
expect(all_dashboard_paths).to contain_exactly(self_monitoring_dashboard, project_dashboard)
end
end
end
end
......@@ -30,6 +30,12 @@ describe Gitlab::Metrics::Dashboard::ServiceSelector do
end
end
context 'when the path is for the self monitoring dashboard' do
let(:arguments) { { dashboard_path: self_monitoring_dashboard_path } }
it { is_expected.to be Metrics::Dashboard::SelfMonitoringDashboardService }
end
context 'when the embedded flag is provided' do
let(:arguments) { { embedded: true } }
......
......@@ -5592,6 +5592,24 @@ describe Project do
it { is_expected.to be_falsey }
end
describe '#self_monitoring?' do
let_it_be(:project) { create(:project) }
subject { project.self_monitoring? }
context 'when the project is instance self monitoring' do
before do
stub_application_setting(self_monitoring_project_id: project.id)
end
it { is_expected.to be true }
end
context 'when the project is not self monitoring' do
it { is_expected.to be false }
end
end
def rugged_config
rugged_repo(project.repository).config
end
......
# frozen_string_literal: true
require 'spec_helper'
describe Metrics::Dashboard::SelfMonitoringDashboardService, :use_clean_rails_memory_store_caching do
include MetricsDashboardHelpers
let_it_be(:user) { create(:user) }
let_it_be(:project) { create(:project) }
let_it_be(:environment) { create(:environment, project: project) }
before do
project.add_maintainer(user)
stub_application_setting(self_monitoring_project_id: project.id)
end
describe '#get_dashboard' do
let(:service_params) { [project, user, { environment: environment }] }
let(:service_call) { described_class.new(*service_params).get_dashboard }
it_behaves_like 'valid dashboard service response'
it_behaves_like 'raises error for users with insufficient permissions'
it_behaves_like 'caches the unprocessed dashboard for subsequent calls'
end
describe '.all_dashboard_paths' do
it 'returns the dashboard attributes' do
all_dashboards = described_class.all_dashboard_paths(project)
expect(all_dashboards).to eq(
[{
path: described_class::DASHBOARD_PATH,
display_name: described_class::DASHBOARD_NAME,
default: true,
system_dashboard: false
}]
)
end
end
describe '.valid_params?' do
subject { described_class.valid_params?(params) }
context 'with environment' do
let(:params) { { environment: environment } }
it { is_expected.to be_truthy }
end
context 'with dashboard_path' do
let(:params) { { dashboard_path: self_monitoring_dashboard_path } }
it { is_expected.to be_truthy }
end
context 'with a different dashboard selected' do
let(:dashboard_path) { '.gitlab/dashboards/test.yml' }
let(:params) { { dashboard_path: dashboard_path, environment: environment } }
it { is_expected.to be_falsey }
end
context 'missing environment and dashboard_path' do
let(:params) { {} }
it { is_expected.to be_falsey }
end
end
end
......@@ -29,4 +29,8 @@ module MetricsDashboardHelpers
def business_metric_title
PrometheusMetricEnums.group_details[:business][:group_title]
end
def self_monitoring_dashboard_path
Metrics::Dashboard::SelfMonitoringDashboardService::DASHBOARD_PATH
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment