2020-06-23 14:09:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module AlertManagement
|
|
|
|
class AlertPresenter < Gitlab::View::Presenter::Delegated
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
include IncidentManagement::Settings
|
2020-08-13 14:10:36 -04:00
|
|
|
include ActionView::Helpers::UrlHelper
|
2020-06-23 14:09:28 -04:00
|
|
|
|
2020-09-15 08:09:30 -04:00
|
|
|
MARKDOWN_LINE_BREAK = " \n"
|
|
|
|
HORIZONTAL_LINE = "\n\n---\n\n"
|
|
|
|
|
|
|
|
delegate :metrics_dashboard_url, :runbook, to: :parsed_payload
|
2020-06-23 14:09:28 -04:00
|
|
|
|
2020-09-30 20:10:16 -04:00
|
|
|
def initialize(alert, **attributes)
|
2020-06-23 14:09:28 -04:00
|
|
|
super
|
|
|
|
|
|
|
|
@alert = alert
|
|
|
|
@project = alert.project
|
|
|
|
end
|
|
|
|
|
|
|
|
def issue_description
|
|
|
|
[
|
|
|
|
issue_summary_markdown,
|
|
|
|
alert_markdown,
|
|
|
|
incident_management_setting.issue_template_content
|
2020-09-15 08:09:30 -04:00
|
|
|
].compact.join(HORIZONTAL_LINE)
|
2020-06-23 14:09:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def start_time
|
|
|
|
started_at&.strftime('%d %B %Y, %-l:%M%p (%Z)')
|
|
|
|
end
|
|
|
|
|
2020-08-11 11:10:08 -04:00
|
|
|
def details_url
|
2020-08-13 14:10:36 -04:00
|
|
|
details_project_alert_management_url(project, alert.iid)
|
2020-08-11 11:10:08 -04:00
|
|
|
end
|
|
|
|
|
2020-09-14 08:09:34 -04:00
|
|
|
def details
|
|
|
|
Gitlab::Utils::InlineHash.merge_keys(payload)
|
|
|
|
end
|
|
|
|
|
2020-09-22 08:09:39 -04:00
|
|
|
def show_incident_issues_link?
|
|
|
|
project.incident_management_setting&.create_issue?
|
|
|
|
end
|
|
|
|
|
|
|
|
def show_performance_dashboard_link?
|
|
|
|
prometheus_alert.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def incident_issues_link
|
2020-12-08 04:09:41 -05:00
|
|
|
project_incidents_url(project)
|
2020-09-22 08:09:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def performance_dashboard_link
|
|
|
|
if environment
|
|
|
|
metrics_project_environment_url(project, environment)
|
|
|
|
else
|
|
|
|
metrics_project_environments_url(project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def email_title
|
|
|
|
[environment&.name, query_title].compact.join(': ')
|
|
|
|
end
|
|
|
|
|
2020-08-13 14:10:36 -04:00
|
|
|
private
|
|
|
|
|
2020-06-23 14:09:28 -04:00
|
|
|
attr_reader :alert, :project
|
2020-09-15 08:09:30 -04:00
|
|
|
delegate :alert_markdown, :full_query, to: :parsed_payload
|
2020-06-23 14:09:28 -04:00
|
|
|
|
2020-09-15 08:09:30 -04:00
|
|
|
def issue_summary_markdown
|
|
|
|
<<~MARKDOWN.chomp
|
|
|
|
#{metadata_list}
|
2020-09-16 08:10:15 -04:00
|
|
|
#{metric_embed_for_alert}
|
2020-09-15 08:09:30 -04:00
|
|
|
MARKDOWN
|
2020-06-23 14:09:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def metadata_list
|
|
|
|
metadata = []
|
|
|
|
|
|
|
|
metadata << list_item('Start time', start_time)
|
|
|
|
metadata << list_item('Severity', severity)
|
|
|
|
metadata << list_item('full_query', backtick(full_query)) if full_query
|
|
|
|
metadata << list_item('Service', service) if service
|
|
|
|
metadata << list_item('Monitoring tool', monitoring_tool) if monitoring_tool
|
|
|
|
metadata << list_item('Hosts', host_links) if hosts.any?
|
2020-07-07 05:08:57 -04:00
|
|
|
metadata << list_item('Description', description) if description.present?
|
2020-08-11 11:10:08 -04:00
|
|
|
metadata << list_item('GitLab alert', details_url) if details_url.present?
|
2020-06-23 14:09:28 -04:00
|
|
|
|
|
|
|
metadata.join(MARKDOWN_LINE_BREAK)
|
|
|
|
end
|
|
|
|
|
2020-09-15 08:09:30 -04:00
|
|
|
def metric_embed_for_alert
|
|
|
|
"\n[](#{metrics_dashboard_url})" if metrics_dashboard_url
|
|
|
|
end
|
2020-06-23 14:09:28 -04:00
|
|
|
|
|
|
|
def list_item(key, value)
|
|
|
|
"**#{key}:** #{value}".strip
|
|
|
|
end
|
|
|
|
|
|
|
|
def backtick(value)
|
|
|
|
"`#{value}`"
|
|
|
|
end
|
|
|
|
|
|
|
|
def host_links
|
|
|
|
hosts.join(' ')
|
|
|
|
end
|
2020-09-22 08:09:39 -04:00
|
|
|
|
|
|
|
def query_title
|
|
|
|
return title unless prometheus_alert
|
|
|
|
|
|
|
|
"#{prometheus_alert.title} #{prometheus_alert.computed_operator} #{prometheus_alert.threshold} for 5 minutes"
|
|
|
|
end
|
2020-06-23 14:09:28 -04:00
|
|
|
end
|
|
|
|
end
|