2020-03-13 15:44:24 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Projects
|
|
|
|
module Alerting
|
2021-02-22 17:27:13 +05:30
|
|
|
class NotifyService
|
|
|
|
include BaseServiceUtility
|
2020-03-13 15:44:24 +05:30
|
|
|
include Gitlab::Utils::StrongMemoize
|
2020-07-28 23:09:34 +05:30
|
|
|
include ::IncidentManagement::Settings
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
def initialize(project, payload)
|
|
|
|
@project = project
|
|
|
|
@payload = payload
|
|
|
|
end
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
def execute(token, integration = nil)
|
|
|
|
@integration = integration
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
return bad_request unless valid_payload_size?
|
2021-01-29 00:20:46 +05:30
|
|
|
return forbidden unless active_integration?
|
2020-03-13 15:44:24 +05:30
|
|
|
return unauthorized unless valid_token?(token)
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
process_alert
|
2020-05-24 23:13:21 +05:30
|
|
|
return bad_request unless alert.persisted?
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
process_incident_issues if process_issues?
|
2020-04-08 14:13:33 +05:30
|
|
|
send_alert_email if send_email?
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
ServiceResponse.success
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
attr_reader :project, :payload, :integration
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
def process_alert
|
2021-01-03 14:25:43 +05:30
|
|
|
if alert.persisted?
|
|
|
|
process_existing_alert
|
2020-06-23 00:09:42 +05:30
|
|
|
else
|
|
|
|
create_alert
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def process_existing_alert
|
|
|
|
if incoming_payload.ends_at.present?
|
|
|
|
process_resolved_alert
|
2020-11-24 15:15:51 +05:30
|
|
|
else
|
|
|
|
alert.register_new_event!
|
|
|
|
end
|
|
|
|
|
|
|
|
alert
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def process_resolved_alert
|
2020-11-24 15:15:51 +05:30
|
|
|
return unless auto_close_incident?
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
if alert.resolve(incoming_payload.ends_at)
|
2020-11-24 15:15:51 +05:30
|
|
|
close_issue(alert.issue)
|
|
|
|
end
|
|
|
|
|
|
|
|
alert
|
|
|
|
end
|
|
|
|
|
|
|
|
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?
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def create_alert
|
2021-01-03 14:25:43 +05:30
|
|
|
return unless alert.save
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
alert.execute_services
|
2021-01-29 00:20:46 +05:30
|
|
|
SystemNoteService.create_new_alert(alert, notification_source)
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def process_incident_issues
|
2021-01-29 00:20:46 +05:30
|
|
|
return if alert.issue || alert.resolved?
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
::IncidentManagement::ProcessAlertWorker.perform_async(nil, nil, alert.id)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
def send_alert_email
|
|
|
|
notification_service
|
|
|
|
.async
|
2021-01-29 00:20:46 +05:30
|
|
|
.prometheus_alerts_fired(project, [alert])
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def alert
|
|
|
|
strong_memoize(:alert) do
|
|
|
|
existing_alert || new_alert
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def existing_alert
|
|
|
|
return unless incoming_payload.gitlab_fingerprint
|
|
|
|
|
|
|
|
AlertManagement::Alert.not_resolved.for_fingerprint(project, incoming_payload.gitlab_fingerprint).first
|
|
|
|
end
|
|
|
|
|
|
|
|
def new_alert
|
|
|
|
AlertManagement::Alert.new(**incoming_payload.alert_params, ended_at: nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
def incoming_payload
|
|
|
|
strong_memoize(:incoming_payload) do
|
2021-02-22 17:27:13 +05:30
|
|
|
Gitlab::AlertManagement::Payload.parse(project, payload.to_h)
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
def notification_source
|
|
|
|
alert.monitoring_tool || integration&.name || 'Generic Alert Endpoint'
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def valid_payload_size?
|
2021-02-22 17:27:13 +05:30
|
|
|
Gitlab::Utils::DeepSize.new(payload).valid?
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
def active_integration?
|
|
|
|
integration&.active?
|
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def valid_token?(token)
|
2021-01-29 00:20:46 +05:30
|
|
|
token == integration.token
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def bad_request
|
2020-04-22 19:07:51 +05:30
|
|
|
ServiceResponse.error(message: 'Bad Request', http_status: :bad_request)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def unauthorized
|
2020-04-22 19:07:51 +05:30
|
|
|
ServiceResponse.error(message: 'Unauthorized', http_status: :unauthorized)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def forbidden
|
2020-04-22 19:07:51 +05:30
|
|
|
ServiceResponse.error(message: 'Forbidden', http_status: :forbidden)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|