-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathservicelevelobjective_test.go
380 lines (369 loc) · 12.1 KB
/
servicelevelobjective_test.go
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package controllers
import (
"fmt"
"testing"
"github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
pyrrav1alpha1 "github.com/pyrra-dev/pyrra/kubernetes/api/v1alpha1"
"github.com/pyrra-dev/pyrra/slo"
)
var (
trueBool = true
httpSLO = pyrrav1alpha1.ServiceLevelObjective{
TypeMeta: metav1.TypeMeta{
APIVersion: pyrrav1alpha1.GroupVersion.Version,
Kind: "ServiceLevelObjective",
},
ObjectMeta: metav1.ObjectMeta{
Name: "http",
UID: "123",
Labels: map[string]string{
slo.PropagationLabelsPrefix + "team": "foo",
"team": "bar",
},
Annotations: map[string]string{
slo.PropagationLabelsPrefix + "description": "foo",
"description": "bar",
},
},
Spec: pyrrav1alpha1.ServiceLevelObjectiveSpec{
Target: "99.5",
Window: "28d",
ServiceLevelIndicator: pyrrav1alpha1.ServiceLevelIndicator{
Ratio: &pyrrav1alpha1.RatioIndicator{
Errors: pyrrav1alpha1.Query{
Metric: `http_requests_total{job="app",status=~"5.."}`,
},
Total: pyrrav1alpha1.Query{
Metric: `http_requests_total{job="app"}`,
},
},
},
},
}
)
func Test_makePrometheusRule(t *testing.T) {
tests := []struct {
name string
objective pyrrav1alpha1.ServiceLevelObjective
rules *monitoringv1.PrometheusRule
}{
{
name: "http",
objective: httpSLO,
rules: &monitoringv1.PrometheusRule{
TypeMeta: metav1.TypeMeta{
APIVersion: monitoring.GroupName + "/" + monitoringv1.Version,
Kind: monitoringv1.PrometheusRuleKind,
},
ObjectMeta: metav1.ObjectMeta{
Name: "http",
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: pyrrav1alpha1.GroupVersion.Version,
Kind: "ServiceLevelObjective",
Name: "http",
UID: "123",
Controller: &trueBool,
},
},
Labels: map[string]string{
"pyrra.dev/team": "foo",
"team": "bar",
},
},
Spec: monitoringv1.PrometheusRuleSpec{
Groups: []monitoringv1.RuleGroup{
{
Name: "http-increase",
Interval: "2m30s",
Rules: []monitoringv1.Rule{
{
Record: "http_requests:increase4w",
Expr: intstr.FromString(`sum by (status) (increase(http_requests_total{job="app"}[4w]))`),
Labels: map[string]string{
"job": "app",
"slo": "http",
"team": "foo",
},
},
{
Alert: "SLOMetricAbsent",
Expr: intstr.FromString(`absent(http_requests_total{job="app"}) == 1`),
For: "2m",
Annotations: map[string]string{
"description": "foo",
},
Labels: map[string]string{
"severity": "critical",
"job": "app",
"slo": "http",
"team": "foo",
},
},
},
},
{
Name: "http",
Interval: "30s",
Rules: []monitoringv1.Rule{
{
Record: "http_requests:burnrate5m",
Expr: intstr.FromString(`sum(rate(http_requests_total{job="app",status=~"5.."}[5m])) / sum(rate(http_requests_total{job="app"}[5m]))`),
Labels: map[string]string{"job": "app", "slo": "http", "team": "foo"},
},
{
Record: "http_requests:burnrate30m",
Expr: intstr.FromString(`sum(rate(http_requests_total{job="app",status=~"5.."}[30m])) / sum(rate(http_requests_total{job="app"}[30m]))`),
Labels: map[string]string{"job": "app", "slo": "http", "team": "foo"},
},
{
Record: "http_requests:burnrate1h",
Expr: intstr.FromString(`sum(rate(http_requests_total{job="app",status=~"5.."}[1h])) / sum(rate(http_requests_total{job="app"}[1h]))`),
Labels: map[string]string{"job": "app", "slo": "http", "team": "foo"},
},
{
Record: "http_requests:burnrate2h",
Expr: intstr.FromString(`sum(rate(http_requests_total{job="app",status=~"5.."}[2h])) / sum(rate(http_requests_total{job="app"}[2h]))`),
Labels: map[string]string{"job": "app", "slo": "http", "team": "foo"},
},
{
Record: "http_requests:burnrate6h",
Expr: intstr.FromString(`sum(rate(http_requests_total{job="app",status=~"5.."}[6h])) / sum(rate(http_requests_total{job="app"}[6h]))`),
Labels: map[string]string{"job": "app", "slo": "http", "team": "foo"},
},
{
Record: "http_requests:burnrate1d",
Expr: intstr.FromString(`sum(rate(http_requests_total{job="app",status=~"5.."}[1d])) / sum(rate(http_requests_total{job="app"}[1d]))`),
Labels: map[string]string{"job": "app", "slo": "http", "team": "foo"},
},
{
Record: "http_requests:burnrate4d",
Expr: intstr.FromString(`sum(rate(http_requests_total{job="app",status=~"5.."}[4d])) / sum(rate(http_requests_total{job="app"}[4d]))`),
Labels: map[string]string{"job": "app", "slo": "http", "team": "foo"},
},
{
Alert: "ErrorBudgetBurn",
Expr: intstr.FromString(`http_requests:burnrate5m{job="app",slo="http"} > (14 * (1-0.995)) and http_requests:burnrate1h{job="app",slo="http"} > (14 * (1-0.995))`),
For: "2m",
Labels: map[string]string{"severity": "critical", "job": "app", "long": "1h", "slo": "http", "short": "5m", "team": "foo", "exhaustion": "2d"},
Annotations: map[string]string{"description": "foo"},
},
{
Alert: "ErrorBudgetBurn",
Expr: intstr.FromString(`http_requests:burnrate30m{job="app",slo="http"} > (5.6 * (1-0.995)) and http_requests:burnrate6h{job="app",slo="http"} > (5.6 * (1-0.995))`),
For: "15m",
Labels: map[string]string{"severity": "critical", "job": "app", "long": "6h", "slo": "http", "short": "30m", "team": "foo", "exhaustion": "5d"},
Annotations: map[string]string{"description": "foo"},
},
{
Alert: "ErrorBudgetBurn",
Expr: intstr.FromString(`http_requests:burnrate2h{job="app",slo="http"} > (2.8 * (1-0.995)) and http_requests:burnrate1d{job="app",slo="http"} > (2.8 * (1-0.995))`),
For: "1h",
Labels: map[string]string{"severity": "warning", "job": "app", "long": "1d", "slo": "http", "short": "2h", "team": "foo", "exhaustion": "10d"},
Annotations: map[string]string{"description": "foo"},
},
{
Alert: "ErrorBudgetBurn",
Expr: intstr.FromString(`http_requests:burnrate6h{job="app",slo="http"} > (1 * (1-0.995)) and http_requests:burnrate4d{job="app",slo="http"} > (1 * (1-0.995))`),
For: "3h",
Labels: map[string]string{"severity": "warning", "job": "app", "long": "4d", "slo": "http", "short": "6h", "team": "foo", "exhaustion": "4w"},
Annotations: map[string]string{"description": "foo"},
},
},
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
prometheusRule, err := makePrometheusRule(tt.objective, false)
require.NoError(t, err)
require.Equal(t, tt.rules, prometheusRule)
})
}
}
func Test_makeConfigMap(t *testing.T) {
rules := `groups:
- interval: 2m30s
name: http-increase
rules:
- expr: sum by (status) (increase(http_requests_total{job="app"}[4w]))
labels:
job: app
slo: http
team: foo
record: http_requests:increase4w
- alert: SLOMetricAbsent
annotations:
description: foo
expr: absent(http_requests_total{job="app"}) == 1
for: 2m
labels:
job: app
severity: critical
slo: http
team: foo
- interval: 30s
name: http
rules:
- expr: sum(rate(http_requests_total{job="app",status=~"5.."}[5m])) / sum(rate(http_requests_total{job="app"}[5m]))
labels:
job: app
slo: http
team: foo
record: http_requests:burnrate5m
- expr: sum(rate(http_requests_total{job="app",status=~"5.."}[30m])) / sum(rate(http_requests_total{job="app"}[30m]))
labels:
job: app
slo: http
team: foo
record: http_requests:burnrate30m
- expr: sum(rate(http_requests_total{job="app",status=~"5.."}[1h])) / sum(rate(http_requests_total{job="app"}[1h]))
labels:
job: app
slo: http
team: foo
record: http_requests:burnrate1h
- expr: sum(rate(http_requests_total{job="app",status=~"5.."}[2h])) / sum(rate(http_requests_total{job="app"}[2h]))
labels:
job: app
slo: http
team: foo
record: http_requests:burnrate2h
- expr: sum(rate(http_requests_total{job="app",status=~"5.."}[6h])) / sum(rate(http_requests_total{job="app"}[6h]))
labels:
job: app
slo: http
team: foo
record: http_requests:burnrate6h
- expr: sum(rate(http_requests_total{job="app",status=~"5.."}[1d])) / sum(rate(http_requests_total{job="app"}[1d]))
labels:
job: app
slo: http
team: foo
record: http_requests:burnrate1d
- expr: sum(rate(http_requests_total{job="app",status=~"5.."}[4d])) / sum(rate(http_requests_total{job="app"}[4d]))
labels:
job: app
slo: http
team: foo
record: http_requests:burnrate4d
- alert: ErrorBudgetBurn
annotations:
description: foo
expr: http_requests:burnrate5m{job="app",slo="http"} > (14 * (1-0.995)) and http_requests:burnrate1h{job="app",slo="http"}
> (14 * (1-0.995))
for: 2m
labels:
exhaustion: 2d
job: app
long: 1h
severity: critical
short: 5m
slo: http
team: foo
- alert: ErrorBudgetBurn
annotations:
description: foo
expr: http_requests:burnrate30m{job="app",slo="http"} > (5.6 * (1-0.995)) and
http_requests:burnrate6h{job="app",slo="http"} > (5.6 * (1-0.995))
for: 15m
labels:
exhaustion: 5d
job: app
long: 6h
severity: critical
short: 30m
slo: http
team: foo
- alert: ErrorBudgetBurn
annotations:
description: foo
expr: http_requests:burnrate2h{job="app",slo="http"} > (2.8 * (1-0.995)) and http_requests:burnrate1d{job="app",slo="http"}
> (2.8 * (1-0.995))
for: 1h
labels:
exhaustion: 10d
job: app
long: 1d
severity: warning
short: 2h
slo: http
team: foo
- alert: ErrorBudgetBurn
annotations:
description: foo
expr: http_requests:burnrate6h{job="app",slo="http"} > (1 * (1-0.995)) and http_requests:burnrate4d{job="app",slo="http"}
> (1 * (1-0.995))
for: 3h
labels:
exhaustion: 4w
job: app
long: 4d
severity: warning
short: 6h
slo: http
team: foo
`
testcases := []struct {
name string
configMapName string
objective pyrrav1alpha1.ServiceLevelObjective
want *corev1.ConfigMap
err error
}{
{
name: "NoInput",
err: fmt.Errorf("failed to get objective"),
},
{
name: "HTTP",
configMapName: "http",
objective: httpSLO,
want: &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "ConfigMap",
},
ObjectMeta: metav1.ObjectMeta{
Name: "http",
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: pyrrav1alpha1.GroupVersion.Version,
Kind: "ServiceLevelObjective",
Name: "http",
UID: "123",
Controller: &trueBool,
},
},
Labels: map[string]string{
slo.PropagationLabelsPrefix + "team": "foo",
"team": "bar",
},
},
Data: map[string]string{
"http.rules.yaml": rules,
},
},
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
configMap, err := makeConfigMap(tc.configMapName, tc.objective, false)
if tc.err != nil {
require.Error(t, err)
require.ErrorAs(t, err, &tc.err)
} else {
require.NoError(t, err)
}
require.Equal(t, tc.want, configMap)
})
}
}