Class: Gitlab::AlertManagement::Payload::Base
- Inherits:
-
Object
- Object
- Gitlab::AlertManagement::Payload::Base
- Includes:
- ActiveModel::Model, Routing, Utils::StrongMemoize
- Defined in:
- lib/gitlab/alert_management/payload/base.rb
Direct Known Subclasses
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
-
#integration ⇒ Object
Returns the value of attribute integration.
-
#payload ⇒ Object
Returns the value of attribute payload.
-
#project ⇒ Object
Returns the value of attribute project.
Class Method Summary collapse
-
.attribute(key, paths:, type: nil, fallback: -> { nil }) ⇒ Object
Defines a method which allows access to a given value within an alert payload.
Instance Method Summary collapse
-
#alert_params ⇒ Object
Attributes of an AlertManagement::Alert as read directly from a payload.
- #environment ⇒ Object
- #gitlab_fingerprint ⇒ Object
- #has_required_attributes? ⇒ Boolean
- #resolved? ⇒ Boolean
- #severity ⇒ Object
- #source ⇒ Object
Methods included from Routing
includes_helpers, redirect_legacy_paths, url_helpers
Instance Attribute Details
#integration ⇒ Object
Returns the value of attribute integration.
15 16 17 |
# File 'lib/gitlab/alert_management/payload/base.rb', line 15 def integration @integration end |
#payload ⇒ Object
Returns the value of attribute payload.
15 16 17 |
# File 'lib/gitlab/alert_management/payload/base.rb', line 15 def payload @payload end |
#project ⇒ Object
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’‘
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_params ⇒ Object
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 |
#environment ⇒ Object
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_fingerprint ⇒ Object
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
141 142 143 |
# File 'lib/gitlab/alert_management/payload/base.rb', line 141 def has_required_attributes? true end |
#resolved? ⇒ Boolean
137 138 139 |
# File 'lib/gitlab/alert_management/payload/base.rb', line 137 def resolved? status == 'resolved' end |
#severity ⇒ Object
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 |
#source ⇒ Object
149 150 151 |
# File 'lib/gitlab/alert_management/payload/base.rb', line 149 def source monitoring_tool || integration&.name end |