Commit bbd15ea4 authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch '204908-ensure-correct-prometheus-alert-found' into 'master'

Resolve "Firing prometheus alerts configured by gitlab are not found by environment"

Closes #204908

See merge request gitlab-org/gitlab!29119
parents 3d50086e 3f35eade
...@@ -56,7 +56,8 @@ class PrometheusAlert < ApplicationRecord ...@@ -56,7 +56,8 @@ class PrometheusAlert < ApplicationRecord
"for" => "5m", "for" => "5m",
"labels" => { "labels" => {
"gitlab" => "hook", "gitlab" => "hook",
"gitlab_alert_id" => prometheus_metric_id "gitlab_alert_id" => prometheus_metric_id,
"gitlab_prometheus_alert_id" => id
} }
} }
end end
......
---
title: Prevent wrong environment being used when processing Prometheus alert
merge_request: 29119
author:
type: fixed
...@@ -21,6 +21,12 @@ module Gitlab ...@@ -21,6 +21,12 @@ module Gitlab
end end
end end
def gitlab_prometheus_alert_id
strong_memoize(:gitlab_prometheus_alert_id) do
payload&.dig('labels', 'gitlab_prometheus_alert_id')
end
end
def title def title
strong_memoize(:title) do strong_memoize(:title) do
gitlab_alert&.title || parse_title_from_payload gitlab_alert&.title || parse_title_from_payload
...@@ -120,12 +126,19 @@ module Gitlab ...@@ -120,12 +126,19 @@ module Gitlab
end end
def parse_gitlab_alert_from_payload def parse_gitlab_alert_from_payload
return unless metric_id alerts_found = matching_gitlab_alerts
return if alerts_found.blank? || alerts_found.size > 1
alerts_found.first
end
def matching_gitlab_alerts
return unless metric_id || gitlab_prometheus_alert_id
Projects::Prometheus::AlertsFinder Projects::Prometheus::AlertsFinder
.new(project: project, metric: metric_id) .new(project: project, metric: metric_id, id: gitlab_prometheus_alert_id)
.execute .execute
.first
end end
def parse_title_from_payload def parse_title_from_payload
......
...@@ -9,11 +9,14 @@ describe Gitlab::Alerting::Alert do ...@@ -9,11 +9,14 @@ describe Gitlab::Alerting::Alert do
let(:payload) { {} } let(:payload) { {} }
shared_context 'gitlab alert' do shared_context 'gitlab alert' do
let(:gitlab_alert_id) { gitlab_alert.prometheus_metric_id.to_s }
let!(:gitlab_alert) { create(:prometheus_alert, project: project) } let!(:gitlab_alert) { create(:prometheus_alert, project: project) }
let(:gitlab_alert_id) { gitlab_alert.id }
before do before do
payload['labels'] = { 'gitlab_alert_id' => gitlab_alert_id } payload['labels'] = {
'gitlab_alert_id' => gitlab_alert.prometheus_metric_id.to_s,
'gitlab_prometheus_alert_id' => gitlab_alert_id
}
end end
end end
...@@ -68,6 +71,41 @@ describe Gitlab::Alerting::Alert do ...@@ -68,6 +71,41 @@ describe Gitlab::Alerting::Alert do
it { is_expected.to be_nil } it { is_expected.to be_nil }
end end
context 'when two alerts with the same metric exist' do
include_context 'gitlab alert'
let!(:second_gitlab_alert) do
create(:prometheus_alert,
project: project,
prometheus_metric_id: gitlab_alert.prometheus_metric_id
)
end
context 'alert id given in params' do
before do
payload['labels'] = {
'gitlab_alert_id' => gitlab_alert.prometheus_metric_id.to_s,
'gitlab_prometheus_alert_id' => second_gitlab_alert.id
}
end
it { is_expected.to eq(second_gitlab_alert) }
end
context 'metric id given in params' do
# This tests the case when two alerts are found, as metric id
# is not unique.
# Note the metric id was incorrectly named as 'gitlab_alert_id'
# in PrometheusAlert#to_param.
before do
payload['labels'] = { 'gitlab_alert_id' => gitlab_alert.prometheus_metric_id }
end
it { is_expected.to be_nil }
end
end
end end
describe '#title' do describe '#title' do
......
...@@ -96,7 +96,8 @@ describe PrometheusAlert do ...@@ -96,7 +96,8 @@ describe PrometheusAlert do
"for" => "5m", "for" => "5m",
"labels" => { "labels" => {
"gitlab" => "hook", "gitlab" => "hook",
"gitlab_alert_id" => metric.id "gitlab_alert_id" => metric.id,
"gitlab_prometheus_alert_id" => subject.id
}) })
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