-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.go
More file actions
60 lines (51 loc) · 1.49 KB
/
plugin.go
File metadata and controls
60 lines (51 loc) · 1.49 KB
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
package availability
import (
"bytes"
"context"
"fmt"
"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/haproxy/frontend/availability"
)
var queryTpl = template.Must(template.New("").Option("missingkey=error").Parse(`
sum(rate(haproxy_frontend_http_responses_total{ {{.filterError}}code="5xx" }[{{"{{.window}}"}}]))
/
sum(rate(haproxy_frontend_http_responses_total{ {{.filterTotal}} }[{{"{{.window}}"}}]))
`))
// SLIPlugin will return a query that will return the availability error based on frontend responses
// Counts as an error event the requests that have 5xx status code.
func SLIPlugin(ctx context.Context, meta, labels, options map[string]string) (string, error) {
filter, err := getFilter(options)
if err != nil {
return "", fmt.Errorf("could not get filter: %w", err)
}
filterTotal := filter
filterError := filter
// Create query.
var b bytes.Buffer
data := map[string]string{
"filterError": filterError,
"filterTotal": filterTotal,
}
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, error) {
filter, ok := options["filter"]
if !ok || (ok && filter == "") {
return "", nil
}
filter = strings.Trim(filter, "{},")
if filter != "" {
filter += ","
}
return filter, nil
}