-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathplugin.go
73 lines (60 loc) · 1.92 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
73
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/istio/v1/availability"
)
var queryTpl = template.Must(template.New("").Option("missingkey=error").Parse(`
(
sum(rate(istio_requests_total{ {{.filter}}destination_service_name="{{.service}}",destination_service_namespace="{{.namespace}}",response_code=~"(5..|429)" }[{{"{{.window}}"}}]))
/
(sum(rate(istio_requests_total{ {{.filter}}destination_service_name="{{.service}}",destination_service_namespace="{{.namespace}}" }[{{"{{.window}}"}}])) > 0)
) OR on() vector(0)
`))
// SLIPlugin will return a query that will return the availability error based on istio V1 request metrics.
func SLIPlugin(ctx context.Context, meta, labels, options map[string]string) (string, error) {
service, err := getRequiredStringValue("service", options)
if err != nil {
return "", fmt.Errorf("could not get service: %w", err)
}
namespace, err := getRequiredStringValue("namespace", options)
if err != nil {
return "", fmt.Errorf("could not get namespace: %w", err)
}
// Create query.
var b bytes.Buffer
data := map[string]string{
"filter": getFilter(options),
"service": service,
"namespace": namespace,
}
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 getRequiredStringValue(key string, options map[string]string) (string, error) {
value := options[key]
value = strings.TrimSpace(value)
if value == "" {
return "", fmt.Errorf("%s is required", key)
}
return value, nil
}