Commit a6b6aa45 authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch '222253-add-prometheus-columns-to-alert-management-alert' into 'master'

Add prometheus_alert_id and environment_id to Alert management alerts

See merge request gitlab-org/gitlab!34995
parents 7744ae48 7cc35451
......@@ -30,6 +30,8 @@ module AlertManagement
belongs_to :project
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 :assignees, through: :alert_assignees
......
......@@ -21,6 +21,7 @@ class Environment < ApplicationRecord
has_many :prometheus_alerts, 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 :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_deployable, through: :last_deployment, source: 'deployable', source_type: 'CommitStatus'
......
......@@ -16,6 +16,7 @@ class PrometheusAlert < ApplicationRecord
has_many :prometheus_alert_events, inverse_of: :prometheus_alert
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_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
......@@ -8736,6 +8736,8 @@ CREATE TABLE public.alert_management_alerts (
monitoring_tool text,
hosts text[] DEFAULT '{}'::text[] 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_5e9e57cadb CHECK ((char_length(description) <= 1000)),
CONSTRAINT check_bac14dddde CHECK ((char_length(service) <= 100)),
......@@ -18337,12 +18339,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 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 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 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_and_note_id ON public.alert_management_alert_user_mentions USING btree (alert_management_alert_id, note_id);
......@@ -20779,6 +20785,9 @@ ALTER TABLE ONLY public.geo_event_log
ALTER TABLE ONLY public.ci_build_trace_sections
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
ADD CONSTRAINT fk_5265c98f24 FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE CASCADE;
......@@ -20986,6 +20995,9 @@ ALTER TABLE ONLY public.merge_requests
ALTER TABLE ONLY public.epics
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
ADD CONSTRAINT fk_aade90f0fc FOREIGN KEY (saml_provider_id) REFERENCES public.saml_providers(id) ON DELETE CASCADE;
......@@ -23400,6 +23412,7 @@ COPY "schema_migrations" (version) FROM STDIN;
20200618134723
20200619154527
20200619154528
20200622040750
20200622070606
20200622070620
20200622095419
......@@ -23411,5 +23424,6 @@ COPY "schema_migrations" (version) FROM STDIN;
20200623170000
20200623185440
20200624075411
20200625045442
\.
......@@ -5,7 +5,9 @@ require 'spec_helper'
RSpec.describe AlertManagement::Alert do
describe 'associations' do
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(:notes) }
it { is_expected.to have_many(:ordered_notes) }
......
......@@ -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 have_many(:deployments) }
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(:manual_actions).to(:last_deployment) }
......
......@@ -33,6 +33,10 @@ RSpec.describe PrometheusAlert do
describe 'associations' do
it { is_expected.to belong_to(:project) }
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
describe 'project validations' do
......
......@@ -134,7 +134,9 @@ RSpec.describe Projects::Alerting::NotifyService do
monitoring_tool: payload_raw.fetch(:monitoring_tool),
service: payload_raw.fetch(:service),
fingerprint: Digest::SHA1.hexdigest(fingerprint),
ended_at: nil
ended_at: nil,
prometheus_alert_id: nil,
environment_id: nil
)
end
......@@ -193,7 +195,9 @@ RSpec.describe Projects::Alerting::NotifyService do
monitoring_tool: nil,
service: nil,
fingerprint: nil,
ended_at: nil
ended_at: nil,
prometheus_alert_id: nil,
environment_id: nil
)
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