debian-mirror-gitlab/app/services/alert_management/process_prometheus_alert_service.rb

117 lines
2.9 KiB
Ruby
Raw Normal View History

2020-05-24 23:13:21 +05:30
# frozen_string_literal: true
module AlertManagement
class ProcessPrometheusAlertService < BaseService
include Gitlab::Utils::StrongMemoize
def execute
return bad_request unless parsed_alert.valid?
process_alert_management_alert
ServiceResponse.success
end
private
delegate :firing?, :resolved?, :gitlab_fingerprint, :ends_at, to: :parsed_alert
def parsed_alert
strong_memoize(:parsed_alert) do
Gitlab::Alerting::Alert.new(project: project, payload: params)
end
end
def process_alert_management_alert
process_firing_alert_management_alert if firing?
process_resolved_alert_management_alert if resolved?
end
def process_firing_alert_management_alert
if am_alert.present?
2020-06-23 00:09:42 +05:30
am_alert.register_new_event!
2020-05-24 23:13:21 +05:30
reset_alert_management_alert_status
else
create_alert_management_alert
end
2020-08-18 19:51:02 +05:30
process_incident_alert
2020-05-24 23:13:21 +05:30
end
def reset_alert_management_alert_status
return if am_alert.trigger
logger.warn(
message: 'Unable to update AlertManagement::Alert status to triggered',
project_id: project.id,
alert_id: am_alert.id
)
end
def create_alert_management_alert
2020-08-18 19:51:02 +05:30
new_alert = AlertManagement::Alert.new(am_alert_params.merge(ended_at: nil))
if new_alert.save
new_alert.execute_services
@am_alert = new_alert
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-08-18 19:51:02 +05:30
alert_errors: new_alert.errors.messages
2020-05-24 23:13:21 +05:30
)
end
def am_alert_params
Gitlab::AlertManagement::AlertParams.from_prometheus_alert(project: project, parsed_alert: parsed_alert)
end
def process_resolved_alert_management_alert
return if am_alert.blank?
2020-07-28 23:09:34 +05:30
if am_alert.resolve(ends_at)
close_issue(am_alert.issue)
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,
alert_id: am_alert.id
)
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-08-18 19:51:02 +05:30
def process_incident_alert
return unless am_alert
return if am_alert.issue
IncidentManagement::ProcessAlertWorker.perform_async(nil, nil, am_alert.id)
end
2020-05-24 23:13:21 +05:30
def logger
@logger ||= Gitlab::AppLogger
end
def am_alert
2020-08-18 19:51:02 +05:30
strong_memoize(:am_alert) do
AlertManagement::Alert.not_resolved.for_fingerprint(project, gitlab_fingerprint).first
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