blob: 862b5b2bdc39ab5ffb6391b01bbae9a67569d5f2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::AlertManagement::Payload::ManagedPrometheus do
let_it_be(:project) { create(:project) }
let(:raw_payload) { {} }
let(:parsed_payload) { described_class.new(project: project, payload: raw_payload) }
it_behaves_like 'subclass has expected api'
shared_context 'with gitlab alert' do
let_it_be(:gitlab_alert) { create(:prometheus_alert, project: project) }
let(:metric_id) { gitlab_alert.prometheus_metric_id.to_s }
let(:alert_id) { gitlab_alert.id.to_s }
end
describe '#metric_id' do
subject { parsed_payload.metric_id }
it { is_expected.to be_nil }
context 'with gitlab_alert_id' do
let(:raw_payload) { { 'labels' => { 'gitlab_alert_id' => '12' } } }
it { is_expected.to eq(12) }
end
end
describe '#gitlab_prometheus_alert_id' do
subject { parsed_payload.gitlab_prometheus_alert_id }
it { is_expected.to be_nil }
context 'with gitlab_alert_id' do
let(:raw_payload) { { 'labels' => { 'gitlab_prometheus_alert_id' => '12' } } }
it { is_expected.to eq(12) }
end
end
describe '#gitlab_alert' do
subject { parsed_payload.gitlab_alert }
context 'without alert info in payload' do
it { is_expected.to be_nil }
end
context 'with metric id in payload' do
let(:raw_payload) { { 'labels' => { 'gitlab_alert_id' => metric_id } } }
let(:metric_id) { '-1' }
context 'without matching alert' do
it { is_expected.to be_nil }
end
context 'with matching alert' do
include_context 'with gitlab alert'
it { is_expected.to eq(gitlab_alert) }
context 'when unclear which alert applies' do
# With multiple alerts for different environments,
# we can't be sure which prometheus alert the payload
# belongs to
let_it_be(:another_alert) do
create(:prometheus_alert,
prometheus_metric: gitlab_alert.prometheus_metric,
project: project)
end
it { is_expected.to be_nil }
end
end
end
context 'with alert id' do
# gitlab_prometheus_alert_id is a stronger identifier,
# but was added after gitlab_alert_id; we won't
# see it without gitlab_alert_id also present
let(:raw_payload) do
{
'labels' => {
'gitlab_alert_id' => metric_id,
'gitlab_prometheus_alert_id' => alert_id
}
}
end
context 'without matching alert' do
let(:alert_id) { '-1' }
let(:metric_id) { '-1' }
it { is_expected.to be_nil }
end
context 'with matching alerts' do
include_context 'with gitlab alert'
it { is_expected.to eq(gitlab_alert) }
end
end
end
describe '#full_query' do
subject { parsed_payload.full_query }
it { is_expected.to be_nil }
context 'with gitlab alert' do
include_context 'with gitlab alert'
let(:raw_payload) { { 'labels' => { 'gitlab_alert_id' => metric_id } } }
it { is_expected.to eq(gitlab_alert.full_query) }
end
context 'with sufficient fallback info' do
let(:raw_payload) { { 'generatorURL' => 'http://localhost:9090/graph?g0.expr=vector%281%29' } }
it { is_expected.to eq('vector(1)') }
end
end
describe '#environment' do
subject { parsed_payload.environment }
context 'with gitlab alert' do
include_context 'with gitlab alert'
let(:raw_payload) { { 'labels' => { 'gitlab_alert_id' => metric_id } } }
it { is_expected.to eq(gitlab_alert.environment) }
end
context 'with sufficient fallback info' do
let_it_be(:environment) { create(:environment, project: project, name: 'production') }
let(:raw_payload) do
{
'labels' => {
'gitlab_alert_id' => '-1',
'gitlab_environment_name' => 'production'
}
}
end
it { is_expected.to eq(environment) }
end
end
describe '#metrics_dashboard_url' do
subject { parsed_payload.metrics_dashboard_url }
context 'without alert' do
it { is_expected.to be_nil }
end
context 'with gitlab alert' do
include_context 'gitlab-managed prometheus alert attributes' do
let(:raw_payload) { payload }
end
it { is_expected.to eq(dashboard_url_for_alert) }
end
end
end
|