Commit adb328bd authored by Nick Thomas's avatar Nick Thomas

Merge branch 'sy-firing-alert-runbook-be' into 'master'

Expose runbook in alert GraphQL API

See merge request gitlab-org/gitlab!38510
parents e5a5e54a 305ae13d
...@@ -94,8 +94,12 @@ module Types ...@@ -94,8 +94,12 @@ module Types
field :metrics_dashboard_url, field :metrics_dashboard_url,
GraphQL::STRING_TYPE, GraphQL::STRING_TYPE,
null: true, null: true,
description: 'URL for metrics embed for the alert', description: 'URL for metrics embed for the alert'
resolve: -> (alert, _args, _context) { alert.present.metrics_dashboard_url }
field :runbook,
GraphQL::STRING_TYPE,
null: true,
description: 'Runbook for the alert as defined in alert details'
field :todos, field :todos,
Types::TodoType.connection_type, Types::TodoType.connection_type,
......
...@@ -118,6 +118,7 @@ module AlertManagement ...@@ -118,6 +118,7 @@ module AlertManagement
end end
delegate :iid, to: :issue, prefix: true, allow_nil: true delegate :iid, to: :issue, prefix: true, allow_nil: true
delegate :metrics_dashboard_url, :runbook, to: :present
scope :for_iid, -> (iid) { where(iid: iid) } scope :for_iid, -> (iid) { where(iid: iid) }
scope :for_status, -> (status) { where(status: status) } scope :for_status, -> (status) { where(status: status) }
......
...@@ -37,6 +37,12 @@ module AlertManagement ...@@ -37,6 +37,12 @@ module AlertManagement
MARKDOWN MARKDOWN
end end
def runbook
strong_memoize(:runbook) do
payload&.dig('runbook')
end
end
def metrics_dashboard_url; end def metrics_dashboard_url; end
private private
......
...@@ -2,6 +2,12 @@ ...@@ -2,6 +2,12 @@
module AlertManagement module AlertManagement
class PrometheusAlertPresenter < AlertManagement::AlertPresenter class PrometheusAlertPresenter < AlertManagement::AlertPresenter
def runbook
strong_memoize(:runbook) do
payload&.dig('annotations', 'runbook')
end
end
def metrics_dashboard_url def metrics_dashboard_url
alerting_alert.metrics_dashboard_url alerting_alert.metrics_dashboard_url
end end
......
---
title: Expose runbook field in alert_management_alert GraphQL API
merge_request: 38510
author:
type: added
...@@ -294,6 +294,11 @@ type AlertManagementAlert implements Noteable { ...@@ -294,6 +294,11 @@ type AlertManagementAlert implements Noteable {
last: Int last: Int
): NoteConnection! ): NoteConnection!
"""
Runbook for the alert as defined in alert details
"""
runbook: String
""" """
Service the alert came from Service the alert came from
""" """
......
...@@ -801,6 +801,20 @@ ...@@ -801,6 +801,20 @@
"isDeprecated": false, "isDeprecated": false,
"deprecationReason": null "deprecationReason": null
}, },
{
"name": "runbook",
"description": "Runbook for the alert as defined in alert details",
"args": [
],
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{ {
"name": "service", "name": "service",
"description": "Service the alert came from", "description": "Service the alert came from",
...@@ -71,6 +71,7 @@ Describes an alert from the project's Alert Management ...@@ -71,6 +71,7 @@ Describes an alert from the project's Alert Management
| `issueIid` | ID | Internal ID of the GitLab issue attached to the alert | | `issueIid` | ID | Internal ID of the GitLab issue attached to the alert |
| `metricsDashboardUrl` | String | URL for metrics embed for the alert | | `metricsDashboardUrl` | String | URL for metrics embed for the alert |
| `monitoringTool` | String | Monitoring tool the alert came from | | `monitoringTool` | String | Monitoring tool the alert came from |
| `runbook` | String | Runbook for the alert as defined in alert details |
| `service` | String | Service the alert came from | | `service` | String | Service the alert came from |
| `severity` | AlertManagementSeverity | Severity of the alert | | `severity` | AlertManagementSeverity | Severity of the alert |
| `startedAt` | Time | Timestamp the alert was raised | | `startedAt` | Time | Timestamp the alert was raised |
......
...@@ -28,6 +28,7 @@ RSpec.describe GitlabSchema.types['AlertManagementAlert'] do ...@@ -28,6 +28,7 @@ RSpec.describe GitlabSchema.types['AlertManagementAlert'] do
notes notes
discussions discussions
metrics_dashboard_url metrics_dashboard_url
runbook
todos todos
] ]
......
...@@ -8,11 +8,12 @@ RSpec.describe AlertManagement::AlertPresenter do ...@@ -8,11 +8,12 @@ RSpec.describe AlertManagement::AlertPresenter do
{ {
'title' => 'Alert title', 'title' => 'Alert title',
'start_time' => '2020-04-27T10:10:22.265949279Z', 'start_time' => '2020-04-27T10:10:22.265949279Z',
'custom' => { 'param' => 73 } 'custom' => { 'param' => 73 },
'runbook' => 'https://runbook.com'
} }
end end
let_it_be(:alert) do let_it_be(:alert) do
create(:alert_management_alert, :with_description, :with_host, :with_service, :with_monitoring_tool, project: project, payload: generic_payload) build(:alert_management_alert, :with_description, :with_host, :with_service, :with_monitoring_tool, project: project, payload: generic_payload)
end end
subject(:presenter) { described_class.new(alert) } subject(:presenter) { described_class.new(alert) }
...@@ -34,7 +35,8 @@ RSpec.describe AlertManagement::AlertPresenter do ...@@ -34,7 +35,8 @@ RSpec.describe AlertManagement::AlertPresenter do
#### Alert Details #### Alert Details
**custom.param:** 73 **custom.param:** 73#{markdown_line_break}
**runbook:** https://runbook.com
MARKDOWN MARKDOWN
) )
end end
...@@ -45,4 +47,10 @@ RSpec.describe AlertManagement::AlertPresenter do ...@@ -45,4 +47,10 @@ RSpec.describe AlertManagement::AlertPresenter do
expect(presenter.metrics_dashboard_url).to be_nil expect(presenter.metrics_dashboard_url).to be_nil
end end
end end
describe '#runbook' do
it 'shows the runbook from the payload' do
expect(presenter.runbook).to eq('https://runbook.com')
end
end
end end
...@@ -4,7 +4,7 @@ require 'spec_helper' ...@@ -4,7 +4,7 @@ require 'spec_helper'
RSpec.describe AlertManagement::PrometheusAlertPresenter do RSpec.describe AlertManagement::PrometheusAlertPresenter do
let_it_be(:project) { create(:project) } let_it_be(:project) { create(:project) }
let_it_be(:payload) do let(:payload) do
{ {
'annotations' => { 'annotations' => {
'title' => 'Alert title', 'title' => 'Alert title',
...@@ -15,6 +15,7 @@ RSpec.describe AlertManagement::PrometheusAlertPresenter do ...@@ -15,6 +15,7 @@ RSpec.describe AlertManagement::PrometheusAlertPresenter do
'generatorURL' => 'http://8d467bd4607a:9090/graph?g0.expr=vector%281%29&g0.tab=1' 'generatorURL' => 'http://8d467bd4607a:9090/graph?g0.expr=vector%281%29&g0.tab=1'
} }
end end
let(:alert) do let(:alert) do
create(:alert_management_alert, :prometheus, project: project, payload: payload) create(:alert_management_alert, :prometheus, project: project, payload: payload)
end end
...@@ -65,4 +66,17 @@ RSpec.describe AlertManagement::PrometheusAlertPresenter do ...@@ -65,4 +66,17 @@ RSpec.describe AlertManagement::PrometheusAlertPresenter do
it { is_expected.to eq(dashboard_url_for_alert) } it { is_expected.to eq(dashboard_url_for_alert) }
end end
end end
describe '#runbook' do
subject { presenter.runbook }
it { is_expected.to be_nil }
context 'with runbook in payload' do
let(:expected_runbook) { 'https://awesome-runbook.com' }
let(:payload) { { 'annotations' => { 'runbook' => expected_runbook } } }
it { is_expected.to eq(expected_runbook) }
end
end
end end
...@@ -4,7 +4,7 @@ require 'spec_helper' ...@@ -4,7 +4,7 @@ require 'spec_helper'
RSpec.describe 'getting Alert Management Alerts' do RSpec.describe 'getting Alert Management Alerts' do
include GraphqlHelpers include GraphqlHelpers
let_it_be(:payload) { { 'custom' => { 'alert' => 'payload' } } } let_it_be(:payload) { { 'custom' => { 'alert' => 'payload' }, 'runbook' => 'runbook' } }
let_it_be(:project) { create(:project, :repository) } let_it_be(:project) { create(:project, :repository) }
let_it_be(:current_user) { create(:user) } let_it_be(:current_user) { create(:user) }
let_it_be(:resolved_alert) { create(:alert_management_alert, :all_fields, :resolved, project: project, issue: nil, severity: :low) } let_it_be(:resolved_alert) { create(:alert_management_alert, :all_fields, :resolved, project: project, issue: nil, severity: :low) }
...@@ -71,10 +71,11 @@ RSpec.describe 'getting Alert Management Alerts' do ...@@ -71,10 +71,11 @@ RSpec.describe 'getting Alert Management Alerts' do
'eventCount' => triggered_alert.events, 'eventCount' => triggered_alert.events,
'startedAt' => triggered_alert.started_at.strftime('%Y-%m-%dT%H:%M:%SZ'), 'startedAt' => triggered_alert.started_at.strftime('%Y-%m-%dT%H:%M:%SZ'),
'endedAt' => nil, 'endedAt' => nil,
'details' => { 'custom.alert' => 'payload' }, 'details' => { 'custom.alert' => 'payload', 'runbook' => 'runbook' },
'createdAt' => triggered_alert.created_at.strftime('%Y-%m-%dT%H:%M:%SZ'), 'createdAt' => triggered_alert.created_at.strftime('%Y-%m-%dT%H:%M:%SZ'),
'updatedAt' => triggered_alert.updated_at.strftime('%Y-%m-%dT%H:%M:%SZ'), 'updatedAt' => triggered_alert.updated_at.strftime('%Y-%m-%dT%H:%M:%SZ'),
'metricsDashboardUrl' => nil 'metricsDashboardUrl' => nil,
'runbook' => 'runbook'
) )
expect(second_alert).to include( expect(second_alert).to include(
......
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