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

includes_helpers, redirect_legacy_paths, url_helpers

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



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

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.



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

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

#environmentObject



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

def environment
  strong_memoize(:environment) do
    next unless environment_name

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

#gitlab_fingerprintObject



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

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)


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

def has_required_attributes?
  true
end

#resolved?Boolean

Returns:

  • (Boolean)


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

def resolved?
  status == 'resolved'
end

#severityObject



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

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

#sourceObject



151
152
153
# File 'lib/gitlab/alert_management/payload/base.rb', line 151

def source
  monitoring_tool || integration&.name
end