Commit 40ccd348 authored by charlie ablett's avatar charlie ablett

Merge branch '216449-extract-fingerprint-from-alert-events-models' into 'master'

Resolve "Follow-up from "Persist Prometheus alerts in the DB""

Closes #216449

See merge request gitlab-org/gitlab!31611
parents ab09a438 0bc2c922
...@@ -34,10 +34,4 @@ class PrometheusAlertEvent < ApplicationRecord ...@@ -34,10 +34,4 @@ class PrometheusAlertEvent < ApplicationRecord
def self.status_value_for(name) def self.status_value_for(name)
state_machines[:status].states[name].value state_machines[:status].states[name].value
end end
def self.payload_key_for(gitlab_alert_id, started_at)
plain = [gitlab_alert_id, started_at].join('/')
Digest::SHA1.hexdigest(plain)
end
end end
...@@ -15,10 +15,4 @@ class SelfManagedPrometheusAlertEvent < ApplicationRecord ...@@ -15,10 +15,4 @@ class SelfManagedPrometheusAlertEvent < ApplicationRecord
yield event if block_given? yield event if block_given?
end end
end end
def self.payload_key_for(started_at, alert_name, query_expression)
plain = [started_at, alert_name, query_expression].join('/')
Digest::SHA1.hexdigest(plain)
end
end end
...@@ -40,17 +40,13 @@ module Projects ...@@ -40,17 +40,13 @@ module Projects
def create_managed_prometheus_alert_event(parsed_alert) def create_managed_prometheus_alert_event(parsed_alert)
alert = find_alert(parsed_alert.metric_id) alert = find_alert(parsed_alert.metric_id)
payload_key = PrometheusAlertEvent.payload_key_for(parsed_alert.metric_id, parsed_alert.starts_at_raw) event = PrometheusAlertEvent.find_or_initialize_by_payload_key(parsed_alert.project, alert, parsed_alert.gitlab_fingerprint)
event = PrometheusAlertEvent.find_or_initialize_by_payload_key(parsed_alert.project, alert, payload_key)
set_status(parsed_alert, event) set_status(parsed_alert, event)
end end
def create_self_managed_prometheus_alert_event(parsed_alert) def create_self_managed_prometheus_alert_event(parsed_alert)
payload_key = SelfManagedPrometheusAlertEvent.payload_key_for(parsed_alert.starts_at_raw, parsed_alert.title, parsed_alert.full_query) event = SelfManagedPrometheusAlertEvent.find_or_initialize_by_payload_key(parsed_alert.project, parsed_alert.gitlab_fingerprint) do |event|
event = SelfManagedPrometheusAlertEvent.find_or_initialize_by_payload_key(parsed_alert.project, payload_key) do |event|
event.environment = parsed_alert.environment event.environment = parsed_alert.environment
event.title = parsed_alert.title event.title = parsed_alert.title
event.query_expression = parsed_alert.full_query event.query_expression = parsed_alert.full_query
......
...@@ -41,23 +41,11 @@ module IncidentManagement ...@@ -41,23 +41,11 @@ module IncidentManagement
end end
def find_gitlab_managed_event(alert) def find_gitlab_managed_event(alert)
payload_key = payload_key_for_alert(alert) PrometheusAlertEvent.find_by_payload_key(alert.gitlab_fingerprint)
PrometheusAlertEvent.find_by_payload_key(payload_key)
end end
def find_self_managed_event(alert) def find_self_managed_event(alert)
payload_key = payload_key_for_alert(alert) SelfManagedPrometheusAlertEvent.find_by_payload_key(alert.gitlab_fingerprint)
SelfManagedPrometheusAlertEvent.find_by_payload_key(payload_key)
end
def payload_key_for_alert(alert)
if alert.gitlab_managed?
PrometheusAlertEvent.payload_key_for(alert.metric_id, alert.starts_at_raw)
else
SelfManagedPrometheusAlertEvent.payload_key_for(alert.starts_at_raw, alert.title, alert.full_query)
end
end end
def create_issue(project, alert) def create_issue(project, alert)
......
...@@ -121,9 +121,9 @@ module Gitlab ...@@ -121,9 +121,9 @@ module Gitlab
def plain_gitlab_fingerprint def plain_gitlab_fingerprint
if gitlab_managed? if gitlab_managed?
[metric_id, starts_at].join('/') [metric_id, starts_at_raw].join('/')
else # self managed else # self managed
[starts_at, title, full_query].join('/') [starts_at_raw, title, full_query].join('/')
end end
end end
......
...@@ -253,7 +253,7 @@ describe Gitlab::Alerting::Alert do ...@@ -253,7 +253,7 @@ describe Gitlab::Alerting::Alert do
include_context 'gitlab alert' include_context 'gitlab alert'
it 'returns a fingerprint' do it 'returns a fingerprint' do
plain_fingerprint = [alert.metric_id, alert.starts_at].join('/') plain_fingerprint = [alert.metric_id, alert.starts_at_raw].join('/')
is_expected.to eq(Digest::SHA1.hexdigest(plain_fingerprint)) is_expected.to eq(Digest::SHA1.hexdigest(plain_fingerprint))
end end
...@@ -263,7 +263,7 @@ describe Gitlab::Alerting::Alert do ...@@ -263,7 +263,7 @@ describe Gitlab::Alerting::Alert do
include_context 'full query' include_context 'full query'
it 'returns a fingerprint' do it 'returns a fingerprint' do
plain_fingerprint = [alert.starts_at, alert.title, alert.full_query].join('/') plain_fingerprint = [alert.starts_at_raw, alert.title, alert.full_query].join('/')
is_expected.to eq(Digest::SHA1.hexdigest(plain_fingerprint)) is_expected.to eq(Digest::SHA1.hexdigest(plain_fingerprint))
end end
......
...@@ -89,9 +89,9 @@ describe Projects::Prometheus::Alerts::CreateEventsService do ...@@ -89,9 +89,9 @@ describe Projects::Prometheus::Alerts::CreateEventsService do
context 'with a resolved payload' do context 'with a resolved payload' do
let(:started_at) { truncate_to_second(Time.current) } let(:started_at) { truncate_to_second(Time.current) }
let(:ended_at) { started_at + 1 } let(:ended_at) { started_at + 1 }
let(:payload_key) { PrometheusAlertEvent.payload_key_for(alert.prometheus_metric_id, utc_rfc3339(started_at)) }
let(:resolved_event) { alert_payload(status: 'resolved', started_at: started_at, ended_at: ended_at) } let(:resolved_event) { alert_payload(status: 'resolved', started_at: started_at, ended_at: ended_at) }
let(:alerts_payload) { { 'alerts' => [resolved_event] } } let(:alerts_payload) { { 'alerts' => [resolved_event] } }
let(:payload_key) { Gitlab::Alerting::Alert.new(project: project, payload: resolved_event).gitlab_fingerprint }
context 'with a matching firing event' do context 'with a matching firing event' do
before do before do
......
...@@ -6,7 +6,7 @@ describe IncidentManagement::ProcessPrometheusAlertWorker do ...@@ -6,7 +6,7 @@ describe IncidentManagement::ProcessPrometheusAlertWorker do
describe '#perform' do describe '#perform' do
let_it_be(:project) { create(:project) } let_it_be(:project) { create(:project) }
let_it_be(:prometheus_alert) { create(:prometheus_alert, project: project) } let_it_be(:prometheus_alert) { create(:prometheus_alert, project: project) }
let_it_be(:payload_key) { PrometheusAlertEvent.payload_key_for(prometheus_alert.prometheus_metric_id, prometheus_alert.created_at.rfc3339) } let(:payload_key) { Gitlab::Alerting::Alert.new(project: project, payload: alert_params).gitlab_fingerprint }
let!(:prometheus_alert_event) { create(:prometheus_alert_event, prometheus_alert: prometheus_alert, payload_key: payload_key) } let!(:prometheus_alert_event) { create(:prometheus_alert_event, prometheus_alert: prometheus_alert, payload_key: payload_key) }
let(:alert_params) do let(:alert_params) do
...@@ -107,7 +107,6 @@ describe IncidentManagement::ProcessPrometheusAlertWorker do ...@@ -107,7 +107,6 @@ describe IncidentManagement::ProcessPrometheusAlertWorker do
let(:starts_at) { Time.now.rfc3339 } let(:starts_at) { Time.now.rfc3339 }
let!(:prometheus_alert_event) do let!(:prometheus_alert_event) do
payload_key = SelfManagedPrometheusAlertEvent.payload_key_for(starts_at, alert_name, 'vector(1)')
create(:self_managed_prometheus_alert_event, project: project, payload_key: payload_key) create(:self_managed_prometheus_alert_event, project: project, payload_key: payload_key)
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