summaryrefslogtreecommitdiff
path: root/lib/gitlab/alert_management/payload.rb
blob: ce09ffd87ee4e649822bde6d498ffc77494f15fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# frozen_string_literal: true

module Gitlab
  module AlertManagement
    module Payload
      MONITORING_TOOLS = {
        prometheus: 'Prometheus',
        cilium: 'Cilium'
      }.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]
        def parse(project, payload, monitoring_tool: nil)
          payload_class = payload_class_for(
            monitoring_tool: monitoring_tool || payload&.dig('monitoring_tool'),
            payload: payload
          )

          payload_class.new(project: project, payload: payload)
        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