Class: Gitlab::AlertManagement::Payload::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, Routing, Utils::StrongMemoize
Defined in:
lib/gitlab/alert_management/payload/base.rb

Direct Known Subclasses

Generic, Prometheus

Constant Summary collapse

SEVERITY_MAPPING =
{
  'critical' => :critical,
  'high' => :high,
  'medium' => :medium,
  'low' => :low,
  'info' => :info
}.freeze
UNMAPPED_SEVERITY =

Handle an unmapped severity value the same way we treat missing values so we can fallback to alert's default severity `critical`.

nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Routing

add_helpers, includes_helpers, redirect_legacy_paths, url_helpers

Methods included from Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Instance Attribute Details

#integrationObject

Returns the value of attribute integration.


15
16
17
# File 'lib/gitlab/alert_management/payload/base.rb', line 15

def integration
  @integration
end

#payloadObject

Returns the value of attribute payload.


15
16
17
# File 'lib/gitlab/alert_management/payload/base.rb', line 15

def payload
  @payload
end

#projectObject

Returns the value of attribute project.


15
16
17
# File 'lib/gitlab/alert_management/payload/base.rb', line 15

def project
  @project
end

Class Method Details

.attribute(key, paths:, type: nil, fallback: -> { nil }) ⇒ Object

Defines a method which allows access to a given value within an alert payload

Example)

attribute :title
          paths: [['title'],
                 ['details', 'title']]
          fallback: Proc.new { 'New Alert' }

The above sample definition will define a method called #title which will return the value from the payload under the key `title` if available, otherwise looking under `details.title`. If neither returns a value, the return value will be `'New Alert'`

Parameters:

  • key (Symbol)

    Name expected to be used to reference value

  • paths (String, Array<String>, Array<Array<String>>, )

    List of (nested) keys at value can be found, the first to yield a result will be used

  • type (Symbol) (defaults to: nil)

    If value should be converted to another type, that should be specified here

  • fallback (Proc) (defaults to: -> { nil })

    Block to be executed to yield a value if a value cannot be idenitied at any provided paths


88
89
90
91
92
93
94
95
96
97
98
# File 'lib/gitlab/alert_management/payload/base.rb', line 88

def self.attribute(key, paths:, type: nil, fallback: -> { nil })
  define_method(key) do
    strong_memoize(key) do
      paths = Array(paths).first.is_a?(String) ? [Array(paths)] : paths
      value = value_for_paths(paths)
      value = parse_value(value, type) if value

      value.presence || fallback.call
    end
  end
end

Instance Method Details

#alert_paramsObject

Attributes of an AlertManagement::Alert as read directly from a payload. Prefer accessing AlertManagement::Alert directly for read operations.


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/gitlab/alert_management/payload/base.rb', line 103

def alert_params
  {
    description: description&.truncate(::AlertManagement::Alert::DESCRIPTION_MAX_LENGTH),
    ended_at: ends_at,
    environment: environment,
    fingerprint: gitlab_fingerprint,
    hosts: truncate_hosts(Array(hosts).flatten),
    monitoring_tool: monitoring_tool&.truncate(::AlertManagement::Alert::TOOL_MAX_LENGTH),
    payload: payload,
    project_id: project.id,
    prometheus_alert: gitlab_alert,
    service: service&.truncate(::AlertManagement::Alert::SERVICE_MAX_LENGTH),
    severity: severity,
    started_at: starts_at,
    title: title&.truncate(::AlertManagement::Alert::TITLE_MAX_LENGTH)
  }.transform_values(&:presence).compact
end

#environmentObject


129
130
131
132
133
134
135
136
137
138
# File 'lib/gitlab/alert_management/payload/base.rb', line 129

def environment
  strong_memoize(:environment) do
    next unless environment_name

    ::Environments::EnvironmentsFinder
      .new(project, nil, { name: environment_name })
      .execute
      .first
  end
end

#gitlab_fingerprintObject


121
122
123
124
125
126
127
# File 'lib/gitlab/alert_management/payload/base.rb', line 121

def gitlab_fingerprint
  strong_memoize(:gitlab_fingerprint) do
    next unless plain_gitlab_fingerprint

    Gitlab::AlertManagement::Fingerprint.generate(plain_gitlab_fingerprint)
  end
end

#has_required_attributes?Boolean

Returns:

  • (Boolean)

144
145
146
# File 'lib/gitlab/alert_management/payload/base.rb', line 144

def has_required_attributes?
  true
end

#resolved?Boolean

Returns:

  • (Boolean)

140
141
142
# File 'lib/gitlab/alert_management/payload/base.rb', line 140

def resolved?
  status == 'resolved'
end

#severityObject


148
149
150
# File 'lib/gitlab/alert_management/payload/base.rb', line 148

def severity
  severity_mapping.fetch(severity_raw.to_s.downcase, UNMAPPED_SEVERITY)
end