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

EXPECTED_PAYLOAD_ATTRIBUTES =

Any attribute expected to be specifically read from or derived from an alert payload should be defined.

[
  :alert_markdown,
  :alert_title,
  :annotations,
  :description,
  :ends_at,
  :environment,
  :environment_name,
  :full_query,
  :generator_url,
  :gitlab_alert,
  :gitlab_fingerprint,
  :gitlab_prometheus_alert_id,
  :gitlab_y_label,
  :has_required_attributes?,
  :hosts,
  :metric_id,
  :metrics_dashboard_url,
  :monitoring_tool,
  :resolved?,
  :runbook,
  :service,
  :severity,
  :starts_at,
  :status,
  :title
].freeze

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

#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


74
75
76
77
78
79
80
81
82
83
84
# File 'lib/gitlab/alert_management/payload/base.rb', line 74

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.


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/gitlab/alert_management/payload/base.rb', line 89

def alert_params
  {
    description: description,
    ended_at: ends_at,
    environment: environment,
    fingerprint: gitlab_fingerprint,
    hosts: Array(hosts),
    monitoring_tool: monitoring_tool,
    payload: payload,
    project_id: project.id,
    prometheus_alert: gitlab_alert,
    service: service,
    severity: severity,
    started_at: starts_at,
    title: title
  }.transform_values(&:presence).compact
end

#environmentObject


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

def environment
  strong_memoize(:environment) do
    next unless environment_name

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

#gitlab_fingerprintObject


107
108
109
110
111
112
113
# File 'lib/gitlab/alert_management/payload/base.rb', line 107

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)

130
131
132
# File 'lib/gitlab/alert_management/payload/base.rb', line 130

def has_required_attributes?
  true
end

#resolved?Boolean

Returns:

  • (Boolean)

126
127
128
# File 'lib/gitlab/alert_management/payload/base.rb', line 126

def resolved?
  status == 'resolved'
end