Skip to content

Commit 8dcbafd

Browse files
committed
WIP
1 parent b373cc2 commit 8dcbafd

File tree

4 files changed

+16
-2
lines changed

4 files changed

+16
-2
lines changed

coderd/healthcheck/database.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type DatabaseReportOptions struct {
3535

3636
func (r *DatabaseReport) Run(ctx context.Context, opts *DatabaseReportOptions) {
3737
r.Warnings = []string{}
38+
r.Severity = health.SeverityOK
3839
r.ThresholdMS = opts.Threshold.Milliseconds()
3940
if r.ThresholdMS == 0 {
4041
r.ThresholdMS = DatabaseDefaultThreshold.Milliseconds()
@@ -49,6 +50,7 @@ func (r *DatabaseReport) Run(ctx context.Context, opts *DatabaseReportOptions) {
4950
pong, err := opts.DB.Ping(ctx)
5051
if err != nil {
5152
r.Error = convertError(xerrors.Errorf("ping: %w", err))
53+
r.Severity = health.SeverityError
5254
return
5355
}
5456
pings = append(pings, pong)
@@ -59,8 +61,9 @@ func (r *DatabaseReport) Run(ctx context.Context, opts *DatabaseReportOptions) {
5961
latency := pings[pingCount/2]
6062
r.Latency = latency.String()
6163
r.LatencyMS = latency.Milliseconds()
62-
if r.LatencyMS < r.ThresholdMS {
63-
r.Healthy = true
64+
if r.LatencyMS >= r.ThresholdMS {
65+
r.Severity = health.SeverityWarning
6466
}
67+
r.Healthy = true
6568
r.Reachable = true
6669
}

coderd/healthcheck/database_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/coder/coder/v2/coderd/database/dbmock"
1414
"github.com/coder/coder/v2/coderd/healthcheck"
15+
"github.com/coder/coder/v2/coderd/healthcheck/health"
1516
"github.com/coder/coder/v2/testutil"
1617
)
1718

@@ -35,6 +36,7 @@ func TestDatabase(t *testing.T) {
3536

3637
assert.True(t, report.Healthy)
3738
assert.True(t, report.Reachable)
39+
assert.Equal(t, health.SeverityOK, report.Severity)
3840
assert.Equal(t, ping.String(), report.Latency)
3941
assert.Equal(t, ping.Milliseconds(), report.LatencyMS)
4042
assert.Equal(t, healthcheck.DatabaseDefaultThreshold.Milliseconds(), report.ThresholdMS)

coderd/healthcheck/websocket.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ func (r *WebsocketReport) Run(ctx context.Context, opts *WebsocketReportOptions)
3636
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
3737
defer cancel()
3838

39+
r.Severity = health.SeverityOK
3940
r.Warnings = []string{}
4041
u, err := opts.AccessURL.Parse("/api/v2/debug/ws")
4142
if err != nil {
4243
r.Error = convertError(xerrors.Errorf("parse access url: %w", err))
44+
r.Severity = health.SeverityError
4345
return
4446
}
4547
if u.Scheme == "https" {
@@ -67,6 +69,7 @@ func (r *WebsocketReport) Run(ctx context.Context, opts *WebsocketReportOptions)
6769
}
6870
if err != nil {
6971
r.Error = convertError(xerrors.Errorf("websocket dial: %w", err))
72+
r.Severity = health.SeverityError
7073
return
7174
}
7275
defer c.Close(websocket.StatusGoingAway, "goodbye")
@@ -76,22 +79,26 @@ func (r *WebsocketReport) Run(ctx context.Context, opts *WebsocketReportOptions)
7679
err := c.Write(ctx, websocket.MessageText, []byte(msg))
7780
if err != nil {
7881
r.Error = convertError(xerrors.Errorf("write message: %w", err))
82+
r.Severity = health.SeverityError
7983
return
8084
}
8185

8286
ty, got, err := c.Read(ctx)
8387
if err != nil {
8488
r.Error = convertError(xerrors.Errorf("read message: %w", err))
89+
r.Severity = health.SeverityError
8590
return
8691
}
8792

8893
if ty != websocket.MessageText {
8994
r.Error = convertError(xerrors.Errorf("received incorrect message type: %v", ty))
95+
r.Severity = health.SeverityError
9096
return
9197
}
9298

9399
if string(got) != msg {
94100
r.Error = convertError(xerrors.Errorf("received incorrect message: wanted %q, got %q", msg, string(got)))
101+
r.Severity = health.SeverityError
95102
return
96103
}
97104
}

coderd/healthcheck/websocket_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"golang.org/x/xerrors"
1313

1414
"github.com/coder/coder/v2/coderd/healthcheck"
15+
"github.com/coder/coder/v2/coderd/healthcheck/health"
1516
"github.com/coder/coder/v2/testutil"
1617
)
1718

@@ -63,6 +64,7 @@ func TestWebsocket(t *testing.T) {
6364
})
6465

6566
require.NotNil(t, wsReport.Error)
67+
require.Equal(t, health.SeverityError, wsReport.Severity)
6668
assert.Equal(t, wsReport.Body, "test error")
6769
assert.Equal(t, wsReport.Code, http.StatusBadRequest)
6870
})

0 commit comments

Comments
 (0)