-
Notifications
You must be signed in to change notification settings - Fork 53
/
def.go
325 lines (285 loc) · 8.71 KB
/
def.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package query
/*
This module defines the datatypes & interfaces used to define SLO queries.
`AvailableQueries` contains the list of all preconfigured queries.
Queries used by SLOs must follow a format like :
totalQueryTempl = template.Must(template.New("").Parse(`
sum(rate({{.MetricId}}{job="{{.JobId}}"}[{{"{{.window}}"}}]))
`))
goodQueryTempl = template.Must(template.New("").Parse(`
sum(rate({{.MetricId}}{job="{{.JobId}}", {{.Filter}}}[{{"{{.window}}"}}]))
`))
Must :
1. Include a nested template with a {{.window}} for SLOs to fill in
2. the templates must only include information that can be filled with the `templateExecutor` struct.
Note: templates are intended to be filled with *api.Service protobuf definitions, so expect only that information will
be available, when SLOs are created at runtime
*/
import (
"net/http"
"regexp"
"strings"
"text/template"
"github.com/prometheus/client_golang/prometheus"
"github.com/rancher/opni/pkg/slo/shared"
api "github.com/rancher/opni/plugins/slo/pkg/apis/slo"
)
var (
AvailableQueries = make(map[string]MetricQuery)
GetDownstreamMetricQueryTempl = template.Must(template.New("").Parse(`
group by(__name__)({__name__=~"{{.NameRegex}}"})
`))
)
type templateExecutor struct {
MetricIdGood string
MetricIdTotal string
JobId string
}
// must not contain spaces
const MockTestServerName = "MyServer"
func init() {
// Names should be unique for each pre-configured query, as they are used as keys
// in the map
//FIXME: doesn't turn into a prometheus range
uptimeSLOQuery := New().
Name("uptime").
GoodQuery(
NewQueryBuilder().
Query(`
(sum(rate({{.MetricIdGood}}{job="{{.JobId}}"} == 1)))[{{"{{.window}}"}}]
`).
MetricFilter(`up`).
BuildRatio()).
TotalQuery(
NewQueryBuilder().
Query(`
(sum(rate({{.MetricIdTotal}}{job="{{.JobId}}"})))[{{"{{.window}}"}}]
`).
MetricFilter(`up`).BuildRatio()).
Collector(uptimeCollector).
GoodEventGenerator(uptimeGoodEvents).
BadEventGenerator(uptimeBadEvents).
Description("Measures the uptime of a kubernetes service").
Datasource(shared.MonitoringDatasource).Build()
AvailableQueries[uptimeSLOQuery.name] = &uptimeSLOQuery
httpAvailabilitySLOQuery := New().
Name("http-availability").
GoodQuery(
NewQueryBuilder().
Query(`
(sum(rate({{.MetricIdGood}}{job="{{.JobId}}",code=~"(2..|3..)"}[{{"{{.window}}"}}])))
`).
MetricFilter(`.*http_request_duration_seconds_count`).
BuildRatio()).
TotalQuery(
NewQueryBuilder().
Query(`
(sum(rate({{.MetricIdTotal}}{job="{{.JobId}}"}[{{"{{.window}}"}}])))
`).
MetricFilter(`.*http_request_duration_seconds_count`).BuildRatio()).
Collector(availabilityCollector).
GoodEventGenerator(availabilityGoodEvents).
BadEventGenerator(availabilityBadEvents).
Description(`Measures the availability of a kubernetes service using http status codes.
Codes 2XX and 3XX are considered as available.`).
Datasource(shared.MonitoringDatasource).Build()
AvailableQueries[httpAvailabilitySLOQuery.name] = &httpAvailabilitySLOQuery
httpResponseTimeSLOQuery := New().
Name("http-latency").
GoodQuery(
NewQueryBuilder().
Query(`
sum(rate({{.MetricIdGood}}{job="{{.JobId}},"le="0.3",verb!="WATCH"}[{{"{{.window}}"}}]))
`).
MetricFilter(`.*http_request_duration_seconds_bucket`).
BuildHistogram()).
TotalQuery(
NewQueryBuilder().
Query(`
sum(rate({{.MetricIdTotal}}{job="{{.JobId}}",verb!="WATCH"}[{{"{{.window}}"}}]))
`).
MetricFilter(`.*http_request_duration_seconds_count`).BuildRatio()).
Collector(latencyCollector).
GoodEventGenerator(latencyGoodEvents).
BadEventGenerator(latencyBadEvents).
Description(`Quantifies the latency of http requests made against a kubernetes service
by classifying them as good (<=300ms) or bad(>=300ms)`).
Datasource(shared.MonitoringDatasource).Build()
AvailableQueries[httpResponseTimeSLOQuery.name] = &httpResponseTimeSLOQuery
}
type matcher func([]string) string
type SLOQueryResult struct {
GoodQuery string
TotalQuery string
}
type MetricQuery interface {
// User facing name of the pre-confured metric
Name() string
// User-facing description of the pre-configured metric
Description() string
// Each metric has a unique opni datasource (monitoring vs logging) by which it is filtered by
Datasource() string
Construct(*api.ServiceInfo) (*SLOQueryResult, error)
// Some metrics will have different labels for metrics, so handle them independently
GetGoodQuery() Query
GetTotalQuery() Query
// Auto-instrumentation server methods
GetCollector() prometheus.Collector
GetGoodEventGenerator() func(w http.ResponseWriter, r *http.Request)
GetBadEventGenerator() func(w http.ResponseWriter, r *http.Request)
}
type Query interface {
FillQueryTemplate(info templateExecutor) (string, error)
GetMetricFilter() string
Validate() error
IsRatio() bool
BestMatch([]string) string
IsHistogram() bool
Construct(*api.ServiceInfo) (string, error)
}
type QueryBuilder interface {
Query(string) QueryBuilder
MetricFilter(string) QueryBuilder
Matcher(*matcher) QueryBuilder
BuildRatio() RatioQuery
BuildHistogram() HistogramQuery
}
type queryBuilder struct {
query template.Template
filter regexp.Regexp
matcher matcher
}
func NewQueryBuilder() QueryBuilder {
return queryBuilder{}
}
func (q queryBuilder) Query(query string) QueryBuilder {
tmpl := template.Must(template.New("").Parse(strings.TrimSpace(query)))
q.query = *tmpl
return q
}
func (q queryBuilder) MetricFilter(filter string) QueryBuilder {
regex := regexp.MustCompile(strings.TrimSpace(filter))
q.filter = *regex
return q
}
// defaults to `MatchMinLength`
func (q queryBuilder) Matcher(matcher *matcher) QueryBuilder {
if matcher == nil {
q.matcher = MatchMinLength
} else {
q.matcher = *matcher
}
return q
}
func (q queryBuilder) BuildRatio() RatioQuery {
if q.matcher == nil {
q.matcher = MatchMinLength
}
r := RatioQuery{
query: q.query,
metricFilter: q.filter,
matcher: q.matcher,
}
err := r.Validate()
if err != nil {
panic(err)
}
return r
}
func (q queryBuilder) BuildHistogram() HistogramQuery {
if q.matcher == nil {
q.matcher = MatchMinLength
}
h := HistogramQuery{
query: q.query,
metricFilter: q.filter,
matcher: q.matcher,
}
err := h.Validate()
if err != nil {
panic(err)
}
return h
}
type SloQueryBuilder interface {
Name(name string) SloQueryBuilder
GoodQuery(q Query) SloQueryBuilder
TotalQuery(q Query) SloQueryBuilder
Description(description string) SloQueryBuilder
Datasource(datasource string) SloQueryBuilder
Collector(prometheus.Collector) SloQueryBuilder
GoodEventGenerator(goodEvents func(w http.ResponseWriter, r *http.Request)) SloQueryBuilder
BadEventGenerator(badEvents func(w http.ResponseWriter, r *http.Request)) SloQueryBuilder
Build() PrometheusQueryImpl
}
type sloQueryBuilder struct {
name string
metricFilter regexp.Regexp
goodQuery Query
totalQuery Query
description string
datasource string
collector prometheus.Collector
totalCollector prometheus.Collector
goodEventsGen func(w http.ResponseWriter, r *http.Request)
badEventsGen func(w http.ResponseWriter, r *http.Request)
}
func New() SloQueryBuilder {
return sloQueryBuilder{}
}
func (s sloQueryBuilder) Name(name string) SloQueryBuilder {
s.name = name
return s
}
func (s sloQueryBuilder) GoodQuery(q Query) SloQueryBuilder {
s.goodQuery = q
return s
}
func (s sloQueryBuilder) TotalQuery(q Query) SloQueryBuilder {
s.totalQuery = q
return s
}
func (s sloQueryBuilder) Description(description string) SloQueryBuilder {
s.description = description
return s
}
func (s sloQueryBuilder) Datasource(datasource string) SloQueryBuilder {
s.datasource = datasource
return s
}
func (s sloQueryBuilder) Collector(
collector prometheus.Collector) SloQueryBuilder {
s.collector = collector
return s
}
func (s sloQueryBuilder) GoodEventGenerator(
goodEvents func(w http.ResponseWriter, r *http.Request),
) SloQueryBuilder {
s.goodEventsGen = goodEvents
return s
}
func (s sloQueryBuilder) BadEventGenerator(
badEvents func(w http.ResponseWriter, r *http.Request),
) SloQueryBuilder {
s.badEventsGen = badEvents
return s
}
func (s sloQueryBuilder) Build() PrometheusQueryImpl {
//TODO figure out how to validate at compile time
if s.collector == nil {
panic("auto-instrumentation collectors must be set")
}
if s.goodEventsGen == nil || s.badEventsGen == nil {
panic("auto-instrumentation events must be set")
}
return PrometheusQueryImpl{
name: s.name,
datasource: s.datasource,
description: s.description,
GoodQuery: s.goodQuery,
TotalQuery: s.totalQuery,
collector: s.collector,
goodEventsGen: s.goodEventsGen,
badEventsGen: s.badEventsGen,
}
}