Commit 5631edfc authored by Vitali Tatarintev's avatar Vitali Tatarintev

Extract PrometheusAlertPresenter class

Extract `AlertManagement::PrometheusAlertPresenter`
from `AlertManagement::AlertPresenter`
to present Prometheus specific alerts.
parent 342aa58a
...@@ -164,6 +164,12 @@ module AlertManagement ...@@ -164,6 +164,12 @@ module AlertManagement
project.execute_services(hook_data, :alert_hooks) project.execute_services(hook_data, :alert_hooks)
end end
def present
return super(presenter_class: AlertManagement::PrometheusAlertPresenter) if prometheus?
super
end
private private
def hook_data def hook_data
......
...@@ -47,11 +47,7 @@ module AlertManagement ...@@ -47,11 +47,7 @@ module AlertManagement
end end
end end
def alert_markdown def alert_markdown; end
return unless alert.prometheus?
alerting_alert.alert_markdown
end
def metadata_list def metadata_list
metadata = [] metadata = []
...@@ -78,22 +74,14 @@ module AlertManagement ...@@ -78,22 +74,14 @@ module AlertManagement
end end
def details_list def details_list
return alerting_alert.annotation_list if alert.prometheus?
alert.details alert.details
.map { |label, value| list_item(label, value) } .map { |label, value| list_item(label, value) }
.join(MARKDOWN_LINE_BREAK) .join(MARKDOWN_LINE_BREAK)
end end
def metric_embed_for_alert def metric_embed_for_alert; end
return unless alert.prometheus?
alerting_alert.metric_embed_for_alert def full_query; end
end
def full_query
alert.prometheus? ? alerting_alert.full_query : nil
end
def list_item(key, value) def list_item(key, value)
"**#{key}:** #{value}".strip "**#{key}:** #{value}".strip
......
# frozen_string_literal: true
module AlertManagement
class PrometheusAlertPresenter < AlertManagement::AlertPresenter
private
def alert_markdown
alerting_alert.alert_markdown
end
def details_list
alerting_alert.annotation_list
end
def metric_embed_for_alert
alerting_alert.metric_embed_for_alert
end
def full_query
alerting_alert.full_query
end
end
end
...@@ -337,4 +337,22 @@ describe AlertManagement::Alert do ...@@ -337,4 +337,22 @@ describe AlertManagement::Alert do
expect { subject }.to change { alert.events }.by(1) expect { subject }.to change { alert.events }.by(1)
end end
end end
describe '#present' do
context 'when alert is generic' do
let(:alert) { build(:alert_management_alert) }
it 'uses generic alert presenter' do
expect(alert.present).to be_kind_of(AlertManagement::AlertPresenter)
end
end
context 'when alert is Prometheus specific' do
let(:alert) { build(:alert_management_alert, :prometheus) }
it 'uses Prometheus Alert presenter' do
expect(alert.present).to be_kind_of(AlertManagement::PrometheusAlertPresenter)
end
end
end
end end
...@@ -11,33 +11,15 @@ RSpec.describe AlertManagement::AlertPresenter do ...@@ -11,33 +11,15 @@ RSpec.describe AlertManagement::AlertPresenter do
'custom' => { 'param' => 73 } 'custom' => { 'param' => 73 }
} }
end end
let_it_be(:generic_alert) do let_it_be(:alert) do
create(:alert_management_alert, :with_host, :with_service, :with_monitoring_tool, project: project, payload: generic_payload) create(:alert_management_alert, :with_host, :with_service, :with_monitoring_tool, project: project, payload: generic_payload)
end end
let_it_be(:prometheus_payload) do
{
'annotations' => {
'title' => 'Alert title',
'gitlab_incident_markdown' => '**`markdown example`**',
'custom annotation' => 'custom annotation value'
},
'startsAt' => '2020-04-27T10:10:22.265949279Z',
'generatorURL' => 'http://8d467bd4607a:9090/graph?g0.expr=vector%281%29&g0.tab=1'
}
end
let_it_be(:prometheus_alert) do
create(:alert_management_alert, :prometheus, project: project, payload: prometheus_payload)
end
let(:alert) { generic_alert }
subject(:presenter) { described_class.new(alert) } subject(:presenter) { described_class.new(alert) }
describe '#issue_description' do describe '#issue_description' do
let(:markdown_line_break) { ' ' } let(:markdown_line_break) { ' ' }
context 'with generic alert' do
let(:alert) { generic_alert }
it 'returns an alert issue description' do it 'returns an alert issue description' do
expect(presenter.issue_description).to eq( expect(presenter.issue_description).to eq(
<<~MARKDOWN.chomp <<~MARKDOWN.chomp
...@@ -56,30 +38,4 @@ RSpec.describe AlertManagement::AlertPresenter do ...@@ -56,30 +38,4 @@ RSpec.describe AlertManagement::AlertPresenter do
) )
end end
end end
context 'with prometheus alert' do
let(:alert) { prometheus_alert }
it 'returns an alert issue description' do
expect(presenter.issue_description).to eq(
<<~MARKDOWN.chomp
#### Summary
**Start time:** #{presenter.start_time}#{markdown_line_break}
**Severity:** #{presenter.severity}#{markdown_line_break}
**full_query:** `vector(1)`#{markdown_line_break}
**Monitoring tool:** Prometheus
#### Alert Details
**custom annotation:** custom annotation value
---
**`markdown example`**
MARKDOWN
)
end
end
end
end end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe AlertManagement::PrometheusAlertPresenter do
let_it_be(:project) { create(:project) }
let_it_be(:prometheus_payload) do
{
'annotations' => {
'title' => 'Alert title',
'gitlab_incident_markdown' => '**`markdown example`**',
'custom annotation' => 'custom annotation value'
},
'startsAt' => '2020-04-27T10:10:22.265949279Z',
'generatorURL' => 'http://8d467bd4607a:9090/graph?g0.expr=vector%281%29&g0.tab=1'
}
end
let_it_be(:alert) do
create(:alert_management_alert, :prometheus, project: project, payload: prometheus_payload)
end
subject(:presenter) { described_class.new(alert) }
describe '#issue_description' do
let(:markdown_line_break) { ' ' }
it 'returns an alert issue description' do
expect(presenter.issue_description).to eq(
<<~MARKDOWN.chomp
#### Summary
**Start time:** #{presenter.start_time}#{markdown_line_break}
**Severity:** #{presenter.severity}#{markdown_line_break}
**full_query:** `vector(1)`#{markdown_line_break}
**Monitoring tool:** Prometheus
#### Alert Details
**custom annotation:** custom annotation value
---
**`markdown example`**
MARKDOWN
)
end
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