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

134 lines
3.2 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
2020-11-24 15:15:51 +05:30
include ::IncidentManagement::Settings
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
ServiceResponse.success
end
private
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
2020-08-18 19:51:02 +05:30
2020-11-24 15:15:51 +05:30
process_incident_issues if process_issues?
2020-05-24 23:13:21 +05:30
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
SystemNoteService.create_new_alert(alert, Gitlab::AlertManagement::AlertParams::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
return unless alert.persisted?
return if alert.issue
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
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,
params,
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