Commit f70adc9b authored by Igor Drozdov's avatar Igor Drozdov

Merge branch '213473-move-alerts-inserter' into 'master'

Move AlertsInserter out of EE

See merge request gitlab-org/gitlab!29333
parents bab6e618 0dfcab42
......@@ -12,7 +12,8 @@ module Metrics
STAGES::CommonMetricsInserter,
STAGES::EndpointInserter,
STAGES::PanelIdsInserter,
STAGES::Sorter
STAGES::Sorter,
STAGES::AlertsInserter
].freeze
def get_dashboard
......@@ -117,5 +118,3 @@ module Metrics
end
end
end
Metrics::Dashboard::BaseService.prepend_if_ee('EE::Metrics::Dashboard::BaseService')
......@@ -14,7 +14,8 @@ module Metrics
STAGES::CustomMetricsDetailsInserter,
STAGES::EndpointInserter,
STAGES::PanelIdsInserter,
STAGES::Sorter
STAGES::Sorter,
STAGES::AlertsInserter
].freeze
class << self
......@@ -30,5 +31,3 @@ module Metrics
end
end
end
Metrics::Dashboard::SystemDashboardService.prepend_if_ee('EE::Metrics::Dashboard::SystemDashboardService')
# frozen_string_literal: true
module EE
module Metrics
module Dashboard
module BaseService
extend ::Gitlab::Utils::Override
EE_SEQUENCE = [
::EE::Gitlab::Metrics::Dashboard::Stages::AlertsInserter
].freeze
override :sequence
def sequence
super + EE_SEQUENCE
end
end
end
end
end
# frozen_string_literal: true
module EE
module Metrics
module Dashboard
module SystemDashboardService
extend ::Gitlab::Utils::Override
EE_SEQUENCE = [
::EE::Gitlab::Metrics::Dashboard::Stages::AlertsInserter
].freeze
override :sequence
def sequence
super + EE_SEQUENCE
end
end
end
end
end
# frozen_string_literal: true
require 'set'
module EE
module Gitlab
module Metrics
module Dashboard
module Stages
class AlertsInserter < ::Gitlab::Metrics::Dashboard::Stages::BaseStage
include ::Gitlab::Utils::StrongMemoize
def transform!
return if metrics_with_alerts.empty?
for_metrics do |metric|
next unless metrics_with_alerts.include?(metric[:metric_id])
metric[:alert_path] = alert_path(metric[:metric_id], project, params[:environment])
end
end
private
def metrics_with_alerts
strong_memoize(:metrics_with_alerts) do
alerts = ::Projects::Prometheus::AlertsFinder
.new(project: project, environment: params[:environment])
.execute
Set.new(alerts.map(&:prometheus_metric_id))
end
end
def alert_path(metric_id, project, environment)
::Gitlab::Routing.url_helpers.project_prometheus_alert_path(project, metric_id, environment_id: environment.id, format: :json)
end
end
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::Metrics::Dashboard::Processor do
let(:project) { build(:project) }
let(:environment) { create(:environment, project: project) }
let(:dashboard_yml) { YAML.load_file('spec/fixtures/lib/gitlab/metrics/dashboard/sample_dashboard.yml') }
let(:params) { [project, dashboard_yml, sequence, { environment: environment }] }
describe 'process' do
let(:dashboard) { described_class.new(*params).process }
let(:sequence) do
[
Gitlab::Metrics::Dashboard::Stages::CommonMetricsInserter,
Gitlab::Metrics::Dashboard::Stages::CustomMetricsInserter,
Gitlab::Metrics::Dashboard::Stages::EndpointInserter,
Gitlab::Metrics::Dashboard::Stages::Sorter,
::EE::Gitlab::Metrics::Dashboard::Stages::AlertsInserter
]
end
context 'when the dashboard references persisted metrics with alerts' do
let!(:alert) do
create(
:prometheus_alert,
environment: environment,
project: project,
prometheus_metric: persisted_metric
)
end
shared_examples_for 'has saved alerts' do
it 'includes an alert path' do
target_metric = all_metrics.find { |metric| metric[:metric_id] == persisted_metric.id }
expect(target_metric).to be_a Hash
expect(target_metric).to include(:alert_path)
expect(target_metric[:alert_path]).to include(
project.path,
persisted_metric.id.to_s,
environment.id.to_s
)
end
end
context 'that are shared across projects' do
let!(:persisted_metric) { create(:prometheus_metric, :common, identifier: 'metric_a1') }
it_behaves_like 'has saved alerts'
end
context 'when the project has associated metrics' do
let!(:persisted_metric) { create(:prometheus_metric, project: project, group: :business) }
it_behaves_like 'has saved alerts'
end
end
context 'when there are no alerts' do
let!(:persisted_metric) { create(:prometheus_metric, :common, identifier: 'metric_a1') }
it 'does not insert an alert_path' do
target_metric = all_metrics.find { |metric| metric[:metric_id] == persisted_metric.id }
expect(target_metric).to be_a Hash
expect(target_metric).not_to include(:alert_path)
end
end
end
private
def all_metrics
dashboard[:panel_groups].flat_map do |group|
group[:panels].flat_map { |panel| panel[:metrics] }
end
end
end
# frozen_string_literal: true
require 'set'
module Gitlab
module Metrics
module Dashboard
module Stages
class AlertsInserter < BaseStage
include ::Gitlab::Utils::StrongMemoize
def transform!
return if metrics_with_alerts.empty?
for_metrics do |metric|
next unless metrics_with_alerts.include?(metric[:metric_id])
metric[:alert_path] = alert_path(metric[:metric_id], project, params[:environment])
end
end
private
def metrics_with_alerts
strong_memoize(:metrics_with_alerts) do
alerts = ::Projects::Prometheus::AlertsFinder
.new(project: project, environment: params[:environment])
.execute
Set.new(alerts.map(&:prometheus_metric_id))
end
end
def alert_path(metric_id, project, environment)
::Gitlab::Routing.url_helpers.project_prometheus_alert_path(project, metric_id, environment_id: environment.id, format: :json)
end
end
end
end
end
end
......@@ -14,9 +14,11 @@ describe Gitlab::Metrics::Dashboard::Processor do
Gitlab::Metrics::Dashboard::Stages::CustomMetricsInserter,
Gitlab::Metrics::Dashboard::Stages::CustomMetricsDetailsInserter,
Gitlab::Metrics::Dashboard::Stages::EndpointInserter,
Gitlab::Metrics::Dashboard::Stages::Sorter
Gitlab::Metrics::Dashboard::Stages::Sorter,
Gitlab::Metrics::Dashboard::Stages::AlertsInserter
]
end
let(:process_params) { [project, dashboard_yml, sequence, { environment: environment }] }
let(:dashboard) { described_class.new(*process_params).process }
......@@ -113,6 +115,54 @@ describe Gitlab::Metrics::Dashboard::Processor do
end
end
context 'when the dashboard references persisted metrics with alerts' do
let!(:alert) do
create(
:prometheus_alert,
environment: environment,
project: project,
prometheus_metric: persisted_metric
)
end
shared_examples_for 'has saved alerts' do
it 'includes an alert path' do
target_metric = all_metrics.find { |metric| metric[:metric_id] == persisted_metric.id }
expect(target_metric).to be_a Hash
expect(target_metric).to include(:alert_path)
expect(target_metric[:alert_path]).to include(
project.path,
persisted_metric.id.to_s,
environment.id.to_s
)
end
end
context 'that are shared across projects' do
let!(:persisted_metric) { create(:prometheus_metric, :common, identifier: 'metric_a1') }
it_behaves_like 'has saved alerts'
end
context 'when the project has associated metrics' do
let!(:persisted_metric) { create(:prometheus_metric, project: project, group: :business) }
it_behaves_like 'has saved alerts'
end
end
context 'when there are no alerts' do
let!(:persisted_metric) { create(:prometheus_metric, :common, identifier: 'metric_a1') }
it 'does not insert an alert_path' do
target_metric = all_metrics.find { |metric| metric[:metric_id] == persisted_metric.id }
expect(target_metric).to be_a Hash
expect(target_metric).not_to include(:alert_path)
end
end
shared_examples_for 'errors with message' do |expected_message|
it 'raises a DashboardLayoutError' do
error_class = Gitlab::Metrics::Dashboard::Errors::DashboardProcessingError
......
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