Commit 305ae13d authored by Sarah Yasonik's avatar Sarah Yasonik Committed by Nick Thomas

Expose runbook in alert GraphQL API

parent f53bd910
......@@ -94,8 +94,12 @@ module Types
field :metrics_dashboard_url,
GraphQL::STRING_TYPE,
null: true,
description: 'URL for metrics embed for the alert',
resolve: -> (alert, _args, _context) { alert.present.metrics_dashboard_url }
description: 'URL for metrics embed for the alert'
field :runbook,
GraphQL::STRING_TYPE,
null: true,
description: 'Runbook for the alert as defined in alert details'
field :todos,
Types::TodoType.connection_type,
......
......@@ -118,6 +118,7 @@ module AlertManagement
end
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_status, -> (status) { where(status: status) }
......
......@@ -37,6 +37,12 @@ module AlertManagement
MARKDOWN
end
def runbook
strong_memoize(:runbook) do
payload&.dig('runbook')
end
end
def metrics_dashboard_url; end
private
......
......@@ -2,6 +2,12 @@
module AlertManagement
class PrometheusAlertPresenter < AlertManagement::AlertPresenter
def runbook
strong_memoize(:runbook) do
payload&.dig('annotations', 'runbook')
end
end
def metrics_dashboard_url
alerting_alert.metrics_dashboard_url
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 {
last: Int
): NoteConnection!
"""
Runbook for the alert as defined in alert details
"""
runbook: String
"""
Service the alert came from
"""
......
......@@ -801,6 +801,20 @@
"isDeprecated": false,
"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",
"description": "Service the alert came from",
......@@ -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 |
| `metricsDashboardUrl` | String | URL for metrics embed for the alert |
| `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 |
| `severity` | AlertManagementSeverity | Severity of the alert |
| `startedAt` | Time | Timestamp the alert was raised |
......
......@@ -28,6 +28,7 @@ RSpec.describe GitlabSchema.types['AlertManagementAlert'] do
notes
discussions
metrics_dashboard_url
runbook
todos
]
......
......@@ -8,11 +8,12 @@ RSpec.describe AlertManagement::AlertPresenter do
{
'title' => 'Alert title',
'start_time' => '2020-04-27T10:10:22.265949279Z',
'custom' => { 'param' => 73 }
'custom' => { 'param' => 73 },
'runbook' => 'https://runbook.com'
}
end
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
subject(:presenter) { described_class.new(alert) }
......@@ -34,7 +35,8 @@ RSpec.describe AlertManagement::AlertPresenter do
#### Alert Details
**custom.param:** 73
**custom.param:** 73#{markdown_line_break}
**runbook:** https://runbook.com
MARKDOWN
)
end
......@@ -45,4 +47,10 @@ RSpec.describe AlertManagement::AlertPresenter do
expect(presenter.metrics_dashboard_url).to be_nil
end
end
describe '#runbook' do
it 'shows the runbook from the payload' do
expect(presenter.runbook).to eq('https://runbook.com')
end
end
end
......@@ -4,7 +4,7 @@ require 'spec_helper'
RSpec.describe AlertManagement::PrometheusAlertPresenter do
let_it_be(:project) { create(:project) }
let_it_be(:payload) do
let(:payload) do
{
'annotations' => {
'title' => 'Alert title',
......@@ -15,6 +15,7 @@ RSpec.describe AlertManagement::PrometheusAlertPresenter do
'generatorURL' => 'http://8d467bd4607a:9090/graph?g0.expr=vector%281%29&g0.tab=1'
}
end
let(:alert) do
create(:alert_management_alert, :prometheus, project: project, payload: payload)
end
......@@ -65,4 +66,17 @@ RSpec.describe AlertManagement::PrometheusAlertPresenter do
it { is_expected.to eq(dashboard_url_for_alert) }
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
......@@ -4,7 +4,7 @@ require 'spec_helper'
RSpec.describe 'getting Alert Management Alerts' do
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(:current_user) { create(:user) }
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
'eventCount' => triggered_alert.events,
'startedAt' => triggered_alert.started_at.strftime('%Y-%m-%dT%H:%M:%SZ'),
'endedAt' => nil,
'details' => { 'custom.alert' => 'payload' },
'details' => { 'custom.alert' => 'payload', 'runbook' => 'runbook' },
'createdAt' => triggered_alert.created_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(
......
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