2020-05-24 23:13:21 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module AlertManagement
|
2021-02-22 17:27:13 +05:30
|
|
|
class ProcessPrometheusAlertService
|
|
|
|
include BaseServiceUtility
|
2020-05-24 23:13:21 +05:30
|
|
|
include Gitlab::Utils::StrongMemoize
|
2020-11-24 15:15:51 +05:30
|
|
|
include ::IncidentManagement::Settings
|
2020-05-24 23:13:21 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
def initialize(project, payload)
|
|
|
|
@project = project
|
|
|
|
@payload = payload
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
def execute
|
2020-11-24 15:15:51 +05:30
|
|
|
return bad_request unless incoming_payload.has_required_attributes?
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
process_alert_management_alert
|
2021-01-29 00:20:46 +05:30
|
|
|
return bad_request unless alert.persisted?
|
|
|
|
|
|
|
|
process_incident_issues if process_issues?
|
|
|
|
send_alert_email if send_email?
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
ServiceResponse.success
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
attr_reader :project, :payload
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
def process_alert_management_alert
|
2020-11-24 15:15:51 +05:30
|
|
|
if incoming_payload.resolved?
|
|
|
|
process_resolved_alert_management_alert
|
|
|
|
else
|
|
|
|
process_firing_alert_management_alert
|
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def process_firing_alert_management_alert
|
2020-11-24 15:15:51 +05:30
|
|
|
if alert.persisted?
|
|
|
|
alert.register_new_event!
|
2020-05-24 23:13:21 +05:30
|
|
|
reset_alert_management_alert_status
|
|
|
|
else
|
|
|
|
create_alert_management_alert
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_alert_management_alert_status
|
2020-11-24 15:15:51 +05:30
|
|
|
return if alert.trigger
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
logger.warn(
|
|
|
|
message: 'Unable to update AlertManagement::Alert status to triggered',
|
|
|
|
project_id: project.id,
|
2020-11-24 15:15:51 +05:30
|
|
|
alert_id: alert.id
|
2020-05-24 23:13:21 +05:30
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_alert_management_alert
|
2020-11-24 15:15:51 +05:30
|
|
|
if alert.save
|
|
|
|
alert.execute_services
|
2021-01-03 14:25:43 +05:30
|
|
|
SystemNoteService.create_new_alert(alert, Gitlab::AlertManagement::Payload::MONITORING_TOOLS[:prometheus])
|
2020-06-23 00:09:42 +05:30
|
|
|
return
|
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
logger.warn(
|
|
|
|
message: 'Unable to create AlertManagement::Alert',
|
|
|
|
project_id: project.id,
|
2020-11-24 15:15:51 +05:30
|
|
|
alert_errors: alert.errors.messages
|
2020-05-24 23:13:21 +05:30
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_resolved_alert_management_alert
|
2020-11-24 15:15:51 +05:30
|
|
|
return unless alert.persisted?
|
|
|
|
return unless auto_close_incident?
|
2020-07-28 23:09:34 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
if alert.resolve(incoming_payload.ends_at)
|
|
|
|
close_issue(alert.issue)
|
2020-07-28 23:09:34 +05:30
|
|
|
return
|
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
logger.warn(
|
|
|
|
message: 'Unable to update AlertManagement::Alert status to resolved',
|
|
|
|
project_id: project.id,
|
2020-11-24 15:15:51 +05:30
|
|
|
alert_id: alert.id
|
2020-05-24 23:13:21 +05:30
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
def close_issue(issue)
|
|
|
|
return if issue.blank? || issue.closed?
|
|
|
|
|
|
|
|
Issues::CloseService
|
|
|
|
.new(project, User.alert_bot)
|
|
|
|
.execute(issue, system_note: false)
|
|
|
|
|
|
|
|
SystemNoteService.auto_resolve_prometheus_alert(issue, project, User.alert_bot) if issue.reset.closed?
|
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
def process_incident_issues
|
2021-01-29 00:20:46 +05:30
|
|
|
return if alert.issue || alert.resolved?
|
2020-08-18 19:51:02 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
IncidentManagement::ProcessAlertWorker.perform_async(nil, nil, alert.id)
|
2020-08-18 19:51:02 +05:30
|
|
|
end
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
def send_alert_email
|
|
|
|
notification_service
|
|
|
|
.async
|
|
|
|
.prometheus_alerts_fired(project, [alert])
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
def logger
|
|
|
|
@logger ||= Gitlab::AppLogger
|
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
def alert
|
|
|
|
strong_memoize(:alert) do
|
|
|
|
existing_alert || new_alert
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def existing_alert
|
|
|
|
strong_memoize(:existing_alert) do
|
|
|
|
AlertManagement::Alert.not_resolved.for_fingerprint(project, incoming_payload.gitlab_fingerprint).first
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def new_alert
|
|
|
|
strong_memoize(:new_alert) do
|
|
|
|
AlertManagement::Alert.new(
|
|
|
|
**incoming_payload.alert_params,
|
|
|
|
ended_at: nil
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def incoming_payload
|
|
|
|
strong_memoize(:incoming_payload) do
|
|
|
|
Gitlab::AlertManagement::Payload.parse(
|
|
|
|
project,
|
2021-02-22 17:27:13 +05:30
|
|
|
payload,
|
2020-11-24 15:15:51 +05:30
|
|
|
monitoring_tool: Gitlab::AlertManagement::Payload::MONITORING_TOOLS[:prometheus]
|
|
|
|
)
|
2020-08-18 19:51:02 +05:30
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def bad_request
|
|
|
|
ServiceResponse.error(message: 'Bad Request', http_status: :bad_request)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|