8000 feat: include health severity in reports by mtojek · Pull Request #10817 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

feat: include health severity in reports #10817

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
WIP
  • Loading branch information
mtojek committed Nov 22, 2023
commit 8dcbafdd5ac47fb39b169534d5d055ade31aa8f6
7 changes: 5 additions & 2 deletions coderd/healthcheck/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type DatabaseReportOptions struct {

func (r *DatabaseReport) Run(ctx context.Context, opts *DatabaseReportOptions) {
r.Warnings = []string{}
r.Severity = health.SeverityOK
r.ThresholdMS = opts.Threshold.Milliseconds()
if r.ThresholdMS == 0 {
r.ThresholdMS = DatabaseDefaultThreshold.Milliseconds()
Expand All @@ -49,6 +50,7 @@ func (r *DatabaseReport) Run(ctx context.Context, opts *DatabaseReportOptions) {
pong, err := opts.DB.Ping(ctx)
if err != nil {
r.Error = convertError(xerrors.Errorf("ping: %w", err))
r.Severity = health.SeverityError
return
}
pings = append(pings, pong)
Expand All @@ -59,8 +61,9 @@ func (r *DatabaseReport) Run(ctx context.Context, opts *DatabaseReportOptions) {
latency := pings[pingCount/2]
r.Latency = latency.String()
r.LatencyMS = latency.Milliseconds()
if r.LatencyMS < r.ThresholdMS {
r.Healthy = true
if r.LatencyMS >= r.ThresholdMS {
r.Severity = health.SeverityWarning
}
r.Healthy = true
r.Reachable = true
}
2 changes: 2 additions & 0 deletions coderd/healthcheck/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/coder/coder/v2/coderd/database/dbmock"
"github.com/coder/coder/v2/coderd/healthcheck"
"github.com/coder/coder/v2/coderd/healthcheck/health"
"github.com/coder/coder/v2/testutil"
)

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

assert.True(t, report.Healthy)
assert.True(t, report.Reachable)
assert.Equal(t, health.SeverityOK, report.Severity)
assert.Equal(t, ping.String(), report.Latency)
assert.Equal(t, ping.Milliseconds(), report.LatencyMS)
assert.Equal(t, healthcheck.DatabaseDefaultThreshold.Milliseconds(), report.ThresholdMS)
Expand Down
7 changes: 7 additions & 0 deletions coderd/healthcheck/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ func (r *WebsocketReport) Run(ctx context.Context, opts *WebsocketReportOptions)
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

r.Severity = health.SeverityOK
r.Warnings = []string{}
u, err := opts.AccessURL.Parse("/api/v2/debug/ws")
if err != nil {
r.Error = convertError(xerrors.Errorf("parse access url: %w", err))
r.Severity = health.SeverityError
return
}
if u.Scheme == "https" {
Expand Down Expand Up @@ -67,6 +69,7 @@ func (r *WebsocketReport) Run(ctx context.Context, opts *WebsocketReportOptions)
}
if err != nil {
r.Error = convertError(xerrors.Errorf("websocket dial: %w", err))
r.Severity = health.SeverityError
return
}
defer c.Close(websocket.StatusGoingAway, "goodbye")
Expand All @@ -76,22 +79,26 @@ func (r *WebsocketReport) Run(ctx context.Context, opts *WebsocketReportOptions)
err := c.Write(ctx, websocket.MessageText, []byte(msg))
if err != nil {
r.Error = convertError(xerrors.Errorf("write message: %w", err))
r.Severity = health.SeverityError
return
}

ty, got, err := c.Read(ctx)
if err != nil {
r.Error = convertError(xerrors.Errorf("read message: %w", err))
r.Severity = health.SeverityError
return
}

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

if string(got) != msg {
r.Error = convertError(xerrors.Errorf("received incorrect message: wanted %q, got %q", msg, string(got)))
r.Severity = health.SeverityError
return
}
}
Expand Down
2 changes: 2 additions & 0 deletions coderd/healthcheck/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"golang.org/x/xerrors"

"github.com/coder/coder/v2/coderd/healthcheck"
"github.com/coder/coder/v2/coderd/healthcheck/health"
"github.com/coder/coder/v2/testutil"
)

Expand Down Expand Up @@ -63,6 +64,7 @@ func TestWebsocket(t *testing.T) {
})

require.NotNil(t, wsReport.Error)
require.Equal(t, health.SeverityError, wsReport.Severity)
assert.Equal(t, wsReport.Body, "test error")
assert.Equal(t, wsReport.Code, http.StatusBadRequest)
})
Expand Down
0