cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/checks/alerts_comparison_test.go
164lines · modecode
| 1 | package checks_test |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/cloudflare/pint/internal/checks" |
| 7 | "github.com/cloudflare/pint/internal/promapi" |
| 8 | ) |
| 9 | |
| 10 | func newComparisonCheck(_ *promapi.FailoverGroup) checks.RuleChecker { |
| 11 | return checks.NewComparisonCheck() |
| 12 | } |
| 13 | |
| 14 | func TestComparisonCheck(t *testing.T) { |
| 15 | testCases := []checkTest{ |
| 16 | { |
| 17 | description: "ignores recording rules", |
| 18 | content: "- record: foo\n expr: up == 0\n", |
| 19 | checker: newComparisonCheck, |
| 20 | prometheus: noProm, |
| 21 | problems: noProblems, |
| 22 | }, |
| 23 | { |
| 24 | description: "ignores rules with syntax errors", |
| 25 | content: "- alert: Foo Is Down\n expr: sum(\n", |
| 26 | checker: newComparisonCheck, |
| 27 | prometheus: noProm, |
| 28 | problems: noProblems, |
| 29 | }, |
| 30 | { |
| 31 | description: "alert expr with > condition", |
| 32 | content: "- alert: Foo Is Down\n for: 10m\n expr: up{job=\"foo\"} > 0\n", |
| 33 | checker: newComparisonCheck, |
| 34 | prometheus: noProm, |
| 35 | problems: noProblems, |
| 36 | }, |
| 37 | { |
| 38 | description: "alert expr with >= condition", |
| 39 | content: "- alert: Foo Is Down\n for: 10m\n expr: up{job=\"foo\"} >= 1\n", |
| 40 | checker: newComparisonCheck, |
| 41 | prometheus: noProm, |
| 42 | problems: noProblems, |
| 43 | }, |
| 44 | { |
| 45 | description: "alert expr with == condition", |
| 46 | content: "- alert: Foo Is Down\n for: 10m\n expr: up{job=\"foo\"} == 1\n", |
| 47 | checker: newComparisonCheck, |
| 48 | prometheus: noProm, |
| 49 | problems: noProblems, |
| 50 | }, |
| 51 | { |
| 52 | description: "alert expr without any condition", |
| 53 | content: "- alert: Foo Is Down\n expr: up{job=\"foo\"}\n", |
| 54 | checker: newComparisonCheck, |
| 55 | prometheus: noProm, |
| 56 | problems: func(uri string) []checks.Problem { |
| 57 | return []checks.Problem{ |
| 58 | { |
| 59 | Fragment: `up{job="foo"}`, |
| 60 | Lines: []int{2}, |
| 61 | Reporter: checks.ComparisonCheckName, |
| 62 | Text: "alert query doesn't have any condition, it will always fire if the metric exists", |
| 63 | Severity: checks.Warning, |
| 64 | }, |
| 65 | } |
| 66 | }, |
| 67 | }, |
| 68 | { |
| 69 | description: "deep level comparison", |
| 70 | content: ` |
| 71 | - alert: High_UDP_Receive_Errors |
| 72 | expr: quantile_over_time(0.7,(irate(udp_packets_drops[2m]))[10m:2m]) > 200 |
| 73 | AND ON (instance) |
| 74 | (rate(node_netstat_Udp_RcvbufErrors[5m])+rate(node_netstat_Udp6_RcvbufErrors[5m])) > 200 |
| 75 | `, |
| 76 | checker: newComparisonCheck, |
| 77 | prometheus: noProm, |
| 78 | problems: noProblems, |
| 79 | }, |
| 80 | { |
| 81 | description: "deep level without comparison", |
| 82 | content: ` |
| 83 | - alert: High_UDP_Receive_Errors |
| 84 | expr: quantile_over_time(0.7,(irate(udp_packets_drops[2m]))[10m:2m]) |
| 85 | AND ON (instance) |
| 86 | rate(node_netstat_Udp_RcvbufErrors[5m])+rate(node_netstat_Udp6_RcvbufErrors[5m]) |
| 87 | `, |
| 88 | checker: newComparisonCheck, |
| 89 | prometheus: noProm, |
| 90 | problems: func(uri string) []checks.Problem { |
| 91 | return []checks.Problem{ |
| 92 | { |
| 93 | Fragment: `quantile_over_time(0.7,(irate(udp_packets_drops[2m]))[10m:2m]) AND ON (instance) rate(node_netstat_Udp_RcvbufErrors[5m])+rate(node_netstat_Udp6_RcvbufErrors[5m])`, |
| 94 | Lines: []int{3}, |
| 95 | Reporter: checks.ComparisonCheckName, |
| 96 | Text: "alert query doesn't have any condition, it will always fire if the metric exists", |
| 97 | Severity: checks.Warning, |
| 98 | }, |
| 99 | } |
| 100 | }, |
| 101 | }, |
| 102 | { |
| 103 | description: "alert unless condition", |
| 104 | content: "- alert: Foo Is Down\n for: 10m\n expr: foo unless bar\n", |
| 105 | checker: newComparisonCheck, |
| 106 | prometheus: noProm, |
| 107 | problems: noProblems, |
| 108 | }, |
| 109 | { |
| 110 | description: "alert expr with bool", |
| 111 | content: "- alert: Error rate is high\n expr: rate(error_count[5m]) > bool 5\n", |
| 112 | checker: newComparisonCheck, |
| 113 | prometheus: noProm, |
| 114 | problems: func(uri string) []checks.Problem { |
| 115 | return []checks.Problem{ |
| 116 | { |
| 117 | Fragment: "rate(error_count[5m]) > bool 5", |
| 118 | Lines: []int{2}, |
| 119 | Reporter: checks.ComparisonCheckName, |
| 120 | Text: "alert query uses bool modifier for comparison, this means it will always return a result and the alert will always fire", |
| 121 | Severity: checks.Bug, |
| 122 | }, |
| 123 | } |
| 124 | }, |
| 125 | }, |
| 126 | { |
| 127 | description: "alert expr with bool and condition", |
| 128 | content: "- alert: Error rate is high\n expr: rate(error_count[5m]) > bool 5 == 1\n", |
| 129 | checker: newComparisonCheck, |
| 130 | prometheus: noProm, |
| 131 | problems: noProblems, |
| 132 | }, |
| 133 | { |
| 134 | description: "alert on absent", |
| 135 | content: "- alert: Foo Is Missing\n expr: absent(foo)\n", |
| 136 | checker: newComparisonCheck, |
| 137 | prometheus: noProm, |
| 138 | problems: noProblems, |
| 139 | }, |
| 140 | { |
| 141 | description: "absent or absent", |
| 142 | content: "- alert: Foo Is Missing\n expr: absent(foo) or absent(bar)\n", |
| 143 | checker: newComparisonCheck, |
| 144 | prometheus: noProm, |
| 145 | problems: noProblems, |
| 146 | }, |
| 147 | { |
| 148 | description: "absent or absent or absent", |
| 149 | content: "- alert: Foo Is Missing\n expr: absent(foo) or absent(bar) or absent(bob{job=\"xx\"})\n", |
| 150 | checker: newComparisonCheck, |
| 151 | prometheus: noProm, |
| 152 | problems: noProblems, |
| 153 | }, |
| 154 | { |
| 155 | description: "(foo > 1) > bool 1", |
| 156 | content: "- alert: Foo Is Missing\n expr: (foo > 1) > bool 1\n", |
| 157 | checker: newComparisonCheck, |
| 158 | prometheus: noProm, |
| 159 | problems: noProblems, |
| 160 | }, |
| 161 | } |
| 162 | |
| 163 | runTests(t, testCases) |
| 164 | } |
| 165 | |