2020-08-20 18:10:16 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module AlertManagement
|
|
|
|
module Payload
|
|
|
|
MONITORING_TOOLS = {
|
2020-12-10 00:09:50 +00:00
|
|
|
prometheus: 'Prometheus',
|
|
|
|
cilium: 'Cilium'
|
2020-08-20 18:10:16 +00:00
|
|
|
}.freeze
|
|
|
|
|
|
|
|
class << self
|
|
|
|
# Instantiates an instance of a subclass of
|
|
|
|
# Gitlab::AlertManagement::Payload::Base. This can
|
|
|
|
# be used to create new alerts or read content from
|
|
|
|
# the payload of an existing AlertManagement::Alert
|
|
|
|
#
|
|
|
|
# @param project [Project]
|
|
|
|
# @param payload [Hash]
|
|
|
|
# @param monitoring_tool [String]
|
2021-02-09 12:09:48 +00:00
|
|
|
# @param integration [AlertManagement::HttpIntegration]
|
|
|
|
def parse(project, payload, monitoring_tool: nil, integration: nil)
|
2020-08-20 18:10:16 +00:00
|
|
|
payload_class = payload_class_for(
|
|
|
|
monitoring_tool: monitoring_tool || payload&.dig('monitoring_tool'),
|
|
|
|
payload: payload
|
|
|
|
)
|
|
|
|
|
2021-02-09 12:09:48 +00:00
|
|
|
payload_class.new(project: project, payload: payload, integration: integration)
|
2020-08-20 18:10:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def payload_class_for(monitoring_tool:, payload:)
|
|
|
|
if monitoring_tool == MONITORING_TOOLS[:prometheus]
|
|
|
|
if gitlab_managed_prometheus?(payload)
|
|
|
|
::Gitlab::AlertManagement::Payload::ManagedPrometheus
|
|
|
|
else
|
|
|
|
::Gitlab::AlertManagement::Payload::Prometheus
|
|
|
|
end
|
|
|
|
else
|
|
|
|
::Gitlab::AlertManagement::Payload::Generic
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def gitlab_managed_prometheus?(payload)
|
|
|
|
payload&.dig('labels', 'gitlab_alert_id').present?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-01-26 15:08:58 +00:00
|
|
|
|
2021-05-11 21:10:21 +00:00
|
|
|
Gitlab::AlertManagement::Payload.prepend_mod_with('Gitlab::AlertManagement::Payload')
|