Commit ca33cd60 authored by Sean Arnold's avatar Sean Arnold Committed by Imre Farkas

Allow generic alerts to set gitlab environment

- Set via gitlab_environment_name
parent 6abf565a
---
title: Add ability to associate Environment with Alert with gitlab_environment_name payload key
merge_request: 39785
author:
type: added
...@@ -48,6 +48,7 @@ You can customize the payload by sending the following parameters. All fields ot ...@@ -48,6 +48,7 @@ You can customize the payload by sending the following parameters. All fields ot
| `hosts` | String or Array | One or more hosts, as to where this incident occurred. | | `hosts` | String or Array | One or more hosts, as to where this incident occurred. |
| `severity` | String | The severity of the alert. Must be one of `critical`, `high`, `medium`, `low`, `info`, `unknown`. Default is `critical`. | | `severity` | String | The severity of the alert. Must be one of `critical`, `high`, `medium`, `low`, `info`, `unknown`. Default is `critical`. |
| `fingerprint` | String or Array | The unique identifier of the alert. This can be used to group occurrences of the same alert. | | `fingerprint` | String or Array | The unique identifier of the alert. This can be used to group occurrences of the same alert. |
| `gitlab_environment_name` | String | The name of the associated GitLab [environment](../../../ci/environments/index.md). This can be used to associate your alert to your environment. |
You can also add custom fields to the alert's payload. The values of extra parameters You can also add custom fields to the alert's payload. The values of extra parameters
are not limited to primitive types, such as strings or numbers, but can be a nested are not limited to primitive types, such as strings or numbers, but can be a nested
......
...@@ -21,7 +21,8 @@ module Gitlab ...@@ -21,7 +21,8 @@ module Gitlab
payload: payload, payload: payload,
started_at: parsed_payload['startsAt'], started_at: parsed_payload['startsAt'],
severity: annotations[:severity], severity: annotations[:severity],
fingerprint: annotations[:fingerprint] fingerprint: annotations[:fingerprint],
environment: annotations[:environment]
} }
end end
......
...@@ -55,7 +55,8 @@ module Gitlab ...@@ -55,7 +55,8 @@ module Gitlab
'service' => payload[:service], 'service' => payload[:service],
'hosts' => hosts.presence, 'hosts' => hosts.presence,
'severity' => severity, 'severity' => severity,
'fingerprint' => fingerprint 'fingerprint' => fingerprint,
'environment' => environment
} }
end end
...@@ -73,6 +74,16 @@ module Gitlab ...@@ -73,6 +74,16 @@ module Gitlab
current_time current_time
end end
def environment
environment_name = payload[:gitlab_environment_name]
return unless environment_name
EnvironmentsFinder.new(project, nil, { name: environment_name })
.find
&.first
end
def secondary_params def secondary_params
payload.except(:start_time) payload.except(:start_time)
end end
......
...@@ -34,7 +34,8 @@ RSpec.describe Gitlab::AlertManagement::AlertParams do ...@@ -34,7 +34,8 @@ RSpec.describe Gitlab::AlertManagement::AlertParams do
hosts: ['gitlab.com'], hosts: ['gitlab.com'],
payload: payload, payload: payload,
started_at: started_at, started_at: started_at,
fingerprint: nil fingerprint: nil,
environment: nil
) )
end end
......
...@@ -124,6 +124,18 @@ RSpec.describe Gitlab::Alerting::NotificationPayloadParser do ...@@ -124,6 +124,18 @@ RSpec.describe Gitlab::Alerting::NotificationPayloadParser do
end end
end end
context 'with environment' do
let(:environment) { create(:environment, project: project) }
before do
payload[:gitlab_environment_name] = environment.name
end
it 'sets the environment ' do
expect(subject.dig('annotations', 'environment')).to eq(environment)
end
end
context 'when payload attributes have blank lines' do context 'when payload attributes have blank lines' do
let(:payload) do let(:payload) do
{ {
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Projects::Alerting::NotifyService do RSpec.describe Projects::Alerting::NotifyService do
let_it_be(:project, reload: true) { create(:project) } let_it_be(:project, reload: true) { create(:project, :repository) }
before do before do
# We use `let_it_be(:project)` so we make sure to clear caches # We use `let_it_be(:project)` so we make sure to clear caches
...@@ -54,6 +54,7 @@ RSpec.describe Projects::Alerting::NotifyService do ...@@ -54,6 +54,7 @@ RSpec.describe Projects::Alerting::NotifyService do
let(:starts_at) { Time.current.change(usec: 0) } let(:starts_at) { Time.current.change(usec: 0) }
let(:fingerprint) { 'testing' } let(:fingerprint) { 'testing' }
let(:service) { described_class.new(project, nil, payload) } let(:service) { described_class.new(project, nil, payload) }
let(:environment) { create(:environment, project: project) }
let(:payload_raw) do let(:payload_raw) do
{ {
title: 'alert title', title: 'alert title',
...@@ -63,7 +64,8 @@ RSpec.describe Projects::Alerting::NotifyService do ...@@ -63,7 +64,8 @@ RSpec.describe Projects::Alerting::NotifyService do
service: 'GitLab Test Suite', service: 'GitLab Test Suite',
description: 'Very detailed description', description: 'Very detailed description',
hosts: ['1.1.1.1', '2.2.2.2'], hosts: ['1.1.1.1', '2.2.2.2'],
fingerprint: fingerprint fingerprint: fingerprint,
gitlab_environment_name: environment.name
}.with_indifferent_access }.with_indifferent_access
end end
...@@ -105,9 +107,9 @@ RSpec.describe Projects::Alerting::NotifyService do ...@@ -105,9 +107,9 @@ RSpec.describe Projects::Alerting::NotifyService do
monitoring_tool: payload_raw.fetch(:monitoring_tool), monitoring_tool: payload_raw.fetch(:monitoring_tool),
service: payload_raw.fetch(:service), service: payload_raw.fetch(:service),
fingerprint: Digest::SHA1.hexdigest(fingerprint), fingerprint: Digest::SHA1.hexdigest(fingerprint),
environment_id: environment.id,
ended_at: nil, ended_at: nil,
prometheus_alert_id: nil, prometheus_alert_id: nil
environment_id: nil
) )
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment