-
Notifications
You must be signed in to change notification settings - Fork 13
/
plugin.go
72 lines (60 loc) · 1.76 KB
/
plugin.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
package availability
import (
"bytes"
"context"
"fmt"
"regexp"
"strings"
"text/template"
)
const (
// SLIPluginVersion is the version of the plugin spec.
SLIPluginVersion = "prometheus/v1"
// SLIPluginID is the registering ID of the plugin.
SLIPluginID = "sloth-common/traefik/v1/availability"
)
var queryTpl = template.Must(template.New("").Option("missingkey=error").Parse(`
(
sum(rate(traefik_backend_requests_total{ {{.filter}}backend=~"{{.backendRegex}}",code=~"(5..|429)" }[{{"{{.window}}"}}]))
/
(sum(rate(traefik_backend_requests_total{ {{.filter}}backend=~"{{.backendRegex}}" }[{{"{{.window}}"}}])) > 0)
) OR on() vector(0)
`))
// SLIPlugin will return a query that will return the availability error based on traefik V1 backend metrics.
func SLIPlugin(ctx context.Context, meta, labels, options map[string]string) (string, error) {
backendRegex, err := getBackendRegex(options)
if err != nil {
return "", fmt.Errorf("could not get backend regex: %w", err)
}
// Create query.
var b bytes.Buffer
data := map[string]string{
"filter": getFilter(options),
"backendRegex": backendRegex,
}
err = queryTpl.Execute(&b, data)
if err != nil {
return "", fmt.Errorf("could not render query template: %w", err)
}
return b.String(), nil
}
func getFilter(options map[string]string) string {
filter := options["filter"]
filter = strings.Trim(filter, "{},")
if filter != "" {
filter += ","
}
return filter
}
func getBackendRegex(options map[string]string) (string, error) {
backend := options["backend_regex"]
backend = strings.TrimSpace(backend)
if backend == "" {
return "", fmt.Errorf("backend is required")
}
_, err := regexp.Compile(backend)
if err != nil {
return "", fmt.Errorf("invalid regex: %w", err)
}
return backend, nil
}