-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
426 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ linters: | |
- gochecknoinits | ||
- goerr113 | ||
- gomnd | ||
- ireturn | ||
- nlreturn | ||
- varnamelen | ||
- wrapcheck | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Package healthz provides HTTP healthz handling. | ||
package healthz | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
// HealthChecker represents a named health checker. | ||
type HealthChecker interface { | ||
Name() string | ||
Check(*http.Request) error | ||
} | ||
|
||
type healthCheck struct { | ||
name string | ||
check func(*http.Request) error | ||
} | ||
|
||
// NamedCheck returns a named health check. | ||
func NamedCheck(name string, check func(*http.Request) error) HealthChecker { | ||
return &healthCheck{ | ||
name: name, | ||
check: check, | ||
} | ||
} | ||
|
||
func (c healthCheck) Name() string { return c.name } | ||
|
||
func (c healthCheck) Check(req *http.Request) error { return c.check(req) } | ||
|
||
// PingHealth returns true when called. | ||
var PingHealth HealthChecker = ping{} | ||
|
||
type ping struct{} | ||
|
||
func (c ping) Name() string { return "ping" } | ||
|
||
func (c ping) Check(_ *http.Request) error { return nil } | ||
|
||
// Handler returns an HTTP check handler. | ||
func Handler(name string, errFn func(string), checks ...HealthChecker) http.Handler { | ||
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { | ||
var ( | ||
checkOutput bytes.Buffer | ||
failedChecks []string | ||
failedLogOutput bytes.Buffer | ||
) | ||
for _, check := range checks { | ||
if err := check.Check(req); err != nil { | ||
_, _ = fmt.Fprintf(&checkOutput, "- %s failed\n", check.Name()) | ||
_, _ = fmt.Fprintf(&failedLogOutput, "%s failed: %v\n", check.Name(), err) | ||
failedChecks = append(failedChecks, check.Name()) | ||
continue | ||
} | ||
|
||
_, _ = fmt.Fprintf(&checkOutput, "+ %s ok\n", check.Name()) | ||
} | ||
|
||
if len(failedChecks) > 0 { | ||
errFn(failedLogOutput.String()) | ||
http.Error(rw, | ||
fmt.Sprintf("%s%s check failed", checkOutput.String(), name), | ||
http.StatusInternalServerError, | ||
) | ||
return | ||
} | ||
|
||
if _, found := req.URL.Query()["verbose"]; !found { | ||
_, _ = fmt.Fprint(rw, "ok") | ||
return | ||
} | ||
|
||
_, _ = checkOutput.WriteTo(rw) | ||
_, _ = fmt.Fprintf(rw, "%s check passed", name) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package healthz_test | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/hamba/pkg/v2/http/healthz" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestHandler(t *testing.T) { | ||
goodCheck := healthz.NamedCheck("good", func(*http.Request) error { return nil }) | ||
|
||
var gotOutput string | ||
h := healthz.Handler("readyz", func(output string) { | ||
gotOutput = output | ||
}, goodCheck) | ||
|
||
req := httptest.NewRequest(http.MethodGet, "/readyz", nil) | ||
rec := httptest.NewRecorder() | ||
|
||
h.ServeHTTP(rec, req) | ||
|
||
assert.Equal(t, http.StatusOK, rec.Code) | ||
assert.Equal(t, "", gotOutput) | ||
assert.Equal(t, `ok`, rec.Body.String()) | ||
} | ||
func TestHandler_Verbose(t *testing.T) { | ||
goodCheck := healthz.NamedCheck("good", func(*http.Request) error { return nil }) | ||
|
||
var gotOutput string | ||
h := healthz.Handler("readyz", func(output string) { | ||
gotOutput = output | ||
}, goodCheck) | ||
|
||
req := httptest.NewRequest(http.MethodGet, "/readyz?verbose=1", nil) | ||
rec := httptest.NewRecorder() | ||
|
||
h.ServeHTTP(rec, req) | ||
|
||
assert.Equal(t, http.StatusOK, rec.Code) | ||
assert.Equal(t, "", gotOutput) | ||
assert.Equal(t, "+ good ok\nreadyz check passed", rec.Body.String()) | ||
} | ||
|
||
func TestHandler_WithFailingChecks(t *testing.T) { | ||
goodCheck := healthz.NamedCheck("good", func(*http.Request) error { return nil }) | ||
badCheck := healthz.NamedCheck("bad", func(*http.Request) error { return errors.New("test error") }) | ||
|
||
var gotOutput string | ||
h := healthz.Handler("readyz", func(output string) { | ||
gotOutput = output | ||
}, goodCheck, badCheck) | ||
|
||
req := httptest.NewRequest(http.MethodGet, "/readyz", nil) | ||
rec := httptest.NewRecorder() | ||
|
||
h.ServeHTTP(rec, req) | ||
|
||
assert.Equal(t, http.StatusInternalServerError, rec.Code) | ||
assert.Equal(t, "bad failed: test error\n", gotOutput) | ||
assert.Equal(t, "+ good ok\n- bad failed\nreadyz check failed\n", rec.Body.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.