debian-mirror-gitlab/app/services/concerns/alert_management/alert_processing.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

134 lines
3.5 KiB
Ruby
Raw Normal View History

2021-03-11 19:13:27 +05:30
# frozen_string_literal: true
module AlertManagement
# Module to support the processing of new alert payloads
# from various sources. Payloads may be for new alerts,
# existing alerts, or acting as a resolving alert.
#
# Performs processing-related tasks, such as creating system
# notes, creating or resolving related issues, and notifying
# stakeholders of the alert.
#
# Requires #project [Project] and #payload [Hash] methods
# to be defined.
module AlertProcessing
include BaseServiceUtility
include Gitlab::Utils::StrongMemoize
include ::IncidentManagement::Settings
# Updates or creates alert from payload for project
# including system notes
def process_alert
2021-06-08 01:23:25 +05:30
alert.persisted? ? process_existing_alert : process_new_alert
2021-03-11 19:13:27 +05:30
end
# Creates or closes issue for alert and notifies stakeholders
def complete_post_processing_tasks
process_incident_issues if process_issues?
send_alert_email if send_email? && notifying_alert?
end
def process_existing_alert
2021-06-08 01:23:25 +05:30
resolving_alert? ? process_resolved_alert : process_firing_alert
2021-03-11 19:13:27 +05:30
end
def process_resolved_alert
2021-04-17 20:07:23 +05:30
SystemNoteService.log_resolving_alert(alert, alert_source)
if alert.resolve(incoming_payload.ends_at)
SystemNoteService.change_alert_status(alert, User.alert_bot)
2022-08-13 15:12:31 +05:30
close_issue(alert.issue_id) if auto_close_incident?
2021-04-17 20:07:23 +05:30
end
2021-03-11 19:13:27 +05:30
end
def process_firing_alert
alert.register_new_event!
end
2022-08-13 15:12:31 +05:30
def close_issue(issue_id)
return unless issue_id
2021-03-11 19:13:27 +05:30
2022-08-13 15:12:31 +05:30
::IncidentManagement::CloseIncidentWorker.perform_async(issue_id)
2021-03-11 19:13:27 +05:30
end
def process_new_alert
2022-08-13 15:12:31 +05:30
return if resolving_alert?
2021-03-11 19:13:27 +05:30
if alert.save
2021-09-30 23:02:18 +05:30
alert.execute_integrations
2021-03-11 19:13:27 +05:30
SystemNoteService.create_new_alert(alert, alert_source)
2022-08-27 11:52:29 +05:30
elsif alert.errors[:fingerprint].any?
refind_and_increment_alert
2021-03-11 19:13:27 +05:30
else
logger.warn(
2022-08-27 11:52:29 +05:30
message: "Unable to create AlertManagement::Alert",
2021-03-11 19:13:27 +05:30
project_id: project.id,
2022-08-27 11:52:29 +05:30
alert_errors: alert.errors.messages,
alert_source: alert_source
2021-03-11 19:13:27 +05:30
)
end
2022-08-27 11:52:29 +05:30
rescue ActiveRecord::RecordNotUnique
refind_and_increment_alert
2021-03-11 19:13:27 +05:30
end
def process_incident_issues
2022-08-13 15:12:31 +05:30
return if alert.issue_id || alert.resolved?
2021-03-11 19:13:27 +05:30
2021-06-08 01:23:25 +05:30
::IncidentManagement::ProcessAlertWorkerV2.perform_async(alert.id)
2021-03-11 19:13:27 +05:30
end
def send_alert_email
notification_service
.async
.prometheus_alerts_fired(project, [alert])
end
def incoming_payload
strong_memoize(:incoming_payload) do
Gitlab::AlertManagement::Payload.parse(project, payload.to_h, integration: integration)
end
end
def alert
strong_memoize(:alert) do
find_existing_alert || build_new_alert
end
end
def find_existing_alert
return unless incoming_payload.gitlab_fingerprint
2022-07-16 23:28:13 +05:30
AlertManagement::Alert.find_unresolved_alert(project, incoming_payload.gitlab_fingerprint)
2021-03-11 19:13:27 +05:30
end
def build_new_alert
AlertManagement::Alert.new(**incoming_payload.alert_params, ended_at: nil)
end
2022-08-27 11:52:29 +05:30
def refind_and_increment_alert
clear_memoization(:alert)
process_firing_alert
end
2021-03-11 19:13:27 +05:30
def resolving_alert?
incoming_payload.ends_at.present?
end
def notifying_alert?
alert.triggered? || alert.resolved?
end
def alert_source
2021-06-08 01:23:25 +05:30
incoming_payload.monitoring_tool
2021-03-11 19:13:27 +05:30
end
def logger
@logger ||= Gitlab::AppLogger
end
end
end
2021-06-08 01:23:25 +05:30
AlertManagement::AlertProcessing.prepend_mod