Skip to content

Latest commit

 

History

History
206 lines (149 loc) · 6.46 KB

File metadata and controls

206 lines (149 loc) · 6.46 KB

healthcheck

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)
}

Index

func HealthHandler(healthchecker *HealthCheck) http.HandlerFunc

HealthHandler provides an HTTP handler that can be used to serve the health check endpoint.

CheckRunResult aggregates the result of running a group of checks.

type CheckRunResult struct {
    Status checks.Status
    Checks map[string][]checks.Result
}

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) *HealthCheck

New 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) CheckRunResult

Execute 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.Check

GetChecks returns the registered checks.

type HealthChecker interface {
    Execute(ctx context.Context) CheckRunResult
}

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(check checks.Check) Option

WithCheck registers a check in the HealthCheck.

func WithDescription(desc string) Option

WithDescription sets the description.

func WithReleaseID(id string) Option

WithReleaseID sets the release ID.

func WithServiceID(id string) Option

WithServiceID sets the service ID.

func WithVersion(version string) Option

WithVersion sets the version.

Generated by gomarkdoc