diff options
Diffstat (limited to 'app/services/alert_management')
-rw-r--r-- | app/services/alert_management/http_integrations/update_service.rb | 15 | ||||
-rw-r--r-- | app/services/alert_management/process_prometheus_alert_service.rb | 121 |
2 files changed, 27 insertions, 109 deletions
diff --git a/app/services/alert_management/http_integrations/update_service.rb b/app/services/alert_management/http_integrations/update_service.rb index 220c4e759f0..af079f670b8 100644 --- a/app/services/alert_management/http_integrations/update_service.rb +++ b/app/services/alert_management/http_integrations/update_service.rb @@ -9,7 +9,7 @@ module AlertManagement def initialize(integration, current_user, params) @integration = integration @current_user = current_user - @params = params + @params = params.with_indifferent_access end def execute @@ -17,7 +17,7 @@ module AlertManagement params[:token] = nil if params.delete(:regenerate_token) - if integration.update(params) + if integration.update(permitted_params) success else error(integration.errors.full_messages.to_sentence) @@ -32,6 +32,15 @@ module AlertManagement current_user&.can?(:admin_operations, integration) end + def permitted_params + params.slice(*permitted_params_keys) + end + + # overriden in EE + def permitted_params_keys + %i[name active token] + end + def error(message) ServiceResponse.error(message: message) end @@ -46,3 +55,5 @@ module AlertManagement end end end + +::AlertManagement::HttpIntegrations::UpdateService.prepend_if_ee('::EE::AlertManagement::HttpIntegrations::UpdateService') diff --git a/app/services/alert_management/process_prometheus_alert_service.rb b/app/services/alert_management/process_prometheus_alert_service.rb index 753162bfdbf..0591376bcdf 100644 --- a/app/services/alert_management/process_prometheus_alert_service.rb +++ b/app/services/alert_management/process_prometheus_alert_service.rb @@ -2,9 +2,8 @@ module AlertManagement class ProcessPrometheusAlertService - include BaseServiceUtility - include Gitlab::Utils::StrongMemoize - include ::IncidentManagement::Settings + extend ::Gitlab::Utils::Override + include ::AlertManagement::AlertProcessing def initialize(project, payload) @project = project @@ -14,11 +13,10 @@ module AlertManagement def execute return bad_request unless incoming_payload.has_required_attributes? - process_alert_management_alert + process_alert return bad_request unless alert.persisted? - process_incident_issues if process_issues? - send_alert_email if send_email? + complete_post_processing_tasks ServiceResponse.success end @@ -27,110 +25,14 @@ module AlertManagement attr_reader :project, :payload - def process_alert_management_alert - if incoming_payload.resolved? - process_resolved_alert_management_alert - else - process_firing_alert_management_alert - end - end - - def process_firing_alert_management_alert - if alert.persisted? - alert.register_new_event! - reset_alert_management_alert_status - else - create_alert_management_alert - end - end - - def reset_alert_management_alert_status - return if alert.trigger - - logger.warn( - message: 'Unable to update AlertManagement::Alert status to triggered', - project_id: project.id, - alert_id: alert.id - ) - end - - def create_alert_management_alert - if alert.save - alert.execute_services - SystemNoteService.create_new_alert(alert, Gitlab::AlertManagement::Payload::MONITORING_TOOLS[:prometheus]) - return - end - - logger.warn( - message: 'Unable to create AlertManagement::Alert', - project_id: project.id, - alert_errors: alert.errors.messages - ) - end - - def process_resolved_alert_management_alert - return unless alert.persisted? - return unless auto_close_incident? - - if alert.resolve(incoming_payload.ends_at) - close_issue(alert.issue) - return - end - - logger.warn( - message: 'Unable to update AlertManagement::Alert status to resolved', - project_id: project.id, - alert_id: alert.id - ) - end - - def close_issue(issue) - return if issue.blank? || issue.closed? - - Issues::CloseService - .new(project, User.alert_bot) - .execute(issue, system_note: false) + override :process_new_alert + def process_new_alert + return if resolving_alert? - SystemNoteService.auto_resolve_prometheus_alert(issue, project, User.alert_bot) if issue.reset.closed? - end - - def process_incident_issues - return if alert.issue || alert.resolved? - - IncidentManagement::ProcessAlertWorker.perform_async(nil, nil, alert.id) - end - - def send_alert_email - notification_service - .async - .prometheus_alerts_fired(project, [alert]) - end - - def logger - @logger ||= Gitlab::AppLogger - end - - 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 + super end + override :incoming_payload def incoming_payload strong_memoize(:incoming_payload) do Gitlab::AlertManagement::Payload.parse( @@ -141,6 +43,11 @@ module AlertManagement end end + override :resolving_alert? + def resolving_alert? + incoming_payload.resolved? + end + def bad_request ServiceResponse.error(message: 'Bad Request', http_status: :bad_request) end |