forked from thanos-io/thanos
/
rule.go
162 lines (139 loc) · 4.14 KB
/
rule.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
package ui
import (
"fmt"
"html/template"
"math"
"net/http"
"path"
"regexp"
"sort"
"github.com/go-kit/kit/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/route"
"github.com/prometheus/prometheus/rules"
)
type Rule struct {
*BaseUI
flagsMap map[string]string
ruleManager *rules.Manager
queryURL string
}
func NewRuleUI(logger log.Logger, ruleManager *rules.Manager, queryURL string, flagsMap map[string]string) *Rule {
return &Rule{
BaseUI: NewBaseUI(logger, "rule_menu.html", ruleTmplFuncs(queryURL)),
flagsMap: flagsMap,
ruleManager: ruleManager,
queryURL: queryURL,
}
}
func ruleTmplFuncs(queryURL string) template.FuncMap {
return template.FuncMap{
"alertStateToClass": func(as rules.AlertState) string {
switch as {
case rules.StateInactive:
return "success"
case rules.StatePending:
return "warning"
case rules.StateFiring:
return "danger"
default:
panic("unknown alert state")
}
},
"queryURL": func() string { return queryURL },
"reReplaceAll": func(pattern, repl, text string) string {
re := regexp.MustCompile(pattern)
return re.ReplaceAllString(text, repl)
},
"humanizeDuration": func(v float64) string {
if math.IsNaN(v) || math.IsInf(v, 0) {
return fmt.Sprintf("%.4g", v)
}
if v == 0 {
return fmt.Sprintf("%.4gs", v)
}
if math.Abs(v) >= 1 {
sign := ""
if v < 0 {
sign = "-"
v = -v
}
seconds := int64(v) % 60
minutes := (int64(v) / 60) % 60
hours := (int64(v) / 60 / 60) % 24
days := (int64(v) / 60 / 60 / 24)
// For days to minutes, we display seconds as an integer.
if days != 0 {
return fmt.Sprintf("%s%dd %dh %dm %ds", sign, days, hours, minutes, seconds)
}
if hours != 0 {
return fmt.Sprintf("%s%dh %dm %ds", sign, hours, minutes, seconds)
}
if minutes != 0 {
return fmt.Sprintf("%s%dm %ds", sign, minutes, seconds)
}
// For seconds, we display 4 significant digts.
return fmt.Sprintf("%s%.4gs", sign, v)
}
prefix := ""
for _, p := range []string{"m", "u", "n", "p", "f", "a", "z", "y"} {
if math.Abs(v) >= 1 {
break
}
prefix = p
v *= 1000
}
return fmt.Sprintf("%.4g%ss", v, prefix)
},
}
}
func (ru *Rule) alerts(w http.ResponseWriter, r *http.Request) {
alerts := ru.ruleManager.AlertingRules()
alertsSorter := byAlertStateAndNameSorter{alerts: alerts}
sort.Sort(alertsSorter)
alertStatus := AlertStatus{
AlertingRules: alertsSorter.alerts,
AlertStateToRowClass: map[rules.AlertState]string{
rules.StateInactive: "success",
rules.StatePending: "warning",
rules.StateFiring: "danger",
},
}
prefix := GetWebPrefix(ru.logger, ru.flagsMap, r)
ru.executeTemplate(w, "alerts.html", prefix, alertStatus)
}
func (ru *Rule) rules(w http.ResponseWriter, r *http.Request) {
prefix := GetWebPrefix(ru.logger, ru.flagsMap, r)
ru.executeTemplate(w, "rules.html", prefix, ru.ruleManager)
}
// root redirects / requests to /graph, taking into account the path prefix value
func (ru *Rule) root(w http.ResponseWriter, r *http.Request) {
prefix := GetWebPrefix(ru.logger, ru.flagsMap, r)
http.Redirect(w, r, path.Join(prefix, "/alerts"), http.StatusFound)
}
func (ru *Rule) Register(r *route.Router) {
instrf := prometheus.InstrumentHandlerFunc
r.Get("/", instrf("root", ru.root))
r.Get("/alerts", instrf("alerts", ru.alerts))
r.Get("/rules", instrf("rules", ru.rules))
r.Get("/static/*filepath", instrf("static", ru.serveStaticAsset))
}
// AlertStatus bundles alerting rules and the mapping of alert states to row classes.
type AlertStatus struct {
AlertingRules []*rules.AlertingRule
AlertStateToRowClass map[rules.AlertState]string
}
type byAlertStateAndNameSorter struct {
alerts []*rules.AlertingRule
}
func (s byAlertStateAndNameSorter) Len() int {
return len(s.alerts)
}
func (s byAlertStateAndNameSorter) Less(i, j int) bool {
return s.alerts[i].State() > s.alerts[j].State() ||
(s.alerts[i].State() == s.alerts[j].State() &&
s.alerts[i].Name() < s.alerts[j].Name())
}
func (s byAlertStateAndNameSorter) Swap(i, j int) {
s.alerts[i], s.alerts[j] = s.alerts[j], s.alerts[i]
}