/
rule_for.go
104 lines (87 loc) · 2.53 KB
/
rule_for.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
package checks
import (
"context"
"fmt"
"time"
"github.com/prometheus/common/model"
"github.com/cloudflare/pint/internal/discovery"
"github.com/cloudflare/pint/internal/output"
"github.com/cloudflare/pint/internal/parser"
)
const (
RuleForCheckName = "rule/for"
)
type RuleForKey string
const (
RuleForFor RuleForKey = "for"
RuleForKeepFiringFor RuleForKey = "keep_firing_for"
)
func NewRuleForCheck(key RuleForKey, minFor, maxFor time.Duration, comment string, severity Severity) RuleForCheck {
return RuleForCheck{
key: key,
minFor: minFor,
maxFor: maxFor,
comment: comment,
severity: severity,
}
}
type RuleForCheck struct {
key RuleForKey
comment string
severity Severity
minFor time.Duration
maxFor time.Duration
}
func (c RuleForCheck) Meta() CheckMeta {
return CheckMeta{
States: []discovery.ChangeType{
discovery.Noop,
discovery.Added,
discovery.Modified,
discovery.Moved,
},
IsOnline: true,
}
}
func (c RuleForCheck) String() string {
return fmt.Sprintf("%s(%s:%s)", RuleForCheckName, output.HumanizeDuration(c.minFor), output.HumanizeDuration(c.maxFor))
}
func (c RuleForCheck) Reporter() string {
return RuleForCheckName
}
func (c RuleForCheck) Check(_ context.Context, _ discovery.Path, rule parser.Rule, _ []discovery.Entry) (problems []Problem) {
if rule.AlertingRule == nil {
return nil
}
var forDur model.Duration
var lines parser.LineRange
switch {
case c.key == RuleForFor && rule.AlertingRule.For != nil:
forDur, _ = model.ParseDuration(rule.AlertingRule.For.Value)
lines = rule.AlertingRule.For.Lines
case c.key == RuleForKeepFiringFor && rule.AlertingRule.KeepFiringFor != nil:
forDur, _ = model.ParseDuration(rule.AlertingRule.KeepFiringFor.Value)
lines = rule.AlertingRule.KeepFiringFor.Lines
default:
lines = rule.AlertingRule.Alert.Lines
}
if time.Duration(forDur) < c.minFor {
problems = append(problems, Problem{
Lines: lines,
Reporter: c.Reporter(),
Text: fmt.Sprintf("This alert rule must have a `%s` field with a minimum duration of %s.", c.key, output.HumanizeDuration(c.minFor)),
Details: maybeComment(c.comment),
Severity: c.severity,
})
}
if c.maxFor > 0 && time.Duration(forDur) > c.maxFor {
problems = append(problems, Problem{
Lines: lines,
Reporter: c.Reporter(),
Text: fmt.Sprintf("This alert rule must have a `%s` field with a maximum duration of %s.", c.key, output.HumanizeDuration(c.maxFor)),
Details: maybeComment(c.comment),
Severity: c.severity,
})
}
return problems
}