diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-02-18 13:34:06 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-02-18 13:34:06 +0300 |
commit | 859a6fb938bb9ee2a317c46dfa4fcc1af49608f0 (patch) | |
tree | d7f2700abe6b4ffcb2dcfc80631b2d87d0609239 /lib/gitlab/alert_management | |
parent | 446d496a6d000c73a304be52587cd9bbc7493136 (diff) |
Add latest changes from gitlab-org/gitlab@13-9-stable-eev13.9.0-rc42
Diffstat (limited to 'lib/gitlab/alert_management')
-rw-r--r-- | lib/gitlab/alert_management/payload.rb | 7 | ||||
-rw-r--r-- | lib/gitlab/alert_management/payload/base.rb | 31 | ||||
-rw-r--r-- | lib/gitlab/alert_management/payload/generic.rb | 5 | ||||
-rw-r--r-- | lib/gitlab/alert_management/payload/prometheus.rb | 38 |
4 files changed, 73 insertions, 8 deletions
diff --git a/lib/gitlab/alert_management/payload.rb b/lib/gitlab/alert_management/payload.rb index ce09ffd87ee..a1063001330 100644 --- a/lib/gitlab/alert_management/payload.rb +++ b/lib/gitlab/alert_management/payload.rb @@ -17,13 +17,14 @@ module Gitlab # @param project [Project] # @param payload [Hash] # @param monitoring_tool [String] - def parse(project, payload, monitoring_tool: nil) + # @param integration [AlertManagement::HttpIntegration] + def parse(project, payload, monitoring_tool: nil, integration: nil) payload_class = payload_class_for( monitoring_tool: monitoring_tool || payload&.dig('monitoring_tool'), payload: payload ) - payload_class.new(project: project, payload: payload) + payload_class.new(project: project, payload: payload, integration: integration) end private @@ -47,3 +48,5 @@ module Gitlab end end end + +Gitlab::AlertManagement::Payload.prepend_if_ee('EE::Gitlab::AlertManagement::Payload') diff --git a/lib/gitlab/alert_management/payload/base.rb b/lib/gitlab/alert_management/payload/base.rb index 0fd593a3780..c8b8d6c259d 100644 --- a/lib/gitlab/alert_management/payload/base.rb +++ b/lib/gitlab/alert_management/payload/base.rb @@ -12,7 +12,7 @@ module Gitlab include Gitlab::Utils::StrongMemoize include Gitlab::Routing - attr_accessor :project, :payload + attr_accessor :project, :payload, :integration # Any attribute expected to be specifically read from # or derived from an alert payload should be defined. @@ -44,11 +44,25 @@ module Gitlab :title ].freeze + private_constant :EXPECTED_PAYLOAD_ATTRIBUTES + # Define expected API for a payload EXPECTED_PAYLOAD_ATTRIBUTES.each do |key| define_method(key) {} end + SEVERITY_MAPPING = { + 'critical' => :critical, + 'high' => :high, + 'medium' => :medium, + 'low' => :low, + 'info' => :info + }.freeze + + # Handle an unmapped severity value the same way we treat missing values + # so we can fallback to alert's default severity `critical`. + UNMAPPED_SEVERITY = nil + # Defines a method which allows access to a given # value within an alert payload # @@ -131,9 +145,21 @@ module Gitlab true end + def severity + severity_mapping.fetch(severity_raw.to_s.downcase, UNMAPPED_SEVERITY) + end + private - def plain_gitlab_fingerprint; end + def plain_gitlab_fingerprint + end + + def severity_raw + end + + def severity_mapping + SEVERITY_MAPPING + end def truncate_hosts(hosts) return hosts if hosts.join.length <= ::AlertManagement::Alert::HOSTS_MAX_LENGTH @@ -147,6 +173,7 @@ module Gitlab end end + # Overriden in EE::Gitlab::AlertManagement::Payload::Generic def value_for_paths(paths) target_path = paths.find { |path| payload&.dig(*path) } diff --git a/lib/gitlab/alert_management/payload/generic.rb b/lib/gitlab/alert_management/payload/generic.rb index e8e85155bef..0eb1bee8181 100644 --- a/lib/gitlab/alert_management/payload/generic.rb +++ b/lib/gitlab/alert_management/payload/generic.rb @@ -6,7 +6,6 @@ module Gitlab module Payload class Generic < Base DEFAULT_TITLE = 'New: Incident' - DEFAULT_SEVERITY = 'critical' attribute :description, paths: 'description' attribute :ends_at, paths: 'end_time', type: :time @@ -15,10 +14,12 @@ module Gitlab attribute :monitoring_tool, paths: 'monitoring_tool' attribute :runbook, paths: 'runbook' attribute :service, paths: 'service' - attribute :severity, paths: 'severity', fallback: -> { DEFAULT_SEVERITY } attribute :starts_at, paths: 'start_time', type: :time, fallback: -> { Time.current.utc } attribute :title, paths: 'title', fallback: -> { DEFAULT_TITLE } + attribute :severity_raw, paths: 'severity' + private :severity_raw + attribute :plain_gitlab_fingerprint, paths: 'fingerprint' private :plain_gitlab_fingerprint end diff --git a/lib/gitlab/alert_management/payload/prometheus.rb b/lib/gitlab/alert_management/payload/prometheus.rb index 336e9b319e8..4c36ebbf3aa 100644 --- a/lib/gitlab/alert_management/payload/prometheus.rb +++ b/lib/gitlab/alert_management/payload/prometheus.rb @@ -1,10 +1,12 @@ # frozen_string_literal: true -# Attribute mapping for alerts via prometheus alerting integration. module Gitlab module AlertManagement module Payload + # Attribute mapping for alerts via prometheus alerting integration. class Prometheus < Base + extend Gitlab::Utils::Override + attribute :alert_markdown, paths: %w(annotations gitlab_incident_markdown) attribute :annotations, paths: 'annotations' attribute :description, paths: %w(annotations description) @@ -26,13 +28,40 @@ module Gitlab paths: [%w(annotations title), %w(annotations summary), %w(labels alertname)] - attribute :starts_at_raw, paths: [%w(startsAt)] private :starts_at_raw + attribute :severity_raw, paths: %w(labels severity) + private :severity_raw + METRIC_TIME_WINDOW = 30.minutes + ADDITIONAL_SEVERITY_MAPPING = { + 's1' => :critical, + 's2' => :high, + 's3' => :medium, + 's4' => :low, + 's5' => :info, + 'p1' => :critical, + 'p2' => :high, + 'p3' => :medium, + 'p4' => :low, + 'p5' => :info, + 'debug' => :info, + 'information' => :info, + 'notice' => :info, + 'warn' => :low, + 'warning' => :low, + 'minor' => :low, + 'error' => :medium, + 'major' => :high, + 'emergency' => :critical, + 'fatal' => :critical, + 'alert' => :medium, + 'page' => :high + }.freeze + def monitoring_tool Gitlab::AlertManagement::Payload::MONITORING_TOOLS[:prometheus] end @@ -67,6 +96,11 @@ module Gitlab private + override :severity_mapping + def severity_mapping + super.merge(ADDITIONAL_SEVERITY_MAPPING) + end + def plain_gitlab_fingerprint [starts_at_raw, title, full_query].join('/') end |