import "github.com/brpaz/go-healthcheck/v2"Package healthcheck provides a library for creating health check endpoints. It includes various built-in checks and new ones can be added easily. It tooks inspiration from https://inadarei.github.io/rfc-healthcheck/ for the response structure. Example Usage:
package main
import "net/http" import "github.com/brpaz/go-healthcheck/v2" import "github.com/brpaz/go-healthcheck/v2/checks/mockcheck"
func main() {
mycheck := mockcheck.NewCheck(
mockcheck.WithName("my-check"),
)
hc := healthcheck.New(
healthcheck.WithServiceID("my-service"),
healthcheck.WithDescription("My Service"),
healthcheck.WithVersion("1.0.0"),
healthcheck.WithReleaseID("1.0.0-SNAPSHOT"),
healthcheck.WithCheck(mycheck),
)
http.HandleFunc("/health", healthcheck.HealthHandler(hc))
http.ListenAndServe(":8080", nil)
}
- func HealthHandler(healthchecker *HealthCheck) http.HandlerFunc
- type CheckRunResult
- type HealthCheck
- type HealthChecker
- type HealthHttpResponse
- type Option
func HealthHandler
func HealthHandler(healthchecker *HealthCheck) http.HandlerFuncHealthHandler provides an HTTP handler that can be used to serve the health check endpoint.
type CheckRunResult
CheckRunResult aggregates the result of running a group of checks.
type CheckRunResult struct {
Status checks.Status
Checks map[string][]checks.Result
}type HealthCheck
HealthCheck aggregates multiple healthchecks and provides metadata about the service.
type HealthCheck struct {
ServiceID string
Description string
Version string
ReleaseID string
Checks []checks.Check
}func New
func New(opts ...Option) *HealthCheckNew creates a new HealthChecker instance the provided options.
func (*HealthCheck) AddCheck
func (h *HealthCheck) AddCheck(check checks.Check)AddCheck adds a new check to the HealthCheck instance.
func (*HealthCheck) Execute
func (h *HealthCheck) Execute(ctx context.Context) CheckRunResultExecute runs all registered healthchecks and returns an aggregated result, composed of the overall status and the individual results of each check. The final status is determined as follows: - If any check returns StatusFail, the overall status is StatusFail. - If no checks return StatusFail but at least one returns StatusWarn, the overall status is StatusWarn. - If all checks return StatusPass, the overall status is StatusPass.
func (*HealthCheck) GetChecks
func (h *HealthCheck) GetChecks() []checks.CheckGetChecks returns the registered checks.
type HealthChecker
type HealthChecker interface {
Execute(ctx context.Context) CheckRunResult
}type HealthHttpResponse
HealthHttpResponse represents the structure of the health check HTTP response.
type HealthHttpResponse struct {
ServiceID string `json:"serviceId,omitempty"`
Description string `json:"description,omitempty"`
Version string `json:"version,omitempty"`
ReleaseID string `json:"releaseId,omitempty"`
Output string `json:"output,omitempty"`
Status checks.Status `json:"status"`
Checks map[string][]checks.Result `json:"checks"`
}type Option
Option is a functional option for configuring HealthCheck.
type Option func(*HealthCheck)func WithCheck
func WithCheck(check checks.Check) OptionWithCheck registers a check in the HealthCheck.
func WithDescription
func WithDescription(desc string) OptionWithDescription sets the description.
func WithReleaseID
func WithReleaseID(id string) OptionWithReleaseID sets the release ID.
func WithServiceID
func WithServiceID(id string) OptionWithServiceID sets the service ID.
func WithVersion
func WithVersion(version string) OptionWithVersion sets the version.
Generated by gomarkdoc