Skip to content

fix: use *string instead of error in healthcheck response #8234

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 3 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
40 changes: 32 additions & 8 deletions coderd/apidoc/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 32 additions & 8 deletions coderd/apidoc/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 14 additions & 10 deletions coderd/healthcheck/accessurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ import (
"time"

"golang.org/x/xerrors"

"github.com/coder/coder/coderd/util/ptr"
)

type AccessURLReport struct {
Healthy bool `json:"healthy"`
Reachable bool `json:"reachable"`
StatusCode int `json:"status_code"`
HealthzResponse string `json:"healthz_response"`
Error error `json:"error"`
AccessURL string `json:"access_url"`
Healthy bool `json:"healthy"`
Reachable bool `json:"reachable"`
StatusCode int `json:"status_code"`
HealthzResponse string `json:"healthz_response"`
Error *string `json:"error"`
}

type AccessURLReportOptions struct {
Expand All @@ -28,36 +31,37 @@ func (r *AccessURLReport) Run(ctx context.Context, opts *AccessURLReportOptions)
defer cancel()

if opts.AccessURL == nil {
r.Error = xerrors.New("access URL is nil")
r.Error = ptr.Ref("access URL is nil")
return
}
r.AccessURL = opts.AccessURL.String()

if opts.Client == nil {
opts.Client = http.DefaultClient
}

accessURL, err := opts.AccessURL.Parse("/healthz")
if err != nil {
r.Error = xerrors.Errorf("parse healthz endpoint: %w", err)
r.Error = convertError(xerrors.Errorf("parse healthz endpoint: %w", err))
return
}

req, err := http.NewRequestWithContext(ctx, "GET", accessURL.String(), nil)
if err != nil {
r.Error = xerrors.Errorf("create healthz request: %w", err)
r.Error = convertError(xerrors.Errorf("create healthz request: %w", err))
return
}

res, err := opts.Client.Do(req)
if err != nil {
r.Error = xerrors.Errorf("get healthz endpoint: %w", err)
r.Error = convertError(xerrors.Errorf("get healthz endpoint: %w", err))
return
}
defer res.Body.Close()

body, err := io.ReadAll(res.Body)
if err != nil {
r.Error = xerrors.Errorf("read healthz response: %w", err)
r.Error = convertError(xerrors.Errorf("read healthz response: %w", err))
return
}

Expand Down
7 changes: 4 additions & 3 deletions coderd/healthcheck/accessurl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestAccessURL(t *testing.T) {
assert.True(t, report.Reachable)
assert.Equal(t, http.StatusOK, report.StatusCode)
assert.Equal(t, "OK", report.HealthzResponse)
assert.NoError(t, report.Error)
assert.Nil(t, report.Error)
})

t.Run("404", func(t *testing.T) {
Expand Down Expand Up @@ -66,7 +66,7 @@ func TestAccessURL(t *testing.T) {
assert.True(t, report.Reachable)
assert.Equal(t, http.StatusNotFound, report.StatusCode)
assert.Equal(t, string(resp), report.HealthzResponse)
assert.NoError(t, report.Error)
assert.Nil(t, report.Error)
})

t.Run("ClientErr", func(t *testing.T) {
Expand Down Expand Up @@ -102,7 +102,8 @@ func TestAccessURL(t *testing.T) {
assert.False(t, report.Reachable)
assert.Equal(t, 0, report.StatusCode)
assert.Equal(t, "", report.HealthzResponse)
assert.ErrorIs(t, report.Error, expErr)
require.NotNil(t, report.Error)
assert.Contains(t, *report.Error, expErr.Error())
})
}

Expand Down
4 changes: 2 additions & 2 deletions coderd/healthcheck/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type DatabaseReport struct {
Healthy bool `json:"healthy"`
Reachable bool `json:"reachable"`
Latency time.Duration `json:"latency"`
Error error `json:"error"`
Error *string `json:"error"`
}

type DatabaseReportOptions struct {
Expand All @@ -31,7 +31,7 @@ func (r *DatabaseReport) Run(ctx context.Context, opts *DatabaseReportOptions) {
for i := 0; i < pingCount; i++ {
pong, err := opts.DB.Ping(ctx)
if err != nil {
r.Error = xerrors.Errorf("ping: %w", err)
r.Error = convertError(xerrors.Errorf("ping: %w", err))
return
}
pings = append(pings, pong)
Expand Down
8 changes: 5 additions & 3 deletions coderd/healthcheck/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/xerrors"

"github.com/coder/coder/coderd/database/dbmock"
Expand Down Expand Up @@ -35,7 +36,7 @@ func TestDatabase(t *testing.T) {
assert.True(t, report.Healthy)
assert.True(t, report.Reachable)
assert.Equal(t, ping, report.Latency)
assert.NoError(t, report.Error)
assert.Nil(t, report.Error)
})

t.Run("Error", func(t *testing.T) {
Expand All @@ -56,7 +57,8 @@ func TestDatabase(t *testing.T) {
assert.False(t, report.Healthy)
assert.False(t, report.Reachable)
assert.Zero(t, report.Latency)
assert.ErrorIs(t, report.Error, err)
require.NotNil(t, report.Error)
assert.Contains(t, *report.Error, err.Error())
})

t.Run("Median", func(t *testing.T) {
Expand All @@ -80,6 +82,6 @@ func TestDatabase(t *testing.T) {
assert.True(t, report.Healthy)
assert.True(t, report.Reachable)
assert.Equal(t, time.Millisecond, report.Latency)
assert.NoError(t, report.Error)
assert.Nil(t, report.Error)
})
}
Loading