Welcome to mirror list, hosted at ThFree Co, Russian Federation.

managed_prometheus.rb « payload « alert_management « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2236e60a0c62fd6381cd18bf0854e01913b4c7fd (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
50
51
52
53
54
55
56
57
58
# frozen_string_literal: true

# Attribute mapping for alerts via prometheus alerting integration,
# and for which payload includes gitlab-controlled attributes.
module Gitlab
  module AlertManagement
    module Payload
      class ManagedPrometheus < ::Gitlab::AlertManagement::Payload::Prometheus
        attribute :gitlab_prometheus_alert_id,
                  paths: %w(labels gitlab_prometheus_alert_id),
                  type: :integer
        attribute :metric_id,
                  paths: %w(labels gitlab_alert_id),
                  type: :integer

        def gitlab_alert
          strong_memoize(:gitlab_alert) do
            next unless metric_id || gitlab_prometheus_alert_id

            alerts = Projects::Prometheus::AlertsFinder
              .new(project: project, metric: metric_id, id: gitlab_prometheus_alert_id)
              .execute

            next if alerts.blank? || alerts.size > 1

            alerts.first
          end
        end

        def full_query
          gitlab_alert&.full_query || super
        end

        def environment
          gitlab_alert&.environment || super
        end

        def metrics_dashboard_url
          return unless gitlab_alert

          metrics_dashboard_project_prometheus_alert_url(
            project,
            gitlab_alert.prometheus_metric_id,
            environment_id: environment.id,
            embedded: true,
            **alert_embed_window_params
          )
        end

        private

        def plain_gitlab_fingerprint
          [metric_id, starts_at_raw].join('/')
        end
      end
    end
  end
end