Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
b84b1252
Commit
b84b1252
authored
Nov 10, 2021
by
Sean Arnold
Committed by
Tetiana Chupryna
Nov 10, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Return alert IIDs and title in create response
parent
ddbff944
Changes
14
Hide whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
106 additions
and
40 deletions
+106
-40
app/controllers/projects/alerting/notifications_controller.rb
...controllers/projects/alerting/notifications_controller.rb
+5
-1
app/controllers/projects/prometheus/alerts_controller.rb
app/controllers/projects/prometheus/alerts_controller.rb
+5
-1
app/serializers/alert_management/alert_entity.rb
app/serializers/alert_management/alert_entity.rb
+8
-0
app/serializers/alert_management/alert_serializer.rb
app/serializers/alert_management/alert_serializer.rb
+7
-0
app/services/alert_management/process_prometheus_alert_service.rb
...ices/alert_management/process_prometheus_alert_service.rb
+2
-5
app/services/concerns/alert_management/responses.rb
app/services/concerns/alert_management/responses.rb
+26
-0
app/services/projects/alerting/notify_service.rb
app/services/projects/alerting/notify_service.rb
+2
-13
app/services/projects/prometheus/alerts/notify_service.rb
app/services/projects/prometheus/alerts/notify_service.rb
+7
-12
doc/operations/incident_management/integrations.md
doc/operations/incident_management/integrations.md
+19
-0
ee/app/services/alert_management/network_alert_service.rb
ee/app/services/alert_management/network_alert_service.rb
+2
-1
spec/controllers/projects/alerting/notifications_controller_spec.rb
...ollers/projects/alerting/notifications_controller_spec.rb
+8
-2
spec/controllers/projects/prometheus/alerts_controller_spec.rb
...controllers/projects/prometheus/alerts_controller_spec.rb
+9
-2
spec/services/projects/prometheus/alerts/notify_service_spec.rb
...ervices/projects/prometheus/alerts/notify_service_spec.rb
+1
-2
spec/support/shared_examples/services/alert_management/alert_processing/alert_firing_shared_examples.rb
...nagement/alert_processing/alert_firing_shared_examples.rb
+5
-1
No files found.
app/controllers/projects/alerting/notifications_controller.rb
View file @
b84b1252
...
...
@@ -18,7 +18,11 @@ module Projects
token
=
extract_alert_manager_token
(
request
)
result
=
notify_service
.
execute
(
token
,
integration
)
head
result
.
http_status
if
result
.
success?
render
json:
AlertManagement
::
AlertSerializer
.
new
.
represent
(
result
.
payload
[
:alerts
]),
code:
result
.
http_status
else
head
result
.
http_status
end
end
private
...
...
app/controllers/projects/prometheus/alerts_controller.rb
View file @
b84b1252
...
...
@@ -30,7 +30,11 @@ module Projects
token
=
extract_alert_manager_token
(
request
)
result
=
notify_service
.
execute
(
token
)
head
result
.
http_status
if
result
.
success?
render
json:
AlertManagement
::
AlertSerializer
.
new
.
represent
(
result
.
payload
[
:alerts
]),
code:
result
.
http_status
else
head
result
.
http_status
end
end
def
create
...
...
app/serializers/alert_management/alert_entity.rb
0 → 100644
View file @
b84b1252
# frozen_string_literal: true
module
AlertManagement
class
AlertEntity
<
Grape
::
Entity
expose
:iid
expose
:title
end
end
app/serializers/alert_management/alert_serializer.rb
0 → 100644
View file @
b84b1252
# frozen_string_literal: true
module
AlertManagement
class
AlertSerializer
<
BaseSerializer
entity
AlertManagement
::
AlertEntity
end
end
app/services/alert_management/process_prometheus_alert_service.rb
View file @
b84b1252
...
...
@@ -4,6 +4,7 @@ module AlertManagement
class
ProcessPrometheusAlertService
extend
::
Gitlab
::
Utils
::
Override
include
::
AlertManagement
::
AlertProcessing
include
::
AlertManagement
::
Responses
def
initialize
(
project
,
payload
)
@project
=
project
...
...
@@ -18,7 +19,7 @@ module AlertManagement
complete_post_processing_tasks
ServiceResponse
.
success
success
(
alert
)
end
private
...
...
@@ -40,9 +41,5 @@ module AlertManagement
def
resolving_alert?
incoming_payload
.
resolved?
end
def
bad_request
ServiceResponse
.
error
(
message:
'Bad Request'
,
http_status: :bad_request
)
end
end
end
app/services/concerns/alert_management/responses.rb
0 → 100644
View file @
b84b1252
# frozen_string_literal: true
module
AlertManagement
# Module to hold common response logic for AlertManagement services.
module
Responses
def
success
(
alerts
)
ServiceResponse
.
success
(
payload:
{
alerts:
Array
(
alerts
)
})
end
def
bad_request
ServiceResponse
.
error
(
message:
'Bad Request'
,
http_status: :bad_request
)
end
def
unauthorized
ServiceResponse
.
error
(
message:
'Unauthorized'
,
http_status: :unauthorized
)
end
def
unprocessable_entity
ServiceResponse
.
error
(
message:
'Unprocessable Entity'
,
http_status: :unprocessable_entity
)
end
def
forbidden
ServiceResponse
.
error
(
message:
'Forbidden'
,
http_status: :forbidden
)
end
end
end
app/services/projects/alerting/notify_service.rb
View file @
b84b1252
...
...
@@ -5,6 +5,7 @@ module Projects
class
NotifyService
extend
::
Gitlab
::
Utils
::
Override
include
::
AlertManagement
::
AlertProcessing
include
::
AlertManagement
::
Responses
def
initialize
(
project
,
payload
)
@project
=
project
...
...
@@ -23,7 +24,7 @@ module Projects
complete_post_processing_tasks
ServiceResponse
.
success
success
(
alert
)
end
private
...
...
@@ -46,18 +47,6 @@ module Projects
def
valid_token?
(
token
)
token
==
integration
.
token
end
def
bad_request
ServiceResponse
.
error
(
message:
'Bad Request'
,
http_status: :bad_request
)
end
def
unauthorized
ServiceResponse
.
error
(
message:
'Unauthorized'
,
http_status: :unauthorized
)
end
def
forbidden
ServiceResponse
.
error
(
message:
'Forbidden'
,
http_status: :forbidden
)
end
end
end
end
app/services/projects/prometheus/alerts/notify_service.rb
View file @
b84b1252
...
...
@@ -6,6 +6,7 @@ module Projects
class
NotifyService
include
Gitlab
::
Utils
::
StrongMemoize
include
::
IncidentManagement
::
Settings
include
::
AlertManagement
::
Responses
# This set of keys identifies a payload as a valid Prometheus
# payload and thus processable by this service. See also
...
...
@@ -27,9 +28,9 @@ module Projects
return
unprocessable_entity
unless
self
.
class
.
processable?
(
payload
)
return
unauthorized
unless
valid_alert_manager_token?
(
token
,
integration
)
process_prometheus_alerts
alert_responses
=
process_prometheus_alerts
ServiceResponse
.
success
alert_response
(
alert_responses
)
end
def
self
.
processable?
(
payload
)
...
...
@@ -128,23 +129,17 @@ module Projects
end
def
process_prometheus_alerts
alerts
.
each
do
|
alert
|
alerts
.
map
do
|
alert
|
AlertManagement
::
ProcessPrometheusAlertService
.
new
(
project
,
alert
.
to_h
)
.
execute
end
end
def
bad_request
ServiceResponse
.
error
(
message:
'Bad Request'
,
http_status: :bad_request
)
end
def
unauthorized
ServiceResponse
.
error
(
message:
'Unauthorized'
,
http_status: :unauthorized
)
end
def
alert_response
(
alert_responses
)
alerts
=
alert_responses
.
map
{
|
resp
|
resp
.
payload
[
:alert
]
}.
compact
def
unprocessable_entity
ServiceResponse
.
error
(
message:
'Unprocessable Entity'
,
http_status: :unprocessable_entity
)
success
(
alerts
)
end
end
end
...
...
doc/operations/incident_management/integrations.md
View file @
b84b1252
...
...
@@ -196,6 +196,25 @@ WARNING:
Using your authorization key in the URL is insecure, as it's visible in server logs. We recommend
using one of the above header options if your tooling supports it.
## Response Body
The JSON response body contains a list of any alerts created within the request:
```
json
[
{
"iid"
:
1
,
"title"
:
"Incident title"
},
{
"iid"
:
2
,
"title"
:
"Second Incident title"
}
]
```
Successful responses return a
`200`
response code.
## Triggering test alerts
> [Introduced](https://gitlab.com/groups/gitlab-org/-/epics/3066) in GitLab in 13.2.
...
...
ee/app/services/alert_management/network_alert_service.rb
View file @
b84b1252
...
...
@@ -5,6 +5,7 @@ module AlertManagement
class
NetworkAlertService
extend
::
Gitlab
::
Utils
::
Override
include
::
AlertManagement
::
AlertProcessing
include
::
AlertManagement
::
Responses
MONITORING_TOOL
=
Gitlab
::
AlertManagement
::
Payload
::
MONITORING_TOOLS
.
fetch
(
:cilium
)
...
...
@@ -20,7 +21,7 @@ module AlertManagement
return
bad_request
unless
alert
.
persisted?
ServiceResponse
.
success
success
(
alert
)
end
private
...
...
spec/controllers/projects/alerting/notifications_controller_spec.rb
View file @
b84b1252
...
...
@@ -16,7 +16,9 @@ RSpec.describe Projects::Alerting::NotificationsController do
end
shared_examples
'process alert payload'
do
|
notify_service_class
|
let
(
:service_response
)
{
ServiceResponse
.
success
}
let
(
:alert_1
)
{
build
(
:alert_management_alert
,
project:
project
)
}
let
(
:alert_2
)
{
build
(
:alert_management_alert
,
project:
project
)
}
let
(
:service_response
)
{
ServiceResponse
.
success
(
payload:
{
alerts:
[
alert_1
,
alert_2
]
})
}
let
(
:notify_service
)
{
instance_double
(
notify_service_class
,
execute:
service_response
)
}
before
do
...
...
@@ -30,9 +32,13 @@ RSpec.describe Projects::Alerting::NotificationsController do
context
'when notification service succeeds'
do
let
(
:permitted_params
)
{
ActionController
::
Parameters
.
new
(
payload
).
permit!
}
it
'responds with
ok
'
do
it
'responds with
the alert data
'
do
make_request
expect
(
json_response
).
to
contain_exactly
(
{
'iid'
=>
alert_1
.
iid
,
'title'
=>
alert_1
.
title
},
{
'iid'
=>
alert_2
.
iid
,
'title'
=>
alert_2
.
title
}
)
expect
(
response
).
to
have_gitlab_http_status
(
:ok
)
end
...
...
spec/controllers/projects/prometheus/alerts_controller_spec.rb
View file @
b84b1252
...
...
@@ -160,7 +160,9 @@ RSpec.describe Projects::Prometheus::AlertsController do
end
describe
'POST #notify'
do
let
(
:service_response
)
{
ServiceResponse
.
success
}
let
(
:alert_1
)
{
build
(
:alert_management_alert
,
:prometheus
,
project:
project
)
}
let
(
:alert_2
)
{
build
(
:alert_management_alert
,
:prometheus
,
project:
project
)
}
let
(
:service_response
)
{
ServiceResponse
.
success
(
payload:
{
alerts:
[
alert_1
,
alert_2
]
})
}
let
(
:notify_service
)
{
instance_double
(
Projects
::
Prometheus
::
Alerts
::
NotifyService
,
execute:
service_response
)
}
before
do
...
...
@@ -173,10 +175,15 @@ RSpec.describe Projects::Prometheus::AlertsController do
end
it
'returns ok if notification succeeds'
do
expect
(
notify_service
).
to
receive
(
:execute
).
and_return
(
ServiceResponse
.
success
)
expect
(
notify_service
).
to
receive
(
:execute
).
and_return
(
service_response
)
post
:notify
,
params:
project_params
,
session:
{
as: :json
}
expect
(
json_response
).
to
contain_exactly
(
{
'iid'
=>
alert_1
.
iid
,
'title'
=>
alert_1
.
title
},
{
'iid'
=>
alert_2
.
iid
,
'title'
=>
alert_2
.
title
}
)
expect
(
response
).
to
have_gitlab_http_status
(
:ok
)
end
...
...
spec/services/projects/prometheus/alerts/notify_service_spec.rb
View file @
b84b1252
...
...
@@ -218,8 +218,7 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService do
.
to
receive
(
:new
)
.
with
(
project
,
kind_of
(
Hash
))
.
exactly
(
3
).
times
.
and_return
(
process_service
)
expect
(
process_service
).
to
receive
(
:execute
).
exactly
(
3
).
times
.
and_call_original
subject
end
...
...
spec/support/shared_examples/services/alert_management/alert_processing/alert_firing_shared_examples.rb
View file @
b84b1252
...
...
@@ -3,7 +3,10 @@
# This shared_example requires the following variables:
# - `service`, the service which includes AlertManagement::AlertProcessing
RSpec
.
shared_examples
'creates an alert management alert or errors'
do
it
{
is_expected
.
to
be_success
}
specify
do
expect
(
subject
).
to
be_success
expect
(
subject
.
payload
).
to
match
(
alerts:
all
(
a_kind_of
(
AlertManagement
::
Alert
)))
end
it
'creates AlertManagement::Alert'
do
expect
(
Gitlab
::
AppLogger
).
not_to
receive
(
:warn
)
...
...
@@ -89,6 +92,7 @@ RSpec.shared_examples 'adds an alert management alert event' do
expect
{
subject
}.
to
change
{
alert
.
reload
.
events
}.
by
(
1
)
expect
(
subject
).
to
be_success
expect
(
subject
.
payload
).
to
match
(
alerts:
all
(
a_kind_of
(
AlertManagement
::
Alert
)))
end
it_behaves_like
'does not create an alert management alert'
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment