/
web.go
174 lines (151 loc) · 4.43 KB
/
web.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
package alertmanager
import (
"encoding/json"
"net/http"
"slices"
"time"
"github.com/synyx/tuwat/pkg/aggregation"
"github.com/synyx/tuwat/pkg/config"
"github.com/synyx/tuwat/pkg/version"
"github.com/synyx/tuwat/pkg/web/common"
"github.com/uptrace/opentelemetry-go-extra/otelzap"
"go.uber.org/zap"
)
type alertmanagerHandler struct {
routes []common.Route
aggregator *aggregation.Aggregator
started time.Time
cfg *config.Config
}
func ApiV2(cfg *config.Config, aggregator *aggregation.Aggregator) http.Handler {
handler := &alertmanagerHandler{
aggregator: aggregator,
started: time.Now(),
cfg: cfg,
}
handler.routes = []common.Route{
common.NewRoute("GET", "/v2/status", handler.status),
common.NewRoute("GET", "/v2/alerts", handler.alerts),
}
return handler
}
func (h *alertmanagerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
switch err := err.(type) {
case error:
otelzap.Ctx(r.Context()).Error("panic serving", zap.Error(err))
default:
otelzap.Ctx(r.Context()).Error("panic serving", zap.Any("error", err))
}
http.Error(w, "500 Internal Server Error", http.StatusInternalServerError)
}
}()
if ok := common.HandleRoute(h.routes, w, r); !ok {
http.Error(w, "404 not found", http.StatusNotFound)
}
}
func (h *alertmanagerHandler) status(w http.ResponseWriter, _ *http.Request) {
status := alertmanagerStatus{
ClusterStatus: clusterStatus{Status: "ready"},
VersionInfo: versionInfo{
Version: version.Info.Version,
Revision: version.Info.Revision,
Branch: "unknown",
BuildUser: "Jane Doe",
BuildDate: version.Info.ReleaseDate,
GoVersion: version.Info.GoVersion,
},
Config: alertmanagerConfig{Original: ""},
Uptime: h.started.Format(time.RFC3339),
}
writer := json.NewEncoder(w)
if err := writer.Encode(&status); err != nil {
http.Error(w, "500 internal server error", http.StatusInternalServerError)
}
}
func (h *alertmanagerHandler) alerts(w http.ResponseWriter, r *http.Request) {
var gettableAlerts []gettableAlert
active := true
inhibited := true
var filters []fieldMatcher
if val := r.URL.Query().Get("active"); val != "" {
active = val == "true"
}
if val := r.URL.Query().Get("inhibited"); val != "" {
inhibited = val == "true"
}
if ok := r.URL.Query().Has("filter"); ok {
for _, val := range r.URL.Query()["filter"] {
matchers := parseFilter(val)
filters = append(filters, matchers...)
}
}
for _, dashboard := range h.cfg.Dashboards {
aggregate := h.aggregator.Alerts(dashboard.Name)
for _, alert := range aggregate.Alerts {
if alert.Status == "green" {
continue
}
gettableAlerts = append(gettableAlerts, mapAlert(dashboard.Name, aggregate, alert, "active"))
}
for _, alert := range aggregate.Blocked {
if alert.Status == "green" {
continue
}
gettableAlerts = append(gettableAlerts, mapAlert(dashboard.Name, aggregate, alert.Alert, "suppressed"))
}
}
if active == false {
gettableAlerts = slices.DeleteFunc(gettableAlerts, func(x gettableAlert) bool {
return x.Status.State == "active"
})
}
if inhibited == false {
gettableAlerts = slices.DeleteFunc(gettableAlerts, func(x gettableAlert) bool {
return x.Status.State == "suppressed"
})
}
if len(filters) > 0 {
gettableAlerts = slices.DeleteFunc(gettableAlerts, func(x gettableAlert) bool {
matches := 0
for _, filter := range filters {
if s, ok := x.Annotations[filter.field]; !ok {
return true
} else if filter.m.MatchString(s) {
matches += 1
}
}
return matches != len(filters)
})
}
writer := json.NewEncoder(w)
if err := writer.Encode(gettableAlerts); err != nil {
http.Error(w, "500 internal server error", http.StatusInternalServerError)
}
}
func mapAlert(dashboard string, aggregate aggregation.Aggregate, alert aggregation.Alert, state string) gettableAlert {
labels := alert.Labels
labels["dashboard"] = dashboard
switch alert.Status {
case "red":
labels["severity"] = "critical"
case "yellow":
labels["severity"] = "warning"
default:
labels["severity"] = ""
}
ga := gettableAlert{
Annotations: labels,
Fingerprint: alert.Id,
StartsAt: time.Now().Add(alert.When).Format(time.RFC3339),
UpdatedAt: aggregate.CheckTime.Format(time.RFC3339),
EndsAt: time.Now().Add(1 * time.Hour).Format(time.RFC3339),
Status: alertStatus{
State: state,
SilencedBy: nil,
InhibitedBy: nil,
},
}
return ga
}