-
Notifications
You must be signed in to change notification settings - Fork 599
/
healthcheck.go
211 lines (178 loc) · 6.43 KB
/
healthcheck.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package healthcheck
import (
"context"
"sync"
"time"
"github.com/coder/coder/v2/buildinfo"
"github.com/coder/coder/v2/coderd/healthcheck/derphealth"
"github.com/coder/coder/v2/coderd/healthcheck/health"
"github.com/coder/coder/v2/coderd/util/ptr"
"github.com/coder/coder/v2/codersdk/healthsdk"
)
type Checker interface {
DERP(ctx context.Context, opts *derphealth.ReportOptions) healthsdk.DERPHealthReport
AccessURL(ctx context.Context, opts *AccessURLReportOptions) healthsdk.AccessURLReport
Websocket(ctx context.Context, opts *WebsocketReportOptions) healthsdk.WebsocketReport
Database(ctx context.Context, opts *DatabaseReportOptions) healthsdk.DatabaseReport
WorkspaceProxy(ctx context.Context, opts *WorkspaceProxyReportOptions) healthsdk.WorkspaceProxyReport
ProvisionerDaemons(ctx context.Context, opts *ProvisionerDaemonsReportDeps) healthsdk.ProvisionerDaemonsReport
}
type ReportOptions struct {
AccessURL AccessURLReportOptions
Database DatabaseReportOptions
DerpHealth derphealth.ReportOptions
Websocket WebsocketReportOptions
WorkspaceProxy WorkspaceProxyReportOptions
ProvisionerDaemons ProvisionerDaemonsReportDeps
Checker Checker
}
type defaultChecker struct{}
func (defaultChecker) DERP(ctx context.Context, opts *derphealth.ReportOptions) healthsdk.DERPHealthReport {
var report derphealth.Report
report.Run(ctx, opts)
return healthsdk.DERPHealthReport(report)
}
func (defaultChecker) AccessURL(ctx context.Context, opts *AccessURLReportOptions) healthsdk.AccessURLReport {
var report AccessURLReport
report.Run(ctx, opts)
return healthsdk.AccessURLReport(report)
}
func (defaultChecker) Websocket(ctx context.Context, opts *WebsocketReportOptions) healthsdk.WebsocketReport {
var report WebsocketReport
report.Run(ctx, opts)
return healthsdk.WebsocketReport(report)
}
func (defaultChecker) Database(ctx context.Context, opts *DatabaseReportOptions) healthsdk.DatabaseReport {
var report DatabaseReport
report.Run(ctx, opts)
return healthsdk.DatabaseReport(report)
}
func (defaultChecker) WorkspaceProxy(ctx context.Context, opts *WorkspaceProxyReportOptions) healthsdk.WorkspaceProxyReport {
var report WorkspaceProxyReport
report.Run(ctx, opts)
return healthsdk.WorkspaceProxyReport(report)
}
func (defaultChecker) ProvisionerDaemons(ctx context.Context, opts *ProvisionerDaemonsReportDeps) healthsdk.ProvisionerDaemonsReport {
var report ProvisionerDaemonsReport
report.Run(ctx, opts)
return healthsdk.ProvisionerDaemonsReport(report)
}
func Run(ctx context.Context, opts *ReportOptions) *healthsdk.HealthcheckReport {
var (
wg sync.WaitGroup
report healthsdk.HealthcheckReport
)
if opts.Checker == nil {
opts.Checker = defaultChecker{}
}
wg.Add(1)
go func() {
defer wg.Done()
defer func() {
if err := recover(); err != nil {
report.DERP.Error = health.Errorf(health.CodeUnknown, "derp report panic: %s", err)
}
}()
report.DERP = opts.Checker.DERP(ctx, &opts.DerpHealth)
}()
wg.Add(1)
go func() {
defer wg.Done()
defer func() {
if err := recover(); err != nil {
report.AccessURL.Error = health.Errorf(health.CodeUnknown, "access url report panic: %s", err)
}
}()
report.AccessURL = opts.Checker.AccessURL(ctx, &opts.AccessURL)
}()
wg.Add(1)
go func() {
defer wg.Done()
defer func() {
if err := recover(); err != nil {
report.Websocket.Error = health.Errorf(health.CodeUnknown, "websocket report panic: %s", err)
}
}()
report.Websocket = opts.Checker.Websocket(ctx, &opts.Websocket)
}()
wg.Add(1)
go func() {
defer wg.Done()
defer func() {
if err := recover(); err != nil {
report.Database.Error = health.Errorf(health.CodeUnknown, "database report panic: %s", err)
}
}()
report.Database = opts.Checker.Database(ctx, &opts.Database)
}()
wg.Add(1)
go func() {
defer wg.Done()
defer func() {
if err := recover(); err != nil {
report.WorkspaceProxy.Error = health.Errorf(health.CodeUnknown, "proxy report panic: %s", err)
}
}()
report.WorkspaceProxy = opts.Checker.WorkspaceProxy(ctx, &opts.WorkspaceProxy)
}()
wg.Add(1)
go func() {
defer wg.Done()
defer func() {
if err := recover(); err != nil {
report.ProvisionerDaemons.Error = health.Errorf(health.CodeUnknown, "provisioner daemon report panic: %s", err)
}
}()
report.ProvisionerDaemons = opts.Checker.ProvisionerDaemons(ctx, &opts.ProvisionerDaemons)
}()
report.CoderVersion = buildinfo.Version()
wg.Wait()
report.Time = time.Now()
report.FailingSections = []healthsdk.HealthSection{}
if report.DERP.Severity.Value() > health.SeverityWarning.Value() {
report.FailingSections = append(report.FailingSections, healthsdk.HealthSectionDERP)
}
if report.AccessURL.Severity.Value() > health.SeverityOK.Value() {
report.FailingSections = append(report.FailingSections, healthsdk.HealthSectionAccessURL)
}
if report.Websocket.Severity.Value() > health.SeverityWarning.Value() {
report.FailingSections = append(report.FailingSections, healthsdk.HealthSectionWebsocket)
}
if report.Database.Severity.Value() > health.SeverityWarning.Value() {
report.FailingSections = append(report.FailingSections, healthsdk.HealthSectionDatabase)
}
if report.WorkspaceProxy.Severity.Value() > health.SeverityWarning.Value() {
report.FailingSections = append(report.FailingSections, healthsdk.HealthSectionWorkspaceProxy)
}
if report.ProvisionerDaemons.Severity.Value() > health.SeverityWarning.Value() {
report.FailingSections = append(report.FailingSections, healthsdk.HealthSectionProvisionerDaemons)
}
report.Healthy = len(report.FailingSections) == 0
// Review healthcheck sub-reports.
report.Severity = health.SeverityOK
if report.DERP.Severity.Value() > report.Severity.Value() {
report.Severity = report.DERP.Severity
}
if report.AccessURL.Severity.Value() > report.Severity.Value() {
report.Severity = report.AccessURL.Severity
}
if report.Websocket.Severity.Value() > report.Severity.Value() {
report.Severity = report.Websocket.Severity
}
if report.Database.Severity.Value() > report.Severity.Value() {
report.Severity = report.Database.Severity
}
if report.WorkspaceProxy.Severity.Value() > report.Severity.Value() {
report.Severity = report.WorkspaceProxy.Severity
}
if report.ProvisionerDaemons.Severity.Value() > report.Severity.Value() {
report.Severity = report.ProvisionerDaemons.Severity
}
return &report
}
func convertError(err error) *string {
if err != nil {
return ptr.Ref(err.Error())
}
return nil
}