@@ -16,21 +16,25 @@ func newAlertsCheck(prom *promapi.FailoverGroup) checks.RuleChecker {
1616
1717func TestAlertsCountCheck (t * testing.T ) {
1818 content := "- alert: Foo Is Down\n expr: up{job=\" foo\" } == 0\n "
19+ now := time .Now ()
1920
2021 testCases := []checkTest {
2122 {
23+ // A recording rule is not checked, so no problems are reported.
2224 description : "ignores recording rules" ,
2325 content : "- record: foo\n expr: up == 0\n " ,
2426 checker : newAlertsCheck ,
2527 prometheus : newSimpleProm ,
2628 },
2729 {
30+ // The expression does not parse, so no problems are reported.
2831 description : "ignores rules with syntax errors" ,
2932 content : "- alert: Foo Is Down\n expr: sum(\n " ,
3033 checker : newAlertsCheck ,
3134 prometheus : newSimpleProm ,
3235 },
3336 {
37+ // The server rejects the query, so the check reports a problem with the error.
3438 description : "bad request" ,
3539 content : content ,
3640 checker : newAlertsCheck ,
@@ -47,6 +51,7 @@ func TestAlertsCountCheck(t *testing.T) {
4751 problems : true ,
4852 },
4953 {
54+ // The optional server is unreachable, so queries report a warning problem.
5055 description : "connection refused / upstream not required / warning" ,
5156 content : content ,
5257 checker : newAlertsCheck ,
@@ -56,7 +61,7 @@ func TestAlertsCountCheck(t *testing.T) {
5661 problems : true ,
5762 },
5863 {
59- description : "empty response" ,
64+ description : "empty response / 0 alerts " ,
6065 content : content ,
6166 checker : newAlertsCheck ,
6267 prometheus : newSimpleProm ,
@@ -72,7 +77,7 @@ func TestAlertsCountCheck(t *testing.T) {
7277 problems : true ,
7378 },
7479 {
75- description : "multiple alerts" ,
80+ description : "multiple alerts / seven ranges for one series / 7 alerts " ,
7681 content : content ,
7782 checker : newAlertsCheck ,
7883 prometheus : newSimpleProm ,
@@ -147,7 +152,7 @@ func TestAlertsCountCheck(t *testing.T) {
147152 problems : true ,
148153 },
149154 {
150- description : "for: 10m" ,
155+ description : "for: 10m / 2 ranges over 10m / 2 alerts " ,
151156 content : "- alert: Foo Is Down\n for: 10m\n expr: up{job=\" foo\" } == 0\n " ,
152157 checker : newAlertsCheck ,
153158 prometheus : newSimpleProm ,
@@ -209,7 +214,7 @@ func TestAlertsCountCheck(t *testing.T) {
209214 problems : true ,
210215 },
211216 {
212- description : "minCount=2" ,
217+ description : "minCount=2 / 2 alerts reported " ,
213218 content : "- alert: Foo Is Down\n for: 10m\n expr: up{job=\" foo\" } == 0\n " ,
214219 checker : func (prom * promapi.FailoverGroup ) checks.RuleChecker {
215220 return checks .NewAlertsCheck (prom , time .Hour * 24 , time .Minute , time .Minute * 5 , 2 , "rule comment" , checks .Information )
@@ -273,7 +278,7 @@ func TestAlertsCountCheck(t *testing.T) {
273278 problems : true ,
274279 },
275280 {
276- description : "minCount=2 severity=bug" ,
281+ description : "minCount=2 severity=bug / 2 alerts reported as bug " ,
277282 content : "- alert: Foo Is Down\n for: 10m\n expr: up{job=\" foo\" } == 0\n " ,
278283 checker : func (prom * promapi.FailoverGroup ) checks.RuleChecker {
279284 return checks .NewAlertsCheck (prom , time .Hour * 24 , time .Minute , time .Minute * 5 , 2 , "" , checks .Bug )
@@ -337,7 +342,7 @@ func TestAlertsCountCheck(t *testing.T) {
337342 problems : true ,
338343 },
339344 {
340- description : "minCount=10" ,
345+ description : "minCount=10 / 2 alerts below minCount, nothing reported " ,
341346 content : "- alert: Foo Is Down\n for: 10m\n expr: up{job=\" foo\" } == 0\n " ,
342347 checker : func (prom * promapi.FailoverGroup ) checks.RuleChecker {
343348 return checks .NewAlertsCheck (prom , time .Hour * 24 , time .Minute , time .Minute * 5 , 10 , "" , checks .Information )
@@ -400,7 +405,7 @@ func TestAlertsCountCheck(t *testing.T) {
400405 },
401406 },
402407 {
403- description : "{__name__=}" ,
408+ description : "{__name__=} / three ranges / 3 alerts " ,
404409 content : `
405410- alert: foo
406411 expr: '{__name__="up", job="foo"} == 0'
@@ -447,7 +452,7 @@ func TestAlertsCountCheck(t *testing.T) {
447452 problems : true ,
448453 },
449454 {
450- description : "{__name__=~}" ,
455+ description : "{__name__=~} / three ranges / 3 alerts " ,
451456 content : `
452457- alert: foo
453458 expr: '{__name__=~"(up|foo)", job="foo"} == 0'
@@ -494,7 +499,7 @@ func TestAlertsCountCheck(t *testing.T) {
494499 problems : true ,
495500 },
496501 {
497- description : "uptime query error" ,
502+ description : "uptime query error / gap detection skipped / 3 alerts " ,
498503 content : "- alert: Foo Is Down\n expr: up{job=\" foo\" } == 0\n " ,
499504 checker : newAlertsCheck ,
500505 prometheus : newSimpleProm ,
@@ -538,7 +543,7 @@ func TestAlertsCountCheck(t *testing.T) {
538543 problems : true ,
539544 },
540545 {
541- description : "keep_firing_for: 10m" ,
546+ description : "keep_firing_for: 10m / 6 ranges 1h apart / 6 alerts " ,
542547 content : "- alert: Foo Is Down\n keep_firing_for: 10m\n expr: up{job=\" foo\" } == 0\n " ,
543548 checker : newAlertsCheck ,
544549 prometheus : newSimpleProm ,
@@ -600,7 +605,7 @@ func TestAlertsCountCheck(t *testing.T) {
600605 problems : true ,
601606 },
602607 {
603- description : "for: 10m + keep_firing_for: 10m" ,
608+ description : "for: 10m + keep_firing_for: 10m / 2 ranges over 10m / 2 alerts " ,
604609 content : "- alert: Foo Is Down\n for: 10m\n keep_firing_for: 10m\n expr: up{job=\" foo\" } == 0\n " ,
605610 checker : newAlertsCheck ,
606611 prometheus : newSimpleProm ,
@@ -661,10 +666,189 @@ func TestAlertsCountCheck(t *testing.T) {
661666 },
662667 problems : true ,
663668 },
669+ {
670+ description : "keep_firing_for=30m merges 15m gaps between ranges / 2 alerts" ,
671+ content : "- alert: Foo Is Down\n keep_firing_for: 30m\n expr: up{job=\" foo\" } == 0\n " ,
672+ checker : newAlertsCheck ,
673+ prometheus : newSimpleProm ,
674+ mocks : []* prometheusMock {
675+ {
676+ conds : []requestCondition {
677+ requireRangeQueryPath ,
678+ formCond {key : "query" , value : `up{job="foo"} == 0` },
679+ },
680+ resp : matrixResponse {
681+ samples : []* model.SampleStream {
682+ generateSampleStream (
683+ map [string ]string {"job" : "foo" },
684+ now .Add (time .Hour * - 20 ),
685+ now .Add (time .Hour * - 20 ).Add (time .Minute * 5 ),
686+ time .Minute ,
687+ ),
688+ generateSampleStream (
689+ map [string ]string {"job" : "foo" },
690+ now .Add (time .Hour * - 20 ).Add (time .Minute * 20 ),
691+ now .Add (time .Hour * - 20 ).Add (time .Minute * 25 ),
692+ time .Minute ,
693+ ),
694+ generateSampleStream (
695+ map [string ]string {"job" : "foo" },
696+ now .Add (time .Hour * - 20 ).Add (time .Minute * 40 ),
697+ now .Add (time .Hour * - 20 ).Add (time .Minute * 45 ),
698+ time .Minute ,
699+ ),
700+ generateSampleStream (
701+ map [string ]string {"job" : "foo" },
702+ now .Add (time .Hour * - 16 ),
703+ now .Add (time .Hour * - 16 ).Add (time .Minute * 5 ),
704+ time .Minute ,
705+ ),
706+ },
707+ },
708+ },
709+ {
710+ conds : []requestCondition {
711+ requireRangeQueryPath ,
712+ formCond {key : "query" , value : "count(\n up\n )" },
713+ },
714+ resp : respondWithSingleRangeVector1D (),
715+ },
716+ },
717+ problems : true ,
718+ },
719+ {
720+ description : "keep_firing_for=2m covers a 2m gap but not wider gaps / 3 alerts" ,
721+ content : "- alert: Foo Is Down\n keep_firing_for: 2m\n expr: up{job=\" foo\" } == 0\n " ,
722+ checker : newAlertsCheck ,
723+ prometheus : newSimpleProm ,
724+ mocks : []* prometheusMock {
725+ {
726+ conds : []requestCondition {
727+ requireRangeQueryPath ,
728+ formCond {key : "query" , value : `up{job="foo"} == 0` },
729+ },
730+ resp : matrixResponse {
731+ samples : []* model.SampleStream {
732+ generateSampleStream (
733+ map [string ]string {"job" : "foo" },
734+ now .Add (time .Hour * - 20 ),
735+ now .Add (time .Hour * - 20 ),
736+ time .Minute ,
737+ ),
738+ generateSampleStream (
739+ map [string ]string {"job" : "foo" },
740+ now .Add (time .Hour * - 20 ).Add (time .Minute * 2 ),
741+ now .Add (time .Hour * - 20 ).Add (time .Minute * 2 ),
742+ time .Minute ,
743+ ),
744+ generateSampleStream (
745+ map [string ]string {"job" : "foo" },
746+ now .Add (time .Hour * - 20 ).Add (time .Minute * 8 ),
747+ now .Add (time .Hour * - 20 ).Add (time .Minute * 8 ),
748+ time .Minute ,
749+ ),
750+ generateSampleStream (
751+ map [string ]string {"job" : "foo" },
752+ now .Add (time .Hour * - 20 ).Add (time .Minute * 12 ),
753+ now .Add (time .Hour * - 20 ).Add (time .Minute * 12 ),
754+ time .Minute ,
755+ ),
756+ },
757+ },
758+ },
759+ {
760+ conds : []requestCondition {
761+ requireRangeQueryPath ,
762+ formCond {key : "query" , value : "count(\n up\n )" },
763+ },
764+ // Prometheus is up the whole window, so any gap is real.
765+ resp : matrixResponse {
766+ samples : []* model.SampleStream {
767+ generateSampleStream (
768+ map [string ]string {},
769+ now .Add (time .Hour * - 24 ),
770+ now ,
771+ time .Minute ,
772+ ),
773+ },
774+ },
775+ },
776+ },
777+ problems : true ,
778+ },
779+ {
780+ // The ranges at +40m and +70m start before keep_firing_for: 30m
781+ // runs out, so foo has one alert. The last 2m range is too short
782+ // for `for`. job=bar fires its own alert.
783+ description : "for=10m + keep_firing_for=30m gives one alert per series / 2 alerts" ,
784+ content : "- alert: Foo Is Down\n for: 10m\n keep_firing_for: 30m\n expr: up{job=\" foo\" } == 0\n " ,
785+ checker : newAlertsCheck ,
786+ prometheus : newSimpleProm ,
787+ mocks : []* prometheusMock {
788+ {
789+ conds : []requestCondition {
790+ requireRangeQueryPath ,
791+ formCond {key : "query" , value : `up{job="foo"} == 0` },
792+ },
793+ resp : matrixResponse {
794+ samples : []* model.SampleStream {
795+ generateSampleStream (
796+ map [string ]string {"job" : "bar" },
797+ now .Add (time .Hour * - 20 ),
798+ now .Add (time .Hour * - 20 ).Add (time .Minute * 15 ),
799+ time .Minute ,
800+ ),
801+ generateSampleStream (
802+ map [string ]string {"job" : "foo" },
803+ now .Add (time .Hour * - 20 ),
804+ now .Add (time .Hour * - 20 ).Add (time .Minute * 15 ),
805+ time .Minute ,
806+ ),
807+ generateSampleStream (
808+ map [string ]string {"job" : "foo" },
809+ now .Add (time .Hour * - 20 ).Add (time .Minute * 40 ),
810+ now .Add (time .Hour * - 20 ).Add (time .Minute * 42 ),
811+ time .Minute ,
812+ ),
813+ generateSampleStream (
814+ map [string ]string {"job" : "foo" },
815+ now .Add (time .Hour * - 20 ).Add (time .Minute * 70 ),
816+ now .Add (time .Hour * - 20 ).Add (time .Minute * 85 ),
817+ time .Minute ,
818+ ),
819+ generateSampleStream (
820+ map [string ]string {"job" : "foo" },
821+ now .Add (time .Hour * - 16 ),
822+ now .Add (time .Hour * - 16 ).Add (time .Minute * 2 ),
823+ time .Minute ,
824+ ),
825+ },
826+ },
827+ },
828+ {
829+ conds : []requestCondition {
830+ requireRangeQueryPath ,
831+ formCond {key : "query" , value : "count(\n up\n )" },
832+ },
833+ // Prometheus is up the whole window, so any gap is real.
834+ resp : matrixResponse {
835+ samples : []* model.SampleStream {
836+ generateSampleStream (
837+ map [string ]string {},
838+ now .Add (time .Hour * - 24 ),
839+ now ,
840+ time .Minute ,
841+ ),
842+ },
843+ },
844+ },
845+ },
846+ problems : true ,
847+ },
664848 {
665849 // Two 7m ranges separated by 3m, Prometheus was down during those 3m.
666850 // Since Prometheus was down there's no gap, so ranges merge into ~17m, exceeding for: 10m.
667- description : "ranges merge when separated by Prometheus downtime " ,
851+ description : "Prometheus down during gap / ranges merge / 1 alert " ,
668852 content : "- alert: Foo Is Down\n for: 10m\n expr: up{job=\" foo\" } == 0\n " ,
669853 checker : newAlertsCheck ,
670854 prometheus : newSimpleProm ,
@@ -724,7 +908,7 @@ func TestAlertsCountCheck(t *testing.T) {
724908 {
725909 // Two 7m ranges but Prometheus was up the entire time.
726910 // There's a gap between them, so they don't merge.
727- description : "ranges stay separate when gap is confirmed by uptime " ,
911+ description : "Prometheus up during gap / ranges stay separate / 0 alerts " ,
728912 content : "- alert: Foo Is Down\n for: 10m\n expr: up{job=\" foo\" } == 0\n " ,
729913 checker : newAlertsCheck ,
730914 prometheus : newSimpleProm ,
0 commit comments