-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
rule_test.go
857 lines (744 loc) · 28.3 KB
/
rule_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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package e2e_test
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"testing"
"time"
"github.com/efficientgo/e2e"
e2emon "github.com/efficientgo/e2e/monitoring"
e2eobs "github.com/efficientgo/e2e/observable"
common_cfg "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/discovery/targetgroup"
"github.com/thanos-io/thanos/pkg/errors"
"gopkg.in/yaml.v2"
"github.com/efficientgo/core/testutil"
"github.com/thanos-io/thanos/pkg/alert"
"github.com/thanos-io/thanos/pkg/clientconfig"
"github.com/thanos-io/thanos/pkg/promclient"
"github.com/thanos-io/thanos/pkg/rules/rulespb"
"github.com/thanos-io/thanos/pkg/runutil"
"github.com/thanos-io/thanos/test/e2e/e2ethanos"
)
const (
testAlertRuleAbortOnPartialResponse = `
groups:
- name: example_abort
interval: 1s
# Abort should be a default: partial_response_strategy: "ABORT"
rules:
- alert: TestAlert_AbortOnPartialResponse
# It must be based on actual metrics otherwise call to StoreAPI would be not involved.
expr: absent(some_metric)
labels:
severity: page
annotations:
summary: "I always complain, but I don't allow partial response in query."
`
testAlertRuleWarnOnPartialResponse = `
groups:
- name: example_warn
interval: 1s
partial_response_strategy: "WARN"
rules:
- alert: TestAlert_WarnOnPartialResponse
# It must be based on actual metric, otherwise call to StoreAPI would be not involved.
expr: absent(some_metric)
labels:
severity: page
annotations:
summary: "I always complain and allow partial response in query."
`
testAlertRuleAddedLaterWebHandler = `
groups:
- name: example
interval: 1s
partial_response_strategy: "WARN"
rules:
- alert: TestAlert_HasBeenLoadedViaWebHandler
# It must be based on actual metric, otherwise call to StoreAPI would be not involved.
expr: absent(some_metric)
labels:
severity: page
annotations:
summary: "I always complain and I have been loaded via /-/reload."
`
testAlertRuleAddedLaterSignal = `
groups:
- name: example
interval: 1s
partial_response_strategy: "WARN"
rules:
- alert: TestAlert_HasBeenLoadedViaWebHandler
# It must be based on actual metric, otherwise call to StoreAPI would be not involved.
expr: absent(some_metric)
labels:
severity: page
annotations:
summary: "I always complain and I have been loaded via sighup signal."
- name: example2
interval: 1s
partial_response_strategy: "WARN"
rules:
- alert: TestAlert_HasBeenLoadedViaWebHandler
# It must be based on actual metric, otherwise call to StoreAPI would be not involved.
expr: absent(some_metric)
labels:
severity: page
annotations:
summary: "I always complain and I have been loaded via sighup signal."
`
testAlertRuleWithLimit = `
groups:
- name: example_with_limit
interval: 1s
partial_response_strategy: "WARN"
limit: 1
rules:
- alert: TestAlert_WithLimit
expr: 'promhttp_metric_handler_requests_total' # It has more than one labels.
labels:
severity: page
annotations:
summary: "with limit"
`
testRuleRecordAbsentMetric = `
groups:
- name: example_record_rules
interval: 1s
rules:
- record: test_absent_metric
expr: absent(nonexistent{job='thanos-receive'})
`
testAlertRuleHoldDuration = `
groups:
- name: example_rule_hold_duration
interval: 1s
rules:
- alert: TestAlert_RuleHoldDuration
# It must be based on actual metric, otherwise call to StoreAPI would be not involved.
expr: absent(some_metric)
for: 2s
labels:
severity: page
annotations:
summary: "I always complain and allow partial response in query."
`
testAlertRuleKeepFiringFor = `
groups:
- name: example_rule_keep_firing_for
interval: 1s
rules:
- alert: TestAlert_KeepFiringFor
expr: absent(metric_keep_firing_for)
for: 2s
keep_firing_for: 10m
labels:
severity: page
`
testRecordingRuleKeepFiringFor = `
groups:
- name: recording_rule_keep_firing_for
interval: 1s
rules:
- record: metric_keep_firing_for
expr: 1
`
amTimeout = model.Duration(10 * time.Second)
)
type rulesResp struct {
Status string
Data *rulespb.RuleGroups
}
func createRuleFile(t *testing.T, path, content string) {
t.Helper()
err := os.WriteFile(path, []byte(content), 0666)
testutil.Ok(t, err)
}
func createRuleFiles(t *testing.T, dir string) {
t.Helper()
for i, rule := range []string{testAlertRuleAbortOnPartialResponse, testAlertRuleWarnOnPartialResponse} {
createRuleFile(t, filepath.Join(dir, fmt.Sprintf("rules-%d.yaml", i)), rule)
}
}
func reloadRulesHTTP(t *testing.T, ctx context.Context, endpoint string) {
req, err := http.NewRequestWithContext(ctx, "POST", "http://"+endpoint+"/-/reload", io.NopCloser(bytes.NewReader(nil)))
testutil.Ok(t, err)
resp, err := http.DefaultClient.Do(req)
testutil.Ok(t, err)
defer resp.Body.Close()
testutil.Equals(t, 200, resp.StatusCode)
}
func reloadRulesSignal(t *testing.T, r *e2eobs.Observable) {
c := e2e.NewCommand("kill", "-1", "1")
testutil.Ok(t, r.Exec(c))
}
func checkReloadSuccessful(t *testing.T, ctx context.Context, endpoint string, expectedRulegroupCount int) {
data := rulesResp{}
errCount := 0
testutil.Ok(t, runutil.Retry(5*time.Second, ctx.Done(), func() error {
req, err := http.NewRequestWithContext(ctx, "GET", "http://"+endpoint+"/api/v1/rules", io.NopCloser(bytes.NewReader(nil)))
if err != nil {
errCount++
return err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
errCount++
return err
}
if resp.StatusCode != 200 {
errCount++
return errors.Newf("statuscode is not 200, got %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
errCount++
return errors.Wrapf(err, "error reading body")
}
if err := resp.Body.Close(); err != nil {
errCount++
return err
}
if err := json.Unmarshal(body, &data); err != nil {
errCount++
return errors.Wrapf(err, "error unmarshaling body")
}
if data.Status != "success" {
errCount++
return errors.Newf("response status is not success, got %s", data.Status)
}
if len(data.Data.Groups) == expectedRulegroupCount {
return nil
}
errCount++
return errors.Newf("different number of rulegroups: expected %d, got %d", expectedRulegroupCount, len(data.Data.Groups))
}))
testutil.Assert(t, len(data.Data.Groups) == expectedRulegroupCount, fmt.Sprintf("expected there to be %d rule groups but got %d. encountered %d errors", expectedRulegroupCount, len(data.Data.Groups), errCount))
}
func rulegroupCorrectData(t *testing.T, ctx context.Context, endpoint string) {
req, err := http.NewRequestWithContext(ctx, "GET", "http://"+endpoint+"/api/v1/rules", io.NopCloser(bytes.NewReader(nil)))
testutil.Ok(t, err)
resp, err := http.DefaultClient.Do(req)
testutil.Ok(t, err)
testutil.Equals(t, 200, resp.StatusCode)
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
testutil.Ok(t, err)
var data = rulesResp{}
testutil.Ok(t, json.Unmarshal(body, &data))
testutil.Equals(t, "success", data.Status)
testutil.Assert(t, len(data.Data.Groups) > 0, "expected there to be some rule groups")
for _, g := range data.Data.Groups {
testutil.Assert(t, g.EvaluationDurationSeconds > 0, "expected it to take more than zero seconds to evaluate")
testutil.Assert(t, !g.LastEvaluation.IsZero(), "expected the rule group to be evaluated at least once")
}
}
func writeTargets(t *testing.T, path string, addrs ...string) {
t.Helper()
var tgs []model.LabelSet
for _, a := range addrs {
tgs = append(
tgs,
model.LabelSet{
model.AddressLabel: model.LabelValue(a),
},
)
}
b, err := yaml.Marshal([]*targetgroup.Group{{Targets: tgs}})
testutil.Ok(t, err)
testutil.Ok(t, os.WriteFile(path+".tmp", b, 0660))
testutil.Ok(t, os.Rename(path+".tmp", path))
}
func TestRule(t *testing.T) {
t.Parallel()
e, err := e2e.NewDockerEnvironment("e2e-test-rule")
testutil.Ok(t, err)
t.Cleanup(e2ethanos.CleanScenario(t, e))
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
t.Cleanup(cancel)
am1 := e2ethanos.NewAlertmanager(e, "1")
am2 := e2ethanos.NewAlertmanager(e, "2")
testutil.Ok(t, e2e.StartAndWaitReady(am1, am2))
rFuture := e2ethanos.NewRulerBuilder(e, "1")
amTargetsSubDir := filepath.Join("rules_am_targets")
testutil.Ok(t, os.MkdirAll(filepath.Join(rFuture.Dir(), amTargetsSubDir), os.ModePerm))
queryTargetsSubDir := filepath.Join("rules_query_targets")
testutil.Ok(t, os.MkdirAll(filepath.Join(rFuture.Dir(), queryTargetsSubDir), os.ModePerm))
rulesSubDir := filepath.Join("rules")
rulesPath := filepath.Join(rFuture.Dir(), rulesSubDir)
testutil.Ok(t, os.MkdirAll(rulesPath, os.ModePerm))
createRuleFiles(t, rulesPath)
r := rFuture.WithAlertManagerConfig([]alert.AlertmanagerConfig{
{
EndpointsConfig: clientconfig.HTTPEndpointsConfig{
FileSDConfigs: []clientconfig.HTTPFileSDConfig{
{
// FileSD which will be used to register discover dynamically am1.
Files: []string{filepath.Join(rFuture.InternalDir(), amTargetsSubDir, "*.yaml")},
RefreshInterval: model.Duration(time.Second),
},
},
StaticAddresses: []string{
am2.InternalEndpoint("http"),
},
Scheme: "http",
},
Timeout: amTimeout,
APIVersion: alert.APIv1,
},
}).InitTSDB(filepath.Join(rFuture.InternalDir(), rulesSubDir), []clientconfig.Config{
{
HTTPConfig: clientconfig.HTTPConfig{
EndpointsConfig: clientconfig.HTTPEndpointsConfig{
// We test Statically Addressed queries in other tests. Focus on FileSD here.
FileSDConfigs: []clientconfig.HTTPFileSDConfig{
{
// FileSD which will be used to register discover dynamically q.
Files: []string{filepath.Join(rFuture.InternalDir(), queryTargetsSubDir, "*.yaml")},
RefreshInterval: model.Duration(time.Second),
},
},
Scheme: "http",
},
},
},
})
testutil.Ok(t, e2e.StartAndWaitReady(r))
q := e2ethanos.NewQuerierBuilder(e, "1", r.InternalEndpoint("grpc")).Init()
testutil.Ok(t, e2e.StartAndWaitReady(q))
t.Run("no query configured", func(t *testing.T) {
// Check for a few evaluations, check all of them failed.
testutil.Ok(t, r.WaitSumMetrics(e2emon.Greater(10), "prometheus_rule_evaluations_total"))
testutil.Ok(t, r.WaitSumMetrics(e2emon.EqualsAmongTwo, "prometheus_rule_evaluations_total", "prometheus_rule_evaluation_failures_total"))
// No alerts sent.
testutil.Ok(t, r.WaitSumMetrics(e2emon.Equals(0), "thanos_alert_sender_alerts_dropped_total"))
})
var currentFailures float64
t.Run("attach query", func(t *testing.T) {
// Attach querier to target files.
writeTargets(t, filepath.Join(rFuture.Dir(), queryTargetsSubDir, "targets.yaml"), q.InternalEndpoint("http"))
testutil.Ok(t, r.WaitSumMetricsWithOptions(e2emon.Equals(1), []string{"thanos_rule_query_apis_dns_provider_results"}, e2emon.WaitMissingMetrics()))
testutil.Ok(t, r.WaitSumMetrics(e2emon.Equals(1), "thanos_rule_alertmanagers_dns_provider_results"))
var currentVal float64
testutil.Ok(t, r.WaitSumMetrics(func(sums ...float64) bool {
currentVal = sums[0]
currentFailures = sums[1]
return true
}, "prometheus_rule_evaluations_total", "prometheus_rule_evaluation_failures_total"))
// Check for a few evaluations, check all of them failed.
testutil.Ok(t, r.WaitSumMetrics(e2emon.Greater(currentVal+4), "prometheus_rule_evaluations_total"))
// No failures.
testutil.Ok(t, r.WaitSumMetrics(e2emon.Equals(currentFailures), "prometheus_rule_evaluation_failures_total"))
// Alerts sent.
testutil.Ok(t, r.WaitSumMetrics(e2emon.Equals(0), "thanos_alert_sender_alerts_dropped_total"))
testutil.Ok(t, r.WaitSumMetrics(e2emon.Greater(4), "thanos_alert_sender_alerts_sent_total"))
// Alerts received.
testutil.Ok(t, am2.WaitSumMetrics(e2emon.Equals(2), "alertmanager_alerts"))
testutil.Ok(t, am2.WaitSumMetrics(e2emon.Greater(4), "alertmanager_alerts_received_total"))
testutil.Ok(t, am2.WaitSumMetrics(e2emon.Equals(0), "alertmanager_alerts_invalid_total"))
// am1 not connected, so should not receive anything.
testutil.Ok(t, am1.WaitSumMetrics(e2emon.Equals(0), "alertmanager_alerts"))
testutil.Ok(t, am1.WaitSumMetrics(e2emon.Equals(0), "alertmanager_alerts_received_total"))
testutil.Ok(t, am1.WaitSumMetrics(e2emon.Equals(0), "alertmanager_alerts_invalid_total"))
})
t.Run("attach am1", func(t *testing.T) {
// Attach am1 to target files.
writeTargets(t, filepath.Join(rFuture.Dir(), amTargetsSubDir, "targets.yaml"), am1.InternalEndpoint("http"))
testutil.Ok(t, r.WaitSumMetrics(e2emon.Equals(1), "thanos_rule_query_apis_dns_provider_results"))
testutil.Ok(t, r.WaitSumMetrics(e2emon.Equals(2), "thanos_rule_alertmanagers_dns_provider_results"))
testutil.Ok(t, r.WaitSumMetrics(e2emon.Equals(currentFailures), "prometheus_rule_evaluation_failures_total"))
var currentVal float64
testutil.Ok(t, am2.WaitSumMetrics(func(sums ...float64) bool {
currentVal = sums[0]
return true
}, "alertmanager_alerts_received_total"))
// Alerts received by both am1 and am2.
testutil.Ok(t, am2.WaitSumMetrics(e2emon.Equals(2), "alertmanager_alerts"))
testutil.Ok(t, am2.WaitSumMetrics(e2emon.Greater(currentVal+4), "alertmanager_alerts_received_total"))
testutil.Ok(t, am2.WaitSumMetrics(e2emon.Equals(0), "alertmanager_alerts_invalid_total"))
testutil.Ok(t, am1.WaitSumMetrics(e2emon.Equals(2), "alertmanager_alerts"))
testutil.Ok(t, am1.WaitSumMetrics(e2emon.Greater(4), "alertmanager_alerts_received_total"))
testutil.Ok(t, am1.WaitSumMetrics(e2emon.Equals(0), "alertmanager_alerts_invalid_total"))
})
t.Run("am1 drops again", func(t *testing.T) {
testutil.Ok(t, os.RemoveAll(filepath.Join(rFuture.Dir(), amTargetsSubDir, "targets.yaml")))
testutil.Ok(t, r.WaitSumMetrics(e2emon.Equals(1), "thanos_rule_query_apis_dns_provider_results"))
testutil.Ok(t, r.WaitSumMetrics(e2emon.Equals(1), "thanos_rule_alertmanagers_dns_provider_results"))
testutil.Ok(t, r.WaitSumMetrics(e2emon.Equals(currentFailures), "prometheus_rule_evaluation_failures_total"))
var currentValAm1 float64
testutil.Ok(t, am1.WaitSumMetrics(func(sums ...float64) bool {
currentValAm1 = sums[0]
return true
}, "alertmanager_alerts_received_total"))
var currentValAm2 float64
testutil.Ok(t, am2.WaitSumMetrics(func(sums ...float64) bool {
currentValAm2 = sums[0]
return true
}, "alertmanager_alerts_received_total"))
// Alerts received by both am1 and am2.
testutil.Ok(t, am2.WaitSumMetrics(e2emon.Equals(2), "alertmanager_alerts"))
testutil.Ok(t, am2.WaitSumMetrics(e2emon.Greater(currentValAm2+4), "alertmanager_alerts_received_total"))
testutil.Ok(t, am2.WaitSumMetrics(e2emon.Equals(0), "alertmanager_alerts_invalid_total"))
// Am1 should not receive more alerts.
testutil.Ok(t, am1.WaitSumMetrics(e2emon.Equals(currentValAm1), "alertmanager_alerts_received_total"))
})
t.Run("duplicate am", func(t *testing.T) {
// am2 is already registered in static addresses.
writeTargets(t, filepath.Join(rFuture.Dir(), amTargetsSubDir, "targets.yaml"), am2.InternalEndpoint("http"))
testutil.Ok(t, r.WaitSumMetrics(e2emon.Equals(1), "thanos_rule_query_apis_dns_provider_results"))
testutil.Ok(t, r.WaitSumMetrics(e2emon.Equals(1), "thanos_rule_alertmanagers_dns_provider_results"))
})
t.Run("rule groups have last evaluation and evaluation duration set", func(t *testing.T) {
rulegroupCorrectData(t, ctx, r.Endpoint("http"))
})
t.Run("signal reload works", func(t *testing.T) {
// Add a new rule via sending sighup
createRuleFile(t, filepath.Join(rulesPath, "newrule.yaml"), testAlertRuleAddedLaterSignal)
reloadRulesSignal(t, r)
checkReloadSuccessful(t, ctx, r.Endpoint("http"), 4)
})
t.Run("http reload works", func(t *testing.T) {
// Add a new rule via /-/reload.
createRuleFile(t, filepath.Join(rulesPath, "newrule.yaml"), testAlertRuleAddedLaterWebHandler)
reloadRulesHTTP(t, ctx, r.Endpoint("http"))
checkReloadSuccessful(t, ctx, r.Endpoint("http"), 3)
})
t.Run("query alerts", func(t *testing.T) {
queryAndAssertSeries(t, ctx, q.Endpoint("http"), func() string { return "ALERTS" }, time.Now, promclient.QueryOptions{
Deduplicate: false,
}, []model.Metric{
{
"__name__": "ALERTS",
"severity": "page",
"alertname": "TestAlert_AbortOnPartialResponse",
"alertstate": "firing",
"replica": "1",
},
{
"__name__": "ALERTS",
"severity": "page",
"alertname": "TestAlert_HasBeenLoadedViaWebHandler",
"alertstate": "firing",
"replica": "1",
},
{
"__name__": "ALERTS",
"severity": "page",
"alertname": "TestAlert_WarnOnPartialResponse",
"alertstate": "firing",
"replica": "1",
},
})
expAlertLabels := []model.LabelSet{
{
"severity": "page",
"alertname": "TestAlert_AbortOnPartialResponse",
"replica": "1",
},
{
"severity": "page",
"alertname": "TestAlert_HasBeenLoadedViaWebHandler",
"replica": "1",
},
{
"severity": "page",
"alertname": "TestAlert_WarnOnPartialResponse",
"replica": "1",
},
}
alrts, err := promclient.NewDefaultClient().AlertmanagerAlerts(ctx, urlParse(t, "http://"+am2.Endpoint("http")))
testutil.Ok(t, err)
testutil.Equals(t, len(expAlertLabels), len(alrts))
for i, a := range alrts {
testutil.Assert(t, a.Labels.Equal(expAlertLabels[i]), "unexpected labels %s", a.Labels)
}
})
}
func TestRule_KeepFiringFor(t *testing.T) {
t.Parallel()
e, err := e2e.NewDockerEnvironment("e2eKeepFiringFor")
testutil.Ok(t, err)
t.Cleanup(e2ethanos.CleanScenario(t, e))
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
t.Cleanup(cancel)
rFuture := e2ethanos.NewRulerBuilder(e, "1")
queryTargetsSubDir := filepath.Join("rules_query_targets")
testutil.Ok(t, os.MkdirAll(filepath.Join(rFuture.Dir(), queryTargetsSubDir), os.ModePerm))
rulesSubDir := filepath.Join("rules")
rulesPath := filepath.Join(rFuture.Dir(), rulesSubDir)
testutil.Ok(t, os.MkdirAll(rulesPath, os.ModePerm))
createRuleFile(t, filepath.Join(rulesPath, "alert_keep_firing_for.yaml"), testAlertRuleKeepFiringFor)
r := rFuture.InitTSDB(filepath.Join(rFuture.InternalDir(), rulesSubDir), []clientconfig.Config{
{
HTTPConfig: clientconfig.HTTPConfig{
EndpointsConfig: clientconfig.HTTPEndpointsConfig{
// We test Statically Addressed queries in other tests. Focus on FileSD here.
FileSDConfigs: []clientconfig.HTTPFileSDConfig{
{
// FileSD which will be used to register discover dynamically q.
Files: []string{filepath.Join(rFuture.InternalDir(), queryTargetsSubDir, "*.yaml")},
RefreshInterval: model.Duration(time.Second),
},
},
Scheme: "http",
},
},
},
})
testutil.Ok(t, e2e.StartAndWaitReady(r))
q := e2ethanos.NewQuerierBuilder(e, "1", r.InternalEndpoint("grpc")).Init()
testutil.Ok(t, e2e.StartAndWaitReady(q))
// Attach querier to target files.
writeTargets(t, filepath.Join(rFuture.Dir(), queryTargetsSubDir, "targets.yaml"), q.InternalEndpoint("http"))
t.Run("check keep_firing_for alert is triggering", func(t *testing.T) {
queryAndAssertSeries(t, ctx, q.Endpoint("http"), func() string { return "ALERTS" }, time.Now, promclient.QueryOptions{
Deduplicate: false,
}, []model.Metric{
{
"__name__": "ALERTS",
"severity": "page",
"alertname": "TestAlert_KeepFiringFor",
"alertstate": "firing",
"replica": "1",
},
})
})
t.Run("resolve keep_firing_for condition", func(t *testing.T) {
// Create a recording rule that will add the missing metric
createRuleFile(t, filepath.Join(rulesPath, "record_metric_keep_firing_for.yaml"), testRecordingRuleKeepFiringFor)
reloadRulesHTTP(t, ctx, r.Endpoint("http"))
// Wait for metric to pop up
queryWaitAndAssert(t, ctx, q.Endpoint("http"), func() string { return "metric_keep_firing_for" }, time.Now, promclient.QueryOptions{
Deduplicate: false,
}, model.Vector{
&model.Sample{
Metric: model.Metric{
"__name__": "metric_keep_firing_for",
"replica": "1",
},
Value: model.SampleValue(1),
},
})
})
t.Run("keep_firing_for should continue triggering", func(t *testing.T) {
// Alert should still be firing
queryAndAssertSeries(t, ctx, q.Endpoint("http"), func() string { return "ALERTS" }, time.Now, promclient.QueryOptions{
Deduplicate: false,
}, []model.Metric{
{
"__name__": "ALERTS",
"severity": "page",
"alertname": "TestAlert_KeepFiringFor",
"alertstate": "firing",
"replica": "1",
},
})
})
}
// TestRule_CanRemoteWriteData checks that Thanos Ruler can be run in stateless mode
// where it remote_writes rule evaluations to a Prometheus remote-write endpoint (typically
// a Thanos Receiver).
func TestRule_CanRemoteWriteData(t *testing.T) {
t.Parallel()
e, err := e2e.NewDockerEnvironment("rule-rw")
testutil.Ok(t, err)
t.Cleanup(e2ethanos.CleanScenario(t, e))
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
t.Cleanup(cancel)
rFuture := e2ethanos.NewRulerBuilder(e, "1")
rulesSubDir := "rules"
rulesPath := filepath.Join(rFuture.Dir(), rulesSubDir)
testutil.Ok(t, os.MkdirAll(rulesPath, os.ModePerm))
for i, rule := range []string{testRuleRecordAbsentMetric, testAlertRuleWarnOnPartialResponse} {
createRuleFile(t, filepath.Join(rulesPath, fmt.Sprintf("rules-%d.yaml", i)), rule)
}
am := e2ethanos.NewAlertmanager(e, "1")
testutil.Ok(t, e2e.StartAndWaitReady(am))
receiver := e2ethanos.NewReceiveBuilder(e, "1").WithIngestionEnabled().Init()
testutil.Ok(t, e2e.StartAndWaitReady(receiver))
rwURL := urlParse(t, e2ethanos.RemoteWriteEndpoint(receiver.InternalEndpoint("remote-write")))
receiver2 := e2ethanos.NewReceiveBuilder(e, "2").WithIngestionEnabled().Init()
testutil.Ok(t, e2e.StartAndWaitReady(receiver2))
rwURL2 := urlParse(t, e2ethanos.RemoteWriteEndpoint(receiver2.InternalEndpoint("remote-write")))
q := e2ethanos.NewQuerierBuilder(e, "1", receiver.InternalEndpoint("grpc"), receiver2.InternalEndpoint("grpc")).Init()
testutil.Ok(t, e2e.StartAndWaitReady(q))
r := rFuture.WithAlertManagerConfig([]alert.AlertmanagerConfig{
{
EndpointsConfig: clientconfig.HTTPEndpointsConfig{
StaticAddresses: []string{
am.InternalEndpoint("http"),
},
Scheme: "http",
},
Timeout: amTimeout,
APIVersion: alert.APIv1,
},
}).InitStateless(filepath.Join(rFuture.InternalDir(), rulesSubDir), []clientconfig.Config{
{
HTTPConfig: clientconfig.HTTPConfig{
EndpointsConfig: clientconfig.HTTPEndpointsConfig{
StaticAddresses: []string{
q.InternalEndpoint("http"),
},
Scheme: "http",
},
},
},
}, []*config.RemoteWriteConfig{
{URL: &common_cfg.URL{URL: rwURL}, Name: "thanos-receiver"},
{URL: &common_cfg.URL{URL: rwURL2}, Name: "thanos-receiver2"},
})
testutil.Ok(t, e2e.StartAndWaitReady(r))
// Wait until remote write samples are written to receivers successfully.
testutil.Ok(t, r.WaitSumMetricsWithOptions(e2emon.GreaterOrEqual(1), []string{"prometheus_remote_storage_samples_total"}, e2emon.WaitMissingMetrics()))
t.Run("can fetch remote-written samples from receiver", func(t *testing.T) {
testRecordedSamples := func() string { return "test_absent_metric" }
queryAndAssertSeries(t, ctx, q.Endpoint("http"), testRecordedSamples, time.Now, promclient.QueryOptions{
Deduplicate: false,
}, []model.Metric{
{
"__name__": "test_absent_metric",
"job": "thanos-receive",
"receive": model.LabelValue(receiver.Name()),
"replica": "1",
"tenant_id": "default-tenant",
},
{
"__name__": "test_absent_metric",
"job": "thanos-receive",
"receive": model.LabelValue(receiver2.Name()),
"replica": "1",
"tenant_id": "default-tenant",
},
})
})
}
func TestStatelessRulerAlertStateRestore(t *testing.T) {
t.Parallel()
e, err := e2e.NewDockerEnvironment("stateless-state")
testutil.Ok(t, err)
t.Cleanup(e2ethanos.CleanScenario(t, e))
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
t.Cleanup(cancel)
am := e2ethanos.NewAlertmanager(e, "1")
testutil.Ok(t, e2e.StartAndWaitReady(am))
receiver := e2ethanos.NewReceiveBuilder(e, "1").WithIngestionEnabled().Init()
testutil.Ok(t, e2e.StartAndWaitReady(receiver))
rwURL := urlParse(t, e2ethanos.RemoteWriteEndpoint(receiver.InternalEndpoint("remote-write")))
q := e2ethanos.NewQuerierBuilder(e, "1", receiver.InternalEndpoint("grpc")).
WithReplicaLabels("replica", "receive").Init()
testutil.Ok(t, e2e.StartAndWaitReady(q))
rulesSubDir := "rules"
var rulers []*e2eobs.Observable
for i := 1; i <= 2; i++ {
rFuture := e2ethanos.NewRulerBuilder(e, fmt.Sprintf("%d", i))
rulesPath := filepath.Join(rFuture.Dir(), rulesSubDir)
testutil.Ok(t, os.MkdirAll(rulesPath, os.ModePerm))
for i, rule := range []string{testAlertRuleHoldDuration} {
createRuleFile(t, filepath.Join(rulesPath, fmt.Sprintf("rules-%d.yaml", i)), rule)
}
r := rFuture.WithAlertManagerConfig([]alert.AlertmanagerConfig{
{
EndpointsConfig: clientconfig.HTTPEndpointsConfig{
StaticAddresses: []string{
am.InternalEndpoint("http"),
},
Scheme: "http",
},
Timeout: amTimeout,
APIVersion: alert.APIv1,
},
}).WithForGracePeriod("500ms").
WithRestoreIgnoredLabels("tenant_id").
InitStateless(filepath.Join(rFuture.InternalDir(), rulesSubDir), []clientconfig.Config{
{
HTTPConfig: clientconfig.HTTPConfig{
EndpointsConfig: clientconfig.HTTPEndpointsConfig{
StaticAddresses: []string{
q.InternalEndpoint("http"),
},
Scheme: "http",
},
},
},
}, []*config.RemoteWriteConfig{
{URL: &common_cfg.URL{URL: rwURL}, Name: "thanos-receiver"},
})
rulers = append(rulers, r)
}
// Start the ruler 1 first.
testutil.Ok(t, e2e.StartAndWaitReady(rulers[0]))
// Wait until the alert firing and ALERTS_FOR_STATE
// series has been written to receiver successfully.
queryAndAssertSeries(t, ctx, q.Endpoint("http"), func() string {
return "ALERTS_FOR_STATE"
}, time.Now, promclient.QueryOptions{
Deduplicate: true,
}, []model.Metric{
{
"__name__": "ALERTS_FOR_STATE",
"alertname": "TestAlert_RuleHoldDuration",
"severity": "page",
"tenant_id": "default-tenant",
},
})
var alerts []*rulespb.AlertInstance
client := promclient.NewDefaultClient()
err = runutil.Retry(time.Second*1, ctx.Done(), func() error {
alerts, err = client.AlertsInGRPC(ctx, urlParse(t, "http://"+rulers[0].Endpoint("http")))
testutil.Ok(t, err)
if len(alerts) > 0 {
if alerts[0].State == rulespb.AlertState_FIRING {
return nil
}
}
return fmt.Errorf("alert is not firing")
})
testutil.Ok(t, err)
// Record the alert active time.
alertActiveAt := alerts[0].ActiveAt
testutil.Ok(t, rulers[0].Stop())
// Start the ruler 2 now and ruler 2 should be able
// to restore the firing alert state.
testutil.Ok(t, e2e.StartAndWaitReady(rulers[1]))
// Wait for 4 rule evaluation iterations to make sure the alert state is restored.
testutil.Ok(t, rulers[1].WaitSumMetricsWithOptions(e2emon.GreaterOrEqual(4), []string{"prometheus_rule_group_iterations_total"}, e2emon.WaitMissingMetrics()))
// Wait until the alert is firing on the second ruler.
err = runutil.Retry(time.Second*1, ctx.Done(), func() error {
alerts, err = client.AlertsInGRPC(ctx, urlParse(t, "http://"+rulers[1].Endpoint("http")))
testutil.Ok(t, err)
if len(alerts) > 0 {
if alerts[0].State == rulespb.AlertState_FIRING {
// The second ruler alert's active at time is the same as the previous one,
// which means the alert state is restored successfully.
if alertActiveAt.Unix() == alerts[0].ActiveAt.Unix() {
return nil
} else {
return fmt.Errorf("alert active time is not restored")
}
}
}
return fmt.Errorf("alert is not firing")
})
testutil.Ok(t, err)
}
// TestRule_CanPersistWALData checks that in stateless mode, Thanos Ruler can persist rule evaluations
// which couldn't be sent to the remote write endpoint (e.g because receiver isn't available).
func TestRule_CanPersistWALData(t *testing.T) {
//TODO: Implement test with unavailable remote-write endpoint(receiver)
}
// Test Ruler behavior on different storepb.PartialResponseStrategy when having partial response from single `failingStoreAPI`.
func TestRulePartialResponse(t *testing.T) {
t.Skip("TODO: Allow HTTP ports from binaries running on host to be accessible.")
// TODO: Implement with failing store.
}