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
more rules
  • Loading branch information
mtojek committed Nov 22, 2023
commit d00802e3867af327f8ea94af6a6bc5075ee88796
9 changes: 9 additions & 0 deletions coderd/healthcheck/accessurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ func (r *AccessURLReport) Run(ctx context.Context, opts *AccessURLReportOptions)
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

r.Severity = health.SeverityOK
r.Warnings = []string{}
if opts.AccessURL == nil {
r.Error = ptr.Ref("access URL is nil")
r.Severity = health.SeverityError
return
}
r.AccessURL = opts.AccessURL.String()
Expand All @@ -49,30 +51,37 @@ func (r *AccessURLReport) Run(ctx context.Context, opts *AccessURLReportOptions)
accessURL, err := opts.AccessURL.Parse("/healthz")
if err != nil {
r.Error = convertError(xerrors.Errorf("parse healthz endpoint: %w", err))
r.Severity = health.SeverityError
return
}

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

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

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

r.Reachable = true
r.Healthy = res.StatusCode == http.StatusOK
r.StatusCode = res.StatusCode
if res.StatusCode != http.StatusOK {
r.Severity = health.SeverityWarning
}
r.HealthzResponse = string(body)
}
4 changes: 4 additions & 0 deletions coderd/healthcheck/accessurl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

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

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

assert.True(t, report.Healthy)
assert.True(t, report.Reachable)
assert.Equal(t, health.SeverityOK, report.Severity)
assert.Equal(t, http.StatusOK, report.StatusCode)
assert.Equal(t, "OK", report.HealthzResponse)
assert.Nil(t, report.Error)
Expand Down Expand Up @@ -64,6 +66,7 @@ func TestAccessURL(t *testing.T) {

assert.False(t, report.Healthy)
assert.True(t, report.Reachable)
assert.Equal(t, health.SeverityWarning, report.Severity)
assert.Equal(t, http.StatusNotFound, report.StatusCode)
assert.Equal(t, string(resp), report.HealthzResponse)
assert.Nil(t, report.Error)
Expand Down Expand Up @@ -100,6 +103,7 @@ func TestAccessURL(t *testing.T) {

assert.False(t, report.Healthy)
assert.False(t, report.Reachable)
assert.Equal(t, health.SeverityError, report.Severity)
assert.Equal(t, 0, report.StatusCode)
assert.Equal(t, "", report.HealthzResponse)
require.NotNil(t, report.Error)
Expand Down
3 changes: 3 additions & 0 deletions coderd/healthcheck/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func TestDatabase(t *testing.T) {

assert.False(t, report.Healthy)
assert.False(t, report.Reachable)
assert.Equal(t, health.SeverityError, report.Severity)
assert.Zero(t, report.Latency)
require.NotNil(t, report.Error)
assert.Equal(t, healthcheck.DatabaseDefaultThreshold.Milliseconds(), report.ThresholdMS)
Expand All @@ -86,6 +87,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, time.Millisecond.String(), report.Latency)
assert.EqualValues(t, 1, report.LatencyMS)
assert.Equal(t, healthcheck.DatabaseDefaultThreshold.Milliseconds(), report.ThresholdMS)
Expand All @@ -112,6 +114,7 @@ func TestDatabase(t *testing.T) {

assert.False(t, report.Healthy)
assert.True(t, report.Reachable)
assert.Equal(t, health.SeverityWarning, report.Severity)
assert.Equal(t, time.Second.String(), report.Latency)
assert.EqualValues(t, 1000, report.LatencyMS)
assert.Equal(t, time.Second.Milliseconds(), report.ThresholdMS)
Expand Down
0