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
| `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`. |
| `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
are not limited to primitive types, such as strings or numbers, but can be a nested
......
......@@ -21,7 +21,8 @@ module Gitlab
payload: payload,
started_at: parsed_payload['startsAt'],
severity: annotations[:severity],
fingerprint: annotations[:fingerprint]
fingerprint: annotations[:fingerprint],
environment: annotations[:environment]
}
end
......
......@@ -55,7 +55,8 @@ module Gitlab
'service' => payload[:service],
'hosts' => hosts.presence,
'severity' => severity,
'fingerprint' => fingerprint
'fingerprint' => fingerprint,
'environment' => environment
}
end
......@@ -73,6 +74,16 @@ module Gitlab
current_time
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
payload.except(:start_time)
end
......
......@@ -34,7 +34,8 @@ RSpec.describe Gitlab::AlertManagement::AlertParams do
hosts: ['gitlab.com'],
payload: payload,
started_at: started_at,
fingerprint: nil
fingerprint: nil,
environment: nil
)
end
......
......@@ -124,6 +124,18 @@ RSpec.describe Gitlab::Alerting::NotificationPayloadParser do
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
let(:payload) do
{
......
......@@ -3,7 +3,7 @@
require 'spec_helper'
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
# We use `let_it_be(:project)` so we make sure to clear caches
......@@ -54,6 +54,7 @@ RSpec.describe Projects::Alerting::NotifyService do
let(:starts_at) { Time.current.change(usec: 0) }
let(:fingerprint) { 'testing' }
let(:service) { described_class.new(project, nil, payload) }
let(:environment) { create(:environment, project: project) }
let(:payload_raw) do
{
title: 'alert title',
......@@ -63,7 +64,8 @@ RSpec.describe Projects::Alerting::NotifyService do
service: 'GitLab Test Suite',
description: 'Very detailed description',
hosts: ['1.1.1.1', '2.2.2.2'],
fingerprint: fingerprint
fingerprint: fingerprint,
gitlab_environment_name: environment.name
}.with_indifferent_access
end
......@@ -105,9 +107,9 @@ RSpec.describe Projects::Alerting::NotifyService do
monitoring_tool: payload_raw.fetch(:monitoring_tool),
service: payload_raw.fetch(:service),
fingerprint: Digest::SHA1.hexdigest(fingerprint),
environment_id: environment.id,
ended_at: nil,
prometheus_alert_id: nil,
environment_id: nil
prometheus_alert_id: nil
)
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