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

EPOCH_MILLISECONDS_DIGIT_COUNT =
(Time.current.to_f * 1000).to_i.to_s.size
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



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

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.



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

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,
    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



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

def environment
  strong_memoize(:environment) do
    next unless environment_name

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

#gitlab_fingerprintObject



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

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)


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

def has_required_attributes?
  true
end

#resolved?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/gitlab/alert_management/payload/base.rb', line 137

def resolved?
  status == 'resolved'
end

#severityObject



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

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

#sourceObject



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

def source
  monitoring_tool || integration&.name
end