diff options
Diffstat (limited to 'lib/gitlab/alert_management/payload')
-rw-r--r-- | lib/gitlab/alert_management/payload/base.rb | 22 | ||||
-rw-r--r-- | lib/gitlab/alert_management/payload/generic.rb | 4 |
2 files changed, 21 insertions, 5 deletions
diff --git a/lib/gitlab/alert_management/payload/base.rb b/lib/gitlab/alert_management/payload/base.rb index 74e47e5226e..0fd593a3780 100644 --- a/lib/gitlab/alert_management/payload/base.rb +++ b/lib/gitlab/alert_management/payload/base.rb @@ -88,19 +88,19 @@ module Gitlab # AlertManagement::Alert directly for read operations. def alert_params { - description: description, + description: description&.truncate(::AlertManagement::Alert::DESCRIPTION_MAX_LENGTH), ended_at: ends_at, environment: environment, fingerprint: gitlab_fingerprint, - hosts: Array(hosts), - monitoring_tool: monitoring_tool, + 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, + service: service&.truncate(::AlertManagement::Alert::SERVICE_MAX_LENGTH), severity: severity, started_at: starts_at, - title: title + title: title&.truncate(::AlertManagement::Alert::TITLE_MAX_LENGTH) }.transform_values(&:presence).compact end @@ -135,6 +135,18 @@ module Gitlab def plain_gitlab_fingerprint; end + def truncate_hosts(hosts) + return hosts if hosts.join.length <= ::AlertManagement::Alert::HOSTS_MAX_LENGTH + + hosts.inject([]) do |new_hosts, host| + remaining_length = ::AlertManagement::Alert::HOSTS_MAX_LENGTH - new_hosts.join.length + + break new_hosts unless remaining_length > 0 + + new_hosts << host.to_s.truncate(remaining_length, omission: '') + end + end + def value_for_paths(paths) target_path = paths.find { |path| payload&.dig(*path) } diff --git a/lib/gitlab/alert_management/payload/generic.rb b/lib/gitlab/alert_management/payload/generic.rb index 7efdfac75dc..e8e85155bef 100644 --- a/lib/gitlab/alert_management/payload/generic.rb +++ b/lib/gitlab/alert_management/payload/generic.rb @@ -8,6 +8,8 @@ module Gitlab DEFAULT_TITLE = 'New: Incident' DEFAULT_SEVERITY = 'critical' + attribute :description, paths: 'description' + attribute :ends_at, paths: 'end_time', type: :time attribute :environment_name, paths: 'gitlab_environment_name' attribute :hosts, paths: 'hosts' attribute :monitoring_tool, paths: 'monitoring_tool' @@ -23,3 +25,5 @@ module Gitlab end end end + +Gitlab::AlertManagement::Payload::Generic.prepend_if_ee('EE::Gitlab::AlertManagement::Payload::Generic') |