-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathtemplates.go
186 lines (160 loc) · 5.01 KB
/
templates.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
package templates
import (
"fmt"
"strconv"
"time"
prometheus "github.com/prometheus/common/model"
"github.com/openshift/route-monitor-operator/pkg/consts/blackboxexporter"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
)
var serviceMonitorPeriod string = "30s"
// TemplateForServiceMonitorResource returns a ServiceMonitor
func TemplateForServiceMonitorResource(url, blackBoxExporterNamespace string, namespacedName types.NamespacedName) monitoringv1.ServiceMonitor {
routeURL := url
routeMonitorLabels := blackboxexporter.GenerateBlackBoxExporterLables()
labelSelector := metav1.LabelSelector{MatchLabels: routeMonitorLabels}
// Currently we only support `http_2xx` as module
// Still make it a variable so we can easily add functionality later
modules := []string{"http_2xx"}
params := map[string][]string{
"module": modules,
"target": {routeURL},
}
serviceMonitor := monitoringv1.ServiceMonitor{
ObjectMeta: metav1.ObjectMeta{
Name: namespacedName.Name,
Namespace: namespacedName.Namespace,
},
Spec: monitoringv1.ServiceMonitorSpec{
Endpoints: []monitoringv1.Endpoint{
{
Port: blackboxexporter.BlackBoxExporterPortName,
// Probe every 30s
Interval: serviceMonitorPeriod,
// Timeout has to be smaller than probe interval
ScrapeTimeout: "15s",
Path: "/probe",
Scheme: "http",
Params: params,
MetricRelabelConfigs: []*monitoringv1.RelabelConfig{
{
Replacement: routeURL,
TargetLabel: "RouteMonitorUrl",
},
},
}},
Selector: labelSelector,
NamespaceSelector: monitoringv1.NamespaceSelector{
MatchNames: []string{
blackBoxExporterNamespace,
},
},
},
}
return serviceMonitor
}
type multiWindowMultiBurnAlertRule struct {
duration string
severity string
longWindow string
shortWindow string
burnRate string
}
func alertThreshold(windowSize, percent, label, burnRate string) string {
rule := "1-(sum(sum_over_time(probe_success{" + label + "}[" + windowSize + "]))" +
"/ sum(count_over_time(probe_success{" + label + "}[" + windowSize + "])))" +
"> (" + burnRate + "*(1-" + percent + "))"
return rule
}
func sufficientProbes(windowSize, label string) string {
window, _ := prometheus.ParseDuration(windowSize)
window_duration := time.Duration(window)
mPeriod, _ := prometheus.ParseDuration(serviceMonitorPeriod)
mPeriod_duration := time.Duration(mPeriod)
necessaryProbesInWindow := int(window_duration.Minutes()/mPeriod_duration.Minutes() * 0.5)
rule := "sum(count_over_time(probe_success{" + label + "}[" + windowSize + "]))" +
" > " + strconv.Itoa(necessaryProbesInWindow)
return rule
}
//render creates a monitoring rule for the defined multiwindow multi-burn rate alert
func (r *multiWindowMultiBurnAlertRule) render(url, percent, label, alertName string) monitoringv1.Rule {
alertString := "" +
alertThreshold(r.shortWindow, percent, label, r.burnRate) +
" and " +
sufficientProbes(r.shortWindow, label) +
"\nand\n" +
alertThreshold(r.longWindow, percent, label, r.burnRate) +
" and " +
sufficientProbes(r.longWindow, label)
return monitoringv1.Rule{
Alert: alertName,
Expr: intstr.FromString(alertString),
Labels: sampleTemplateLabelsWithSev(url, r.severity),
Annotations: map[string]string{
"message": fmt.Sprintf("High error budget burn for %s (current value: {{ $value }})", label),
},
For: r.duration,
}
}
// TemplateForPrometheusRuleResource returns a PrometheusRule
func TemplateForPrometheusRuleResource(url, percent string, namespacedName types.NamespacedName) monitoringv1.PrometheusRule {
routeURLLabel := fmt.Sprintf(`RouteMonitorUrl="%s"`, url)
rules := []monitoringv1.Rule{}
alertRules := []multiWindowMultiBurnAlertRule{
{
duration: "2m",
severity: "critical",
longWindow: "1h",
shortWindow: "5m",
burnRate: "14.40",
},
{
duration: "15m",
severity: "critical",
longWindow: "6h",
shortWindow: "30m",
burnRate: "6",
},
{
duration: "1h",
severity: "warning",
longWindow: "1d",
shortWindow: "2h",
burnRate: "3",
},
{
duration: "3h",
severity: "warning",
longWindow: "3d",
shortWindow: "6h",
burnRate: "1",
},
}
for _, alertrule := range alertRules { // Create all the alerts
rules = append(rules, alertrule.render(url, percent, routeURLLabel, namespacedName.Name+"-ErrorBudgetBurn"))
}
resource := monitoringv1.PrometheusRule{
ObjectMeta: metav1.ObjectMeta{
Name: namespacedName.Name,
Namespace: namespacedName.Namespace,
},
Spec: monitoringv1.PrometheusRuleSpec{
Groups: []monitoringv1.RuleGroup{
{
Name: "SLOs-probe",
Rules: rules,
},
},
},
}
return resource
}
func sampleTemplateLabelsWithSev(url, severity string) map[string]string {
return map[string]string{
"severity": severity,
"RouteMonitorUrl": url,
}
}