/
alert_structs.go
83 lines (67 loc) · 2.11 KB
/
alert_structs.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
package generation
import (
"fmt"
"strconv"
)
type AlertDefaults struct {
displayPrefix string
team string
severity string
runbookUrlAnnotationName string
}
type AlertRule interface {
properties() map[string]string
alertRuleExpression(metricPrefix string) (string, error)
}
type ZeroToleranceErrorAlertRule struct {
AlertRule
props map[string]string
}
func (r ZeroToleranceErrorAlertRule) properties() map[string]string {
return r.props
}
func (r ZeroToleranceErrorAlertRule) alertRuleExpression(metricPrefix string) (string, error) {
return "sum(rate(" + metricPrefix + "errors{error_type='" + r.props["errorLabel"] + "'}[" + r.props["timeRange"] + "])) > 0", nil
}
type ElevatedErrorRateAlertRule struct {
AlertRule
props map[string]string
}
func (r ElevatedErrorRateAlertRule) properties() map[string]string {
return r.props
}
func (r ElevatedErrorRateAlertRule) alertRuleExpression(metricPrefix string) (string, error) {
unvalidatedRate := r.props["ratePerSecondThreshold"]
_, err := strconv.ParseFloat(unvalidatedRate, 64)
if err != nil {
return "", fmt.Errorf("bad ratePerSecondThreshold: %v", err)
}
return "sum(rate(" + metricPrefix + "errors{error_type='" + r.props["errorLabel"] + "'}[" + r.props["timeRange"] + "])) > " + unvalidatedRate, nil
}
// ====================================================================================
type AlertRulesGroup struct {
Name string
Rules []AlertRuleOutput
}
type AlertRuleOutput struct {
Alert string
Expr string
Duration string
Labels map[string]string
Annotations map[string]string
}
// PrometheusOperatorRulesSpec https://github.com/prometheus-operator/prometheus-operator/blob/master/Documentation/api.md#prometheusrulespec
type PrometheusOperatorRulesSpec struct {
Groups []PrometheusOperatorAlertRulesGroup
}
type PrometheusOperatorAlertRulesGroup struct {
Name string
Rules []PrometheusOperatorAlertRuleOutput
}
type PrometheusOperatorAlertRuleOutput struct {
Alert string
Expr string
For string
Labels map[string]string
Annotations map[string]string
}