Commit 474f7ef2 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'pl-refactor-prometheus-alert-fired-email' into 'master'

Refactor sending Prometheus alert mails

See merge request gitlab-org/gitlab-ee!9407
parents 71e6a91a c069134f
......@@ -20,26 +20,21 @@ module EE
subject: subject('Mirror user changed'))
end
# rubocop: disable CodeReuse/ActiveRecord
def prometheus_alert_fired_email(project_id, user_id, alert_params)
alert_metric_id = alert_params["labels"]["gitlab_alert_id"]
alert_metric_id = alert_params.dig('labels', 'gitlab_alert_id')
@project = ::Project.find_by(id: project_id)
return unless @project
@project = ::Project.find(project_id)
user = ::User.find(user_id)
@alert = @project.prometheus_alerts.find_by(prometheus_metric: alert_metric_id)
@alert = @project.prometheus_alerts.for_metric(alert_metric_id).first
return unless @alert
@environment = @alert.environment
user = ::User.find_by(id: user_id)
return unless user
subject_text = "Alert: #{@environment.name} - #{@alert.title} #{@alert.computed_operator} #{@alert.threshold} for 5 minutes"
mail(to: user.notification_email, subject: subject(subject_text))
end
# rubocop: enable CodeReuse/ActiveRecord
end
end
end
# frozen_string_literal: true
require 'spec_helper'
require 'email_spec'
describe EE::Emails::Projects do
include EmailSpec::Matchers
include_context 'gitlab email notification'
shared_examples 'no email' do
it 'does not send mail' do
expect(subject.message).to be_a_kind_of(ActionMailer::Base::NullMail)
end
end
set(:user) { create(:user) }
describe '#prometheus_alert_fired_email' do
subject do
Notify.prometheus_alert_fired_email(project.id, user.id, alert_params)
end
context 'with an alert' do
let(:alert_params) do
{
'labels' => {
'gitlab_alert_id' => alert.prometheus_metric_id.to_s
}
}
end
let(:environment) { alert.environment }
let(:metrics_url) do
metrics_project_environment_url(project, environment)
end
let!(:alert) { create(:prometheus_alert, project: project) }
it_behaves_like 'an email sent from GitLab'
it_behaves_like 'it should not have Gmail Actions links'
it_behaves_like 'a user cannot unsubscribe through footer link'
it 'has expected subject' do
aggregate_failures do
is_expected.to have_subject(/Alert:/)
is_expected.to have_subject(/#{environment.name}/)
title = "#{alert.title} #{alert.computed_operator} #{alert.threshold}"
is_expected.to have_subject(/#{title}/)
end
end
it 'has expected content' do
is_expected.to have_body_text('An alert has been triggered')
is_expected.to have_body_text(project.full_path)
is_expected.to have_body_text(environment.name)
is_expected.to have_body_text(alert.full_query)
is_expected.to have_body_text(metrics_url)
end
end
context 'without an alert' do
let(:alert_params) { {} }
it_behaves_like 'no email'
end
context 'with an unknown alert' do
let(:alert_params) do
{
'labels' => {
'gitlab_alert_id' => 'unknown'
}
}
end
it_behaves_like 'no email'
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