Class: Projects::Prometheus::Alerts::NotifyService

Inherits:
BaseProjectService show all
Includes:
AlertManagement::Responses, Gitlab::Utils::StrongMemoize
Defined in:
app/services/projects/prometheus/alerts/notify_service.rb

Constant Summary collapse

REQUIRED_PAYLOAD_KEYS =

This set of keys identifies a payload as a valid Prometheus payload and thus processable by this service. See also prometheus.io/docs/alerting/configuration/#webhook_config

%w[
  version groupKey status receiver groupLabels commonLabels
  commonAnnotations externalURL alerts
].to_set.freeze
SUPPORTED_VERSION =
'4'
PROCESS_MAX_ALERTS =

If feature flag :prometheus_notify_max_alerts is enabled truncate alerts to 100 and process only them. If feature flag is disabled process any amount of alerts.

This is to mitigate incident: gitlab.com/gitlab-com/gl-infra/production/-/issues/6086

100

Instance Attribute Summary

Attributes inherited from BaseProjectService

#project

Attributes inherited from BaseContainerService

#container, #current_user, #group, #params, #project

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AlertManagement::Responses

#bad_request, #created, #forbidden, #success, #unauthorized, #unprocessable_entity

Methods inherited from BaseContainerService

#group_container?, #namespace_container?, #project_container?, #project_group

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?

Constructor Details

#initialize(project, params) ⇒ NotifyService

Returns a new instance of NotifyService.



28
29
30
# File 'app/services/projects/prometheus/alerts/notify_service.rb', line 28

def initialize(project, params)
  super(project: project, params: params.to_h)
end

Class Method Details

.processable?(payload) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
# File 'app/services/projects/prometheus/alerts/notify_service.rb', line 44

def self.processable?(payload)
  # Workaround for https://gitlab.com/gitlab-org/gitlab/-/issues/220496
  return false unless payload

  REQUIRED_PAYLOAD_KEYS.subset?(payload.keys.to_set) &&
    payload['version'] == SUPPORTED_VERSION
end

Instance Method Details

#execute(token, integration = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'app/services/projects/prometheus/alerts/notify_service.rb', line 32

def execute(token, integration = nil)
  return bad_request unless valid_payload_size?
  return unprocessable_entity unless self.class.processable?(params)
  return unauthorized unless valid_alert_manager_token?(token, integration)

  truncate_alerts! if max_alerts_exceeded?

  process_prometheus_alerts(integration)

  created
end