healthcheck

package
v1.96.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 12, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package healthcheck provides a simple way to define health checks for external services or components.

It provides an HTTP handler to collect the results of the health checks concurrently.

For an implementation example see the examples/service/internal/cli/bind.go file.

Index

Constants

View Source
const (
	// StatusOK represents an OK status.
	StatusOK = "OK"
)

Variables

This section is empty.

Functions

func CheckHTTPStatus

func CheckHTTPStatus(ctx context.Context, httpClient HTTPClient, method string, url string, wantStatusCode int, timeout time.Duration, opts ...CheckOption) error

CheckHTTPStatus checks if the given HTTP request responds with the expected status code.

Types

type CheckOption

type CheckOption func(*checkConfig)

CheckOption is a type alias for a function able to configure HTTP healthcheck options.

func WithConfigureRequest

func WithConfigureRequest(fn func(r *http.Request)) CheckOption

WithConfigureRequest allows to configure the request before it is sent.

type HTTPClient

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPClient contains the function that performs the actual HTTP request.

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler is the struct containng the HTTP handler function that performs the healthchecks.

func NewHandler

func NewHandler(checks []HealthCheck, opts ...HandlerOption) *Handler

NewHandler creates a new instance of the healthcheck handler.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP runs the configured health checks in parallel and collects their results.

type HandlerOption

type HandlerOption func(h *Handler)

HandlerOption is a type alias for a function that configures the healthcheck HTTP handler.

func WithResultWriter

func WithResultWriter(w ResultWriter) HandlerOption

WithResultWriter overrides the default healthcheck result writer.

type HealthCheck

type HealthCheck struct {
	// ID is a unique identifier for the healthcheck.
	ID string

	// Checker is the function used to perform the healthchecks.
	Checker HealthChecker
}

HealthCheck is a structure containing the configuration for a single health check.

func New

func New(id string, checker HealthChecker) HealthCheck

New creates a new instance of a health check configuration with default timeout.

type HealthChecker

type HealthChecker interface {
	HealthCheck(ctx context.Context) error
}

HealthChecker is the interface that wraps the HealthCheck method.

type ResultWriter

type ResultWriter func(ctx context.Context, w http.ResponseWriter, statusCode int, data any)

ResultWriter is a type alias for a function in charge of writing the result of the health checks.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL