Commit d9c27de6 authored by syasonik's avatar syasonik

Automatically embed metrics for gitlab alerts

Inserts a link to a metrics embed in the description of issues
created as a result of alerts from prometheus, for which the
alerting rules were configured through the GitLab UI.

If the alerting rule did not originate from GitLab, no changes
will occur in incident issues.
parent 184a4f17
......@@ -53,7 +53,7 @@ module Projects
#### Summary
#{metadata_list}
#{alert_details}
#{alert_details}#{metric_embed_for_alert}
MARKDOWN
end
......@@ -118,6 +118,10 @@ module Projects
def host_links
Array(hosts.value).join(' ')
end
def metric_embed_for_alert; end
end
end
end
Projects::Prometheus::AlertPresenter.prepend_if_ee('EE::Projects::Prometheus::AlertPresenter')
---
title: Automatically include embedded metrics for GitLab alert incidents
merge_request: 25277
author:
type: added
......@@ -105,3 +105,5 @@ Incident Management features can be easily enabled & disabled via the Project se
#### Auto-creation
GitLab Issues can automatically be created as a result of an Alert notification. An Issue created this way will contain error information to help you further debug the error.
For [GitLab-managed alerting rules](../project/integrations/prometheus.md#setting-up-alerts-for-prometheus-metrics-ultimate), the issue will include an embedded chart for the query corresponding to the alert. The chart will show an hour of data surrounding the starting point of the incident, 30 minutes before and after.
# frozen_string_literal: true
module EE
module Projects
module Prometheus
module AlertPresenter
extend ::Gitlab::Utils::Override
METRIC_TIME_WINDOW = 30.minutes
override :metric_embed_for_alert
def metric_embed_for_alert
return unless gitlab_alert
time = starts_at ? Time.rfc3339(starts_at) : Time.current
url = metrics_dashboard_project_prometheus_alert_url(
project,
gitlab_alert.prometheus_metric_id,
environment_id: environment.id,
start: format_embed_timestamp(time - METRIC_TIME_WINDOW),
end: format_embed_timestamp(time + METRIC_TIME_WINDOW)
)
"\n[](#{url})"
end
def format_embed_timestamp(timestamp)
timestamp.utc.strftime('%FT%TZ')
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe Projects::Prometheus::AlertPresenter do
let_it_be(:project) { create(:project) }
let(:presenter) { described_class.new(alert) }
let(:payload) { {} }
let(:alert) { create(:alerting_alert, project: project, payload: payload) }
describe '#issue_summary_markdown' do
let(:markdown_line_break) { ' ' }
subject { presenter.issue_summary_markdown }
context 'with gitlab alert' do
let(:gitlab_alert) { create(:prometheus_alert, project: project) }
let(:metric_id) { gitlab_alert.prometheus_metric_id }
let(:env_id) { gitlab_alert.environment_id }
let(:starts_at) { '2018-03-12T09:06:00Z' }
let(:expected_markdown) do
<<~MARKDOWN.chomp
#### Summary
**Start time:** #{presenter.starts_at}#{markdown_line_break}
**full_query:** `avg(metric) > 1.0`
[](http://localhost/#{project.full_path}/prometheus/alerts/#{metric_id}/metrics_dashboard?end=2018-03-12T09%3A36%3A00Z&environment_id=#{env_id}&start=2018-03-12T08%3A36%3A00Z)
MARKDOWN
end
before do
payload['labels'] = { 'gitlab_alert_id' => metric_id }
end
context 'without a starting time available' do
around do |example|
Timecop.freeze(starts_at) { example.run }
end
it { is_expected.to eq(expected_markdown) }
end
context 'with a starting time available' do
before do
payload['startsAt'] = starts_at
end
it { is_expected.to eq(expected_markdown) }
end
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