Commit 5e75b46d authored by Stan Hu's avatar Stan Hu

Merge branch '216022-add-dashboard-versioning' into 'master'

Add a version number to metrics dashboard cache keys

See merge request gitlab-org/gitlab!37081
parents 2ff0282b c3a09f0c
...@@ -8,6 +8,9 @@ module Metrics ...@@ -8,6 +8,9 @@ module Metrics
DASHBOARD_PATH = 'config/prometheus/cluster_metrics.yml' DASHBOARD_PATH = 'config/prometheus/cluster_metrics.yml'
DASHBOARD_NAME = 'Cluster' DASHBOARD_NAME = 'Cluster'
# SHA256 hash of dashboard content
DASHBOARD_VERSION = '9349afc1d96329c08ab478ea0b77db94ee5cc2549b8c754fba67a7f424666b22'
SEQUENCE = [ SEQUENCE = [
STAGES::ClusterEndpointInserter, STAGES::ClusterEndpointInserter,
STAGES::PanelIdsInserter, STAGES::PanelIdsInserter,
...@@ -26,6 +29,12 @@ module Metrics ...@@ -26,6 +29,12 @@ module Metrics
def allowed? def allowed?
true true
end end
private
def dashboard_version
DASHBOARD_VERSION
end
end end
end end
end end
...@@ -5,6 +5,15 @@ module Metrics ...@@ -5,6 +5,15 @@ module Metrics
class PodDashboardService < ::Metrics::Dashboard::PredefinedDashboardService class PodDashboardService < ::Metrics::Dashboard::PredefinedDashboardService
DASHBOARD_PATH = 'config/prometheus/pod_metrics.yml' DASHBOARD_PATH = 'config/prometheus/pod_metrics.yml'
DASHBOARD_NAME = 'Pod Health' DASHBOARD_NAME = 'Pod Health'
# SHA256 hash of dashboard content
DASHBOARD_VERSION = 'f12f641d2575d5dcb69e2c633ff5231dbd879ad35020567d8fc4e1090bfdb4b4'
private
def dashboard_version
DASHBOARD_VERSION
end
end end
end end
end end
...@@ -32,8 +32,12 @@ module Metrics ...@@ -32,8 +32,12 @@ module Metrics
private private
def dashboard_version
raise NotImplementedError
end
def cache_key def cache_key
"metrics_dashboard_#{dashboard_path}" "metrics_dashboard_#{dashboard_path}_#{dashboard_version}"
end end
def dashboard_path def dashboard_path
......
...@@ -8,6 +8,9 @@ module Metrics ...@@ -8,6 +8,9 @@ module Metrics
DASHBOARD_PATH = 'config/prometheus/self_monitoring_default.yml' DASHBOARD_PATH = 'config/prometheus/self_monitoring_default.yml'
DASHBOARD_NAME = N_('Default dashboard') DASHBOARD_NAME = N_('Default dashboard')
# SHA256 hash of dashboard content
DASHBOARD_VERSION = '1dff3e3cb76e73c8e368823c98b34c61aec0d141978450dea195a3b3dc2415d6'
SEQUENCE = [ SEQUENCE = [
STAGES::CustomMetricsInserter, STAGES::CustomMetricsInserter,
STAGES::MetricEndpointInserter, STAGES::MetricEndpointInserter,
...@@ -35,6 +38,12 @@ module Metrics ...@@ -35,6 +38,12 @@ module Metrics
params[:dashboard_path].nil? && params[:environment]&.project&.self_monitoring? params[:dashboard_path].nil? && params[:environment]&.project&.self_monitoring?
end end
end end
private
def dashboard_version
DASHBOARD_VERSION
end
end end
end end
end end
...@@ -8,6 +8,9 @@ module Metrics ...@@ -8,6 +8,9 @@ module Metrics
DASHBOARD_PATH = 'config/prometheus/common_metrics.yml' DASHBOARD_PATH = 'config/prometheus/common_metrics.yml'
DASHBOARD_NAME = N_('Default dashboard') DASHBOARD_NAME = N_('Default dashboard')
# SHA256 hash of dashboard content
DASHBOARD_VERSION = '4685fe386c25b1a786b3be18f79bb2ee9828019003e003816284cdb634fa3e13'
SEQUENCE = [ SEQUENCE = [
STAGES::CommonMetricsInserter, STAGES::CommonMetricsInserter,
STAGES::CustomMetricsInserter, STAGES::CustomMetricsInserter,
...@@ -30,6 +33,12 @@ module Metrics ...@@ -30,6 +33,12 @@ module Metrics
}] }]
end end
end end
private
def dashboard_version
DASHBOARD_VERSION
end
end end
end end
end end
...@@ -36,10 +36,18 @@ RSpec.describe Metrics::Dashboard::ClusterDashboardService, :use_clean_rails_mem ...@@ -36,10 +36,18 @@ RSpec.describe Metrics::Dashboard::ClusterDashboardService, :use_clean_rails_mem
describe '#get_dashboard' do describe '#get_dashboard' do
let(:service_params) { [project, user, { cluster: cluster, cluster_type: :project }] } let(:service_params) { [project, user, { cluster: cluster, cluster_type: :project }] }
let(:service_call) { described_class.new(*service_params).get_dashboard } let(:service_call) { subject.get_dashboard }
subject { described_class.new(*service_params) }
it_behaves_like 'valid dashboard service response' it_behaves_like 'valid dashboard service response'
it_behaves_like 'caches the unprocessed dashboard for subsequent calls' it_behaves_like 'caches the unprocessed dashboard for subsequent calls'
it_behaves_like 'refreshes cache when dashboard_version is changed'
it_behaves_like 'dashboard_version contains SHA256 hash of dashboard file content' do
let(:dashboard_path) { described_class::DASHBOARD_PATH }
let(:dashboard_version) { subject.send(:dashboard_version) }
end
context 'when called with a non-system dashboard' do context 'when called with a non-system dashboard' do
let(:dashboard_path) { 'garbage/dashboard/path' } let(:dashboard_path) { 'garbage/dashboard/path' }
......
...@@ -47,6 +47,11 @@ RSpec.describe Metrics::Dashboard::PodDashboardService, :use_clean_rails_memory_ ...@@ -47,6 +47,11 @@ RSpec.describe Metrics::Dashboard::PodDashboardService, :use_clean_rails_memory_
it_behaves_like 'valid dashboard service response' it_behaves_like 'valid dashboard service response'
it_behaves_like 'caches the unprocessed dashboard for subsequent calls' it_behaves_like 'caches the unprocessed dashboard for subsequent calls'
it_behaves_like 'refreshes cache when dashboard_version is changed'
it_behaves_like 'updates gitlab_metrics_dashboard_processing_time_ms metric' it_behaves_like 'updates gitlab_metrics_dashboard_processing_time_ms metric'
it_behaves_like 'dashboard_version contains SHA256 hash of dashboard file content' do
let(:dashboard_version) { subject.send(:dashboard_version) }
end
end end
end end
...@@ -32,7 +32,13 @@ RSpec.describe Metrics::Dashboard::SelfMonitoringDashboardService, :use_clean_ra ...@@ -32,7 +32,13 @@ RSpec.describe Metrics::Dashboard::SelfMonitoringDashboardService, :use_clean_ra
it_behaves_like 'valid dashboard service response' it_behaves_like 'valid dashboard service response'
it_behaves_like 'raises error for users with insufficient permissions' it_behaves_like 'raises error for users with insufficient permissions'
it_behaves_like 'caches the unprocessed dashboard for subsequent calls' it_behaves_like 'caches the unprocessed dashboard for subsequent calls'
it_behaves_like 'refreshes cache when dashboard_version is changed'
it_behaves_like 'updates gitlab_metrics_dashboard_processing_time_ms metric' it_behaves_like 'updates gitlab_metrics_dashboard_processing_time_ms metric'
it_behaves_like 'dashboard_version contains SHA256 hash of dashboard file content' do
let(:dashboard_path) { described_class::DASHBOARD_PATH }
let(:dashboard_version) { subject.send(:dashboard_version) }
end
end end
describe '.all_dashboard_paths' do describe '.all_dashboard_paths' do
......
...@@ -28,8 +28,13 @@ RSpec.describe Metrics::Dashboard::SystemDashboardService, :use_clean_rails_memo ...@@ -28,8 +28,13 @@ RSpec.describe Metrics::Dashboard::SystemDashboardService, :use_clean_rails_memo
it_behaves_like 'valid dashboard service response' it_behaves_like 'valid dashboard service response'
it_behaves_like 'raises error for users with insufficient permissions' it_behaves_like 'raises error for users with insufficient permissions'
it_behaves_like 'caches the unprocessed dashboard for subsequent calls' it_behaves_like 'caches the unprocessed dashboard for subsequent calls'
it_behaves_like 'refreshes cache when dashboard_version is changed'
it_behaves_like 'updates gitlab_metrics_dashboard_processing_time_ms metric' it_behaves_like 'updates gitlab_metrics_dashboard_processing_time_ms metric'
it_behaves_like 'dashboard_version contains SHA256 hash of dashboard file content' do
let(:dashboard_version) { subject.send(:dashboard_version) }
end
context 'when called with a non-system dashboard' do context 'when called with a non-system dashboard' do
let(:dashboard_path) { 'garbage/dashboard/path' } let(:dashboard_path) { 'garbage/dashboard/path' }
......
...@@ -39,6 +39,33 @@ RSpec.shared_examples 'caches the unprocessed dashboard for subsequent calls' do ...@@ -39,6 +39,33 @@ RSpec.shared_examples 'caches the unprocessed dashboard for subsequent calls' do
end end
end end
# This spec is applicable for predefined/out-of-the-box dashboard services.
RSpec.shared_examples 'refreshes cache when dashboard_version is changed' do
specify do
allow_next_instance_of(described_class) do |service|
allow(service).to receive(:dashboard_version).and_return('1', '2')
end
expect(File).to receive(:read).twice.and_call_original
service = described_class.new(*service_params)
service.get_dashboard
service.get_dashboard
end
end
# This spec is applicable for predefined/out-of-the-box dashboard services.
# This shared_example requires the following variables to be defined:
# dashboard_path: Relative path to the dashboard, ex: 'config/prometheus/common_metrics.yml'
# dashboard_version: The version string used in the cache_key.
RSpec.shared_examples 'dashboard_version contains SHA256 hash of dashboard file content' do
specify do
dashboard = File.read(Rails.root.join(dashboard_path))
expect(Digest::SHA256.hexdigest(dashboard)).to eq(dashboard_version)
end
end
RSpec.shared_examples 'valid embedded dashboard service response' do RSpec.shared_examples 'valid embedded dashboard service response' do
let(:dashboard_schema) { Gitlab::Json.parse(fixture_file('lib/gitlab/metrics/dashboard/schemas/embedded_dashboard.json')) } let(:dashboard_schema) { Gitlab::Json.parse(fixture_file('lib/gitlab/metrics/dashboard/schemas/embedded_dashboard.json')) }
......
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