Module: AlertManagement::AlertProcessing

Includes:
BaseServiceUtility, Gitlab::Utils::StrongMemoize, IncidentManagement::Settings
Included in:
ProcessPrometheusAlertService, Projects::Alerting::NotifyService
Defined in:
app/services/concerns/alert_management/alert_processing.rb

Overview

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.

Instance Method Summary collapse

Methods included from IncidentManagement::Settings

#auto_close_incident?, #incident_management_setting, #process_issues?

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_error, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?

Instance Method Details

#alertObject



93
94
95
96
97
# File 'app/services/concerns/alert_management/alert_processing.rb', line 93

def alert
  strong_memoize(:alert) do
    find_existing_alert || build_new_alert
  end
end

#alert_sourceObject



123
124
125
# File 'app/services/concerns/alert_management/alert_processing.rb', line 123

def alert_source
  incoming_payload.source
end

#build_new_alertObject



105
106
107
# File 'app/services/concerns/alert_management/alert_processing.rb', line 105

def build_new_alert
  AlertManagement::Alert.new(**incoming_payload.alert_params, ended_at: nil)
end

#close_issue(issue_id) ⇒ Object



49
50
51
52
53
# File 'app/services/concerns/alert_management/alert_processing.rb', line 49

def close_issue(issue_id)
  return unless issue_id

  ::IncidentManagement::CloseIncidentWorker.perform_async(issue_id)
end

#complete_post_processing_tasksObject

Creates or closes issue for alert and notifies stakeholders



26
27
28
29
# File 'app/services/concerns/alert_management/alert_processing.rb', line 26

def complete_post_processing_tasks
  process_incident_issues if process_issues?
  send_alert_email if send_email? && notifying_alert?
end

#find_existing_alertObject



99
100
101
102
103
# File 'app/services/concerns/alert_management/alert_processing.rb', line 99

def find_existing_alert
  return unless incoming_payload.gitlab_fingerprint

  AlertManagement::Alert.find_unresolved_alert(project, incoming_payload.gitlab_fingerprint)
end

#incoming_payloadObject



87
88
89
90
91
# File 'app/services/concerns/alert_management/alert_processing.rb', line 87

def incoming_payload
  strong_memoize(:incoming_payload) do
    Gitlab::AlertManagement::Payload.parse(project, payload.to_h, integration: integration)
  end
end

#loggerObject



127
128
129
# File 'app/services/concerns/alert_management/alert_processing.rb', line 127

def logger
  @logger ||= Gitlab::AppLogger
end

#notifying_alert?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'app/services/concerns/alert_management/alert_processing.rb', line 119

def notifying_alert?
  alert.triggered? || alert.resolved?
end

#process_alertObject

Updates or creates alert from payload for project including system notes



21
22
23
# File 'app/services/concerns/alert_management/alert_processing.rb', line 21

def process_alert
  alert.persisted? ? process_existing_alert : process_new_alert
end

#process_existing_alertObject



31
32
33
# File 'app/services/concerns/alert_management/alert_processing.rb', line 31

def process_existing_alert
  resolving_alert? ? process_resolved_alert : process_firing_alert
end

#process_firing_alertObject



45
46
47
# File 'app/services/concerns/alert_management/alert_processing.rb', line 45

def process_firing_alert
  alert.register_new_event!
end

#process_incident_issuesObject



75
76
77
78
79
# File 'app/services/concerns/alert_management/alert_processing.rb', line 75

def process_incident_issues
  return if alert.issue_id || alert.resolved?

  ::IncidentManagement::ProcessAlertWorkerV2.perform_async(alert.id)
end

#process_new_alertObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/services/concerns/alert_management/alert_processing.rb', line 55

def process_new_alert
  return if resolving_alert?

  if alert.save
    alert.execute_integrations
    SystemNoteService.create_new_alert(alert, alert_source)
  elsif alert.errors[:fingerprint].any?
    refind_and_increment_alert
  else
    logger.warn(
      message: "Unable to create AlertManagement::Alert",
      project_id: project.id,
      alert_errors: alert.errors.messages,
      alert_source: alert_source
    )
  end
rescue ActiveRecord::RecordNotUnique
  refind_and_increment_alert
end

#process_resolved_alertObject



35
36
37
38
39
40
41
42
43
# File 'app/services/concerns/alert_management/alert_processing.rb', line 35

def process_resolved_alert
  SystemNoteService.log_resolving_alert(alert, alert_source)

  if alert.resolve(incoming_payload.ends_at)
    SystemNoteService.change_alert_status(alert, Users::Internal.alert_bot)

    close_issue(alert.issue_id) if auto_close_incident?
  end
end

#refind_and_increment_alertObject



109
110
111
112
113
# File 'app/services/concerns/alert_management/alert_processing.rb', line 109

def refind_and_increment_alert
  clear_memoization(:alert)

  process_firing_alert
end

#resolving_alert?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'app/services/concerns/alert_management/alert_processing.rb', line 115

def resolving_alert?
  incoming_payload.resolved?
end

#send_alert_emailObject



81
82
83
84
85
# File 'app/services/concerns/alert_management/alert_processing.rb', line 81

def send_alert_email
  notification_service
    .async
    .prometheus_alerts_fired(project, [alert])
end