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
project.execute_services(hook_data, :alert_hooks)
end
def present
return super(presenter_class: AlertManagement::PrometheusAlertPresenter) if prometheus?
super
end
private
def hook_data
......
......@@ -47,11 +47,7 @@ module AlertManagement
end
end
def alert_markdown
return unless alert.prometheus?
alerting_alert.alert_markdown
end
def alert_markdown; end
def metadata_list
metadata = []
......@@ -78,22 +74,14 @@ module AlertManagement
end
def details_list
return alerting_alert.annotation_list if alert.prometheus?
alert.details
.map { |label, value| list_item(label, value) }
.join(MARKDOWN_LINE_BREAK)
end
def metric_embed_for_alert
return unless alert.prometheus?
def metric_embed_for_alert; end
alerting_alert.metric_embed_for_alert
end
def full_query
alert.prometheus? ? alerting_alert.full_query : nil
end
def full_query; end
def list_item(key, value)
"**#{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
expect { subject }.to change { alert.events }.by(1)
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
......@@ -11,75 +11,31 @@ RSpec.describe AlertManagement::AlertPresenter do
'custom' => { 'param' => 73 }
}
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)
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) }
describe '#issue_description' do
let(:markdown_line_break) { ' ' }
context 'with generic alert' do
let(:alert) { generic_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}
**Service:** #{alert.service}#{markdown_line_break}
**Monitoring tool:** #{alert.monitoring_tool}#{markdown_line_break}
**Hosts:** #{alert.hosts.join(' ')}
#### Alert Details
**custom.param:** 73
MARKDOWN
)
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
it 'returns an alert issue description' do
expect(presenter.issue_description).to eq(
<<~MARKDOWN.chomp
#### Summary
**custom annotation:** custom annotation value
**Start time:** #{presenter.start_time}#{markdown_line_break}
**Severity:** #{presenter.severity}#{markdown_line_break}
**Service:** #{alert.service}#{markdown_line_break}
**Monitoring tool:** #{alert.monitoring_tool}#{markdown_line_break}
**Hosts:** #{alert.hosts.join(' ')}
---
#### Alert Details
**`markdown example`**
MARKDOWN
)
end
**custom.param:** 73
MARKDOWN
)
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