-
Notifications
You must be signed in to change notification settings - Fork 2
/
health.go
175 lines (152 loc) · 4.15 KB
/
health.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
175
package status
import (
"encoding/json"
"net/http"
"sync/atomic"
"go.uber.org/zap"
)
type Health struct {
log *zap.Logger
unavailableStatusCode int
statusRegistry map[string]Checker
shutdownInitiated *atomic.Pointer[bool]
}
func NewHealthHandler(sr map[string]Checker, shutdownInitiated *atomic.Pointer[bool], log *zap.Logger, usc int) *Health {
return &Health{
statusRegistry: sr,
unavailableStatusCode: usc,
log: log,
shutdownInitiated: shutdownInitiated,
}
}
func (rd *Health) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if rd.shutdownInitiated != nil && *rd.shutdownInitiated.Load() {
http.Error(w, "service is shutting down", http.StatusOK)
return
}
// report will be used either for all plugins or for the Plugins in the query
report := make([]*Report, 0, 2)
plg := r.URL.Query()[pluginsQuery]
// if no Plugins provided, check them all
if len(plg) == 0 {
rd.log.Debug("no plugins provided, checking all plugins")
for k, pl := range rd.statusRegistry {
if pl == nil {
report = append(report, &Report{
PluginName: k,
ErrorMessage: "plugin is nil or not initialized",
StatusCode: http.StatusNotFound,
})
rd.log.Info("plugin is nil or not initialized", zap.String("plugin", k))
continue
}
st, err := pl.Status()
if err != nil {
w.WriteHeader(rd.unavailableStatusCode)
report = append(report, &Report{
PluginName: k,
ErrorMessage: err.Error(),
StatusCode: rd.unavailableStatusCode,
})
continue
}
if st == nil {
report = append(report, &Report{
PluginName: k,
ErrorMessage: "plugin is not available, returned nil",
StatusCode: rd.unavailableStatusCode,
})
continue
}
switch {
case st.Code >= 500:
w.WriteHeader(rd.unavailableStatusCode)
report = append(report, &Report{
PluginName: k,
ErrorMessage: "internal server error, see logs",
StatusCode: rd.unavailableStatusCode,
})
case st.Code >= 100 && st.Code <= 400:
report = append(report, &Report{
PluginName: k,
StatusCode: st.Code,
})
default:
report = append(report, &Report{
PluginName: k,
ErrorMessage: "unexpected status code",
StatusCode: st.Code,
})
}
}
data, err := json.Marshal(report)
if err != nil {
// TODO do we need to write this error to the ResponseWriter?
rd.log.Error("failed to marshal response", zap.Error(err))
return
}
// write the response
_, err = w.Write(data)
if err != nil {
rd.log.Error("failed to write response", zap.Error(err))
}
return
}
// iterate over all provided Plugins
for i := 0; i < len(plg); i++ {
if svc, ok := rd.statusRegistry[plg[i]]; ok {
if svc == nil {
continue
}
st, err := rd.statusRegistry[plg[i]].Status()
if err != nil {
report = append(report, &Report{
PluginName: plg[i],
ErrorMessage: err.Error(),
StatusCode: http.StatusInternalServerError,
})
continue
}
if st == nil {
report = append(report, &Report{
PluginName: plg[i],
ErrorMessage: "plugin is not available",
StatusCode: rd.unavailableStatusCode,
})
continue
}
switch {
case st.Code >= 500:
// on >=500, write header, because it'll be written on Write (200)
w.WriteHeader(rd.unavailableStatusCode)
report = append(report, &Report{
PluginName: plg[i],
ErrorMessage: "internal server error, see logs",
StatusCode: rd.unavailableStatusCode,
})
case st.Code >= 100 && st.Code <= 400:
report = append(report, &Report{
PluginName: plg[i],
StatusCode: st.Code,
})
default:
report = append(report, &Report{
PluginName: plg[i],
ErrorMessage: "unexpected status code",
StatusCode: st.Code,
})
}
} else {
rd.log.Info("plugin does not support health checks", zap.String("plugin", plg[i]))
}
}
data, err := json.Marshal(report)
if err != nil {
rd.log.Error("failed to marshal response", zap.Error(err))
}
// write the response
_, err = w.Write(data)
if err != nil {
rd.log.Error("failed to write response", zap.Error(err))
}
}