Commit 7cc35451 authored by Sean Arnold's avatar Sean Arnold Committed by Mayra Cabrera

Add prometheus_alert_id to Alert management alerts

- Add model relations and database migrations
parent 61903a35
...@@ -30,6 +30,8 @@ module AlertManagement ...@@ -30,6 +30,8 @@ module AlertManagement
belongs_to :project belongs_to :project
belongs_to :issue, optional: true belongs_to :issue, optional: true
belongs_to :prometheus_alert, optional: true
belongs_to :environment, optional: true
has_many :alert_assignees, inverse_of: :alert has_many :alert_assignees, inverse_of: :alert
has_many :assignees, through: :alert_assignees has_many :assignees, through: :alert_assignees
......
...@@ -21,6 +21,7 @@ class Environment < ApplicationRecord ...@@ -21,6 +21,7 @@ class Environment < ApplicationRecord
has_many :prometheus_alerts, inverse_of: :environment has_many :prometheus_alerts, inverse_of: :environment
has_many :metrics_dashboard_annotations, class_name: 'Metrics::Dashboard::Annotation', inverse_of: :environment has_many :metrics_dashboard_annotations, class_name: 'Metrics::Dashboard::Annotation', inverse_of: :environment
has_many :self_managed_prometheus_alert_events, inverse_of: :environment has_many :self_managed_prometheus_alert_events, inverse_of: :environment
has_many :alert_management_alerts, class_name: 'AlertManagement::Alert', inverse_of: :environment
has_one :last_deployment, -> { success.order('deployments.id DESC') }, class_name: 'Deployment' has_one :last_deployment, -> { success.order('deployments.id DESC') }, class_name: 'Deployment'
has_one :last_deployable, through: :last_deployment, source: 'deployable', source_type: 'CommitStatus' has_one :last_deployable, through: :last_deployment, source: 'deployable', source_type: 'CommitStatus'
......
...@@ -16,6 +16,7 @@ class PrometheusAlert < ApplicationRecord ...@@ -16,6 +16,7 @@ class PrometheusAlert < ApplicationRecord
has_many :prometheus_alert_events, inverse_of: :prometheus_alert has_many :prometheus_alert_events, inverse_of: :prometheus_alert
has_many :related_issues, through: :prometheus_alert_events has_many :related_issues, through: :prometheus_alert_events
has_many :alert_management_alerts, class_name: 'AlertManagement::Alert', inverse_of: :prometheus_alert
after_save :clear_prometheus_adapter_cache! after_save :clear_prometheus_adapter_cache!
after_destroy :clear_prometheus_adapter_cache! after_destroy :clear_prometheus_adapter_cache!
......
---
title: Add prometheus_alert_id and environment_id to Alert management alerts
merge_request: 34995
author:
type: added
# frozen_string_literal: true
class AddPrometheusAlertIdToAlertManagementAlerts < ActiveRecord::Migration[6.0]
DOWNTIME = false
def up
add_column :alert_management_alerts, :prometheus_alert_id, :integer
add_column :alert_management_alerts, :environment_id, :integer
end
def down
remove_column :alert_management_alerts, :prometheus_alert_id
remove_column :alert_management_alerts, :environment_id
end
end
# frozen_string_literal: true
class AddIdxAndFkForPrometheusAndEnvironmentToAlertManagementAlerts < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
add_concurrent_index :alert_management_alerts, :prometheus_alert_id, where: 'prometheus_alert_id is not null'
add_concurrent_foreign_key :alert_management_alerts, :prometheus_alerts, column: :prometheus_alert_id, on_delete: :cascade
add_concurrent_index :alert_management_alerts, :environment_id, where: 'environment_id is not null'
add_concurrent_foreign_key :alert_management_alerts, :environments, column: :environment_id, on_delete: :nullify
end
def down
remove_concurrent_index :alert_management_alerts, :prometheus_alert_id
remove_foreign_key_without_error :alert_management_alerts, column: :prometheus_alert_id
remove_concurrent_index :alert_management_alerts, :environment_id
remove_foreign_key_without_error :alert_management_alerts, column: :environment_id
end
end
...@@ -78,6 +78,8 @@ CREATE TABLE public.alert_management_alerts ( ...@@ -78,6 +78,8 @@ CREATE TABLE public.alert_management_alerts (
monitoring_tool text, monitoring_tool text,
hosts text[] DEFAULT '{}'::text[] NOT NULL, hosts text[] DEFAULT '{}'::text[] NOT NULL,
payload jsonb DEFAULT '{}'::jsonb NOT NULL, payload jsonb DEFAULT '{}'::jsonb NOT NULL,
prometheus_alert_id integer,
environment_id integer,
CONSTRAINT check_2df3e2fdc1 CHECK ((char_length(monitoring_tool) <= 100)), CONSTRAINT check_2df3e2fdc1 CHECK ((char_length(monitoring_tool) <= 100)),
CONSTRAINT check_5e9e57cadb CHECK ((char_length(description) <= 1000)), CONSTRAINT check_5e9e57cadb CHECK ((char_length(description) <= 1000)),
CONSTRAINT check_bac14dddde CHECK ((char_length(service) <= 100)), CONSTRAINT check_bac14dddde CHECK ((char_length(service) <= 100)),
...@@ -9352,12 +9354,16 @@ CREATE INDEX index_alert_assignees_on_alert_id ON public.alert_management_alert_ ...@@ -9352,12 +9354,16 @@ CREATE INDEX index_alert_assignees_on_alert_id ON public.alert_management_alert_
CREATE UNIQUE INDEX index_alert_assignees_on_user_id_and_alert_id ON public.alert_management_alert_assignees USING btree (user_id, alert_id); CREATE UNIQUE INDEX index_alert_assignees_on_user_id_and_alert_id ON public.alert_management_alert_assignees USING btree (user_id, alert_id);
CREATE INDEX index_alert_management_alerts_on_environment_id ON public.alert_management_alerts USING btree (environment_id) WHERE (environment_id IS NOT NULL);
CREATE INDEX index_alert_management_alerts_on_issue_id ON public.alert_management_alerts USING btree (issue_id); CREATE INDEX index_alert_management_alerts_on_issue_id ON public.alert_management_alerts USING btree (issue_id);
CREATE UNIQUE INDEX index_alert_management_alerts_on_project_id_and_fingerprint ON public.alert_management_alerts USING btree (project_id, fingerprint); CREATE UNIQUE INDEX index_alert_management_alerts_on_project_id_and_fingerprint ON public.alert_management_alerts USING btree (project_id, fingerprint);
CREATE UNIQUE INDEX index_alert_management_alerts_on_project_id_and_iid ON public.alert_management_alerts USING btree (project_id, iid); CREATE UNIQUE INDEX index_alert_management_alerts_on_project_id_and_iid ON public.alert_management_alerts USING btree (project_id, iid);
CREATE INDEX index_alert_management_alerts_on_prometheus_alert_id ON public.alert_management_alerts USING btree (prometheus_alert_id) WHERE (prometheus_alert_id IS NOT NULL);
CREATE UNIQUE INDEX index_alert_user_mentions_on_alert_id ON public.alert_management_alert_user_mentions USING btree (alert_management_alert_id) WHERE (note_id IS NULL); CREATE UNIQUE INDEX index_alert_user_mentions_on_alert_id ON public.alert_management_alert_user_mentions USING btree (alert_management_alert_id) WHERE (note_id IS NULL);
CREATE UNIQUE INDEX index_alert_user_mentions_on_alert_id_and_note_id ON public.alert_management_alert_user_mentions USING btree (alert_management_alert_id, note_id); CREATE UNIQUE INDEX index_alert_user_mentions_on_alert_id_and_note_id ON public.alert_management_alert_user_mentions USING btree (alert_management_alert_id, note_id);
...@@ -11538,6 +11544,9 @@ ALTER TABLE ONLY public.geo_event_log ...@@ -11538,6 +11544,9 @@ ALTER TABLE ONLY public.geo_event_log
ALTER TABLE ONLY public.ci_build_trace_sections ALTER TABLE ONLY public.ci_build_trace_sections
ADD CONSTRAINT fk_4ebe41f502 FOREIGN KEY (build_id) REFERENCES public.ci_builds(id) ON DELETE CASCADE; ADD CONSTRAINT fk_4ebe41f502 FOREIGN KEY (build_id) REFERENCES public.ci_builds(id) ON DELETE CASCADE;
ALTER TABLE ONLY public.alert_management_alerts
ADD CONSTRAINT fk_51ab4b6089 FOREIGN KEY (prometheus_alert_id) REFERENCES public.prometheus_alerts(id) ON DELETE CASCADE;
ALTER TABLE ONLY public.path_locks ALTER TABLE ONLY public.path_locks
ADD CONSTRAINT fk_5265c98f24 FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE CASCADE; ADD CONSTRAINT fk_5265c98f24 FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE CASCADE;
...@@ -11745,6 +11754,9 @@ ALTER TABLE ONLY public.merge_requests ...@@ -11745,6 +11754,9 @@ ALTER TABLE ONLY public.merge_requests
ALTER TABLE ONLY public.epics ALTER TABLE ONLY public.epics
ADD CONSTRAINT fk_aa5798e761 FOREIGN KEY (closed_by_id) REFERENCES public.users(id) ON DELETE SET NULL; ADD CONSTRAINT fk_aa5798e761 FOREIGN KEY (closed_by_id) REFERENCES public.users(id) ON DELETE SET NULL;
ALTER TABLE ONLY public.alert_management_alerts
ADD CONSTRAINT fk_aad61aedca FOREIGN KEY (environment_id) REFERENCES public.environments(id) ON DELETE SET NULL;
ALTER TABLE ONLY public.identities ALTER TABLE ONLY public.identities
ADD CONSTRAINT fk_aade90f0fc FOREIGN KEY (saml_provider_id) REFERENCES public.saml_providers(id) ON DELETE CASCADE; ADD CONSTRAINT fk_aade90f0fc FOREIGN KEY (saml_provider_id) REFERENCES public.saml_providers(id) ON DELETE CASCADE;
...@@ -14156,6 +14168,7 @@ COPY "schema_migrations" (version) FROM STDIN; ...@@ -14156,6 +14168,7 @@ COPY "schema_migrations" (version) FROM STDIN;
20200618134723 20200618134723
20200619154527 20200619154527
20200619154528 20200619154528
20200622040750
20200622070606 20200622070606
20200622070620 20200622070620
20200622095419 20200622095419
...@@ -14165,5 +14178,6 @@ COPY "schema_migrations" (version) FROM STDIN; ...@@ -14165,5 +14178,6 @@ COPY "schema_migrations" (version) FROM STDIN;
20200623000320 20200623000320
20200623121135 20200623121135
20200624075411 20200624075411
20200625045442
\. \.
...@@ -5,7 +5,9 @@ require 'spec_helper' ...@@ -5,7 +5,9 @@ require 'spec_helper'
RSpec.describe AlertManagement::Alert do RSpec.describe AlertManagement::Alert do
describe 'associations' do describe 'associations' do
it { is_expected.to belong_to(:project) } it { is_expected.to belong_to(:project) }
it { is_expected.to belong_to(:issue) } it { is_expected.to belong_to(:issue).optional }
it { is_expected.to belong_to(:prometheus_alert).optional }
it { is_expected.to belong_to(:environment).optional }
it { is_expected.to have_many(:assignees).through(:alert_assignees) } it { is_expected.to have_many(:assignees).through(:alert_assignees) }
it { is_expected.to have_many(:notes) } it { is_expected.to have_many(:notes) }
it { is_expected.to have_many(:ordered_notes) } it { is_expected.to have_many(:ordered_notes) }
......
...@@ -18,6 +18,7 @@ RSpec.describe Environment, :use_clean_rails_memory_store_caching do ...@@ -18,6 +18,7 @@ RSpec.describe Environment, :use_clean_rails_memory_store_caching do
it { is_expected.to belong_to(:project).required } it { is_expected.to belong_to(:project).required }
it { is_expected.to have_many(:deployments) } it { is_expected.to have_many(:deployments) }
it { is_expected.to have_many(:metrics_dashboard_annotations) } it { is_expected.to have_many(:metrics_dashboard_annotations) }
it { is_expected.to have_many(:alert_management_alerts) }
it { is_expected.to delegate_method(:stop_action).to(:last_deployment) } it { is_expected.to delegate_method(:stop_action).to(:last_deployment) }
it { is_expected.to delegate_method(:manual_actions).to(:last_deployment) } it { is_expected.to delegate_method(:manual_actions).to(:last_deployment) }
......
...@@ -33,6 +33,10 @@ RSpec.describe PrometheusAlert do ...@@ -33,6 +33,10 @@ RSpec.describe PrometheusAlert do
describe 'associations' do describe 'associations' do
it { is_expected.to belong_to(:project) } it { is_expected.to belong_to(:project) }
it { is_expected.to belong_to(:environment) } it { is_expected.to belong_to(:environment) }
it { is_expected.to belong_to(:prometheus_metric) }
it { is_expected.to have_many(:prometheus_alert_events) }
it { is_expected.to have_many(:related_issues) }
it { is_expected.to have_many(:alert_management_alerts) }
end end
describe 'project validations' do describe 'project validations' do
......
...@@ -134,7 +134,9 @@ RSpec.describe Projects::Alerting::NotifyService do ...@@ -134,7 +134,9 @@ RSpec.describe Projects::Alerting::NotifyService do
monitoring_tool: payload_raw.fetch(:monitoring_tool), monitoring_tool: payload_raw.fetch(:monitoring_tool),
service: payload_raw.fetch(:service), service: payload_raw.fetch(:service),
fingerprint: Digest::SHA1.hexdigest(fingerprint), fingerprint: Digest::SHA1.hexdigest(fingerprint),
ended_at: nil ended_at: nil,
prometheus_alert_id: nil,
environment_id: nil
) )
end end
...@@ -193,7 +195,9 @@ RSpec.describe Projects::Alerting::NotifyService do ...@@ -193,7 +195,9 @@ RSpec.describe Projects::Alerting::NotifyService do
monitoring_tool: nil, monitoring_tool: nil,
service: nil, service: nil,
fingerprint: nil, fingerprint: nil,
ended_at: nil ended_at: nil,
prometheus_alert_id: nil,
environment_id: nil
) )
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