healthcheck

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2024 License: MIT Imports: 1 Imported by: 9

README

Health Check Module

ci go report codecov Deps PkgGoDev

Health check module compatible with K8s probes.

Installation

go get github.com/ankorstore/yokai/healthcheck

Documentation

This module provides a Checker, that:

  • can register any CheckerProbe implementations and organise them for startup, liveness and / or readiness checks
  • and execute them to get an overall CheckerResult

The checker result will be considered as success if ALL registered probes checks are successful.

Probes

This module provides a CheckerProbe interface to implement to provide your own probes, for example:

package probes

import (
	"context"

	"github.com/ankorstore/yokai/healthcheck"
)

// success probe
type SuccessProbe struct{}

func NewSuccessProbe() *SuccessProbe {
	return &SuccessProbe{}
}

func (p *SuccessProbe) Name() string {
	return "successProbe"
}

func (p *SuccessProbe) Check(ctx context.Context) *healthcheck.CheckerProbeResult {
	return healthcheck.NewCheckerProbeResult(true, "some success")
}

// failure probe
type FailureProbe struct{}

func NewFailureProbe() *FailureProbe {
	return &FailureProbe{}
}

func (p *FailureProbe) Name() string {
	return "failureProbe"
}

func (p *FailureProbe) Check(ctx context.Context) *healthcheck.CheckerProbeResult {
	return healthcheck.NewCheckerProbeResult(false, "some failure")
}

Notes:

  • to perform more complex checks, you can inject dependencies to your probes implementation (ex: database, cache, etc)
  • it is recommended to design your probes with a single responsibility (ex: one for database, one for cache, etc)
Checker

You can create a Checker instance, register your CheckerProbe implementations, and launch checks:

package main

import (
	"context"
	"fmt"

	"path/to/probes"
	"github.com/ankorstore/yokai/healthcheck"
)

func main() {
	ctx := context.Background()

	checker, _ := healthcheck.NewDefaultCheckerFactory().Create(
		healthcheck.WithProbe(probes.NewSuccessProbe()),                       // registers for startup, readiness and liveness
		healthcheck.WithProbe(probes.NewFailureProbe(), healthcheck.Liveness), // registers for liveness only
	)

	// startup health check: invoke only successProbe
	startupResult := checker.Check(ctx, healthcheck.Startup)

	fmt.Printf("startup check success: %v", startupResult.Success) // startup check success: true

	for probeName, probeResult := range startupResult.ProbesResults {
		fmt.Printf("probe name: %s, probe success: %v, probe message: %s", probeName, probeResult.Success, probeResult.Message)
		// probe name: successProbe, probe success: true, probe message: some success
	}

	// liveness health check: invoke successProbe and failureProbe
	livenessResult := checker.Check(ctx, healthcheck.Liveness)

	fmt.Printf("liveness check success: %v", livenessResult.Success) // liveness check success: false

	for probeName, probeResult := range livenessResult.ProbesResults {
		fmt.Printf("probe name: %s, probe success: %v, probe message: %s", probeName, probeResult.Success, probeResult.Message)
		// probe name: successProbe, probe success: true, probe message: some success
		// probe name: failureProbe, probe success: false, probe message: some failure
	}

	// readiness health check: invoke successProbe and failureProbe
	readinessResult := checker.Check(ctx, healthcheck.Readiness)

	fmt.Printf("readiness check success: %v", readinessResult.Success) // readiness check success: false

	for probeName, probeResult := range readinessResult.ProbesResults {
		fmt.Printf("probe name: %s, probe success: %v, probe message: %s", probeName, probeResult.Success, probeResult.Message)
		// probe name: successProbe, probe success: true, probe message: some success
		// probe name: failureProbe, probe success: false, probe message: some failure
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Checker

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

Checker provides the possibility to register several CheckerProbe and execute them.

func NewChecker

func NewChecker() *Checker

NewChecker returns a Checker instance.

func (*Checker) Check

func (c *Checker) Check(ctx context.Context, kind ProbeKind) *CheckerResult

Check executes all the registered probes for a ProbeKind, passes a context.Context to each of them, and returns a CheckerResult. The CheckerResult is successful if all probes executed with success.

func (*Checker) Probes

func (c *Checker) Probes(kinds ...ProbeKind) []CheckerProbe

Probes returns the list of CheckerProbe registered for the provided list of ProbeKind. If no ProbeKind is provided, probes matching all kinds will be returned.

func (*Checker) RegisterProbe

func (c *Checker) RegisterProbe(probe CheckerProbe, kinds ...ProbeKind) *Checker

RegisterProbe registers a CheckerProbe for an optional list of ProbeKind. If no ProbeKind is provided, the CheckerProbe will be registered for all kinds.

type CheckerFactory

type CheckerFactory interface {
	Create(options ...CheckerOption) (*Checker, error)
}

CheckerFactory is the interface for Checker factories.

func NewDefaultCheckerFactory

func NewDefaultCheckerFactory() CheckerFactory

NewDefaultCheckerFactory returns a DefaultCheckerFactory, implementing CheckerFactory.

type CheckerOption

type CheckerOption func(o *Options)

CheckerOption are functional options for the CheckerFactory implementations.

func WithProbe

func WithProbe(probe CheckerProbe, kinds ...ProbeKind) CheckerOption

WithProbe is used to register a CheckerProbe for an optional list of ProbeKind. If no ProbeKind was provided, the CheckerProbe will be registered for all kinds.

type CheckerProbe

type CheckerProbe interface {
	Name() string
	Check(ctx context.Context) *CheckerProbeResult
}

CheckerProbe is the interface for the probes executed by the Checker.

type CheckerProbeRegistration

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

CheckerProbeRegistration represents a registration of a CheckerProbe in the Checker.

func NewCheckerProbeRegistration

func NewCheckerProbeRegistration(probe CheckerProbe, kinds ...ProbeKind) *CheckerProbeRegistration

NewCheckerProbeRegistration returns a CheckerProbeRegistration, and accepts a CheckerProbe and an optional list of ProbeKind. If no ProbeKind is provided, the CheckerProbe will be registered to be executed on all kinds of checks.

func (*CheckerProbeRegistration) Kinds

func (r *CheckerProbeRegistration) Kinds() []ProbeKind

Kinds returns the list of ProbeKind of the CheckerProbeRegistration.

func (*CheckerProbeRegistration) Match

func (r *CheckerProbeRegistration) Match(kinds ...ProbeKind) bool

Match returns true if the CheckerProbeRegistration match any of the provided ProbeKind list.

func (*CheckerProbeRegistration) Probe

Probe returns the CheckerProbe of the CheckerProbeRegistration.

type CheckerProbeResult

type CheckerProbeResult struct {
	Success bool   `json:"success"`
	Message string `json:"message"`
}

CheckerProbeResult is the result of a CheckerProbe execution.

func NewCheckerProbeResult

func NewCheckerProbeResult(success bool, message string) *CheckerProbeResult

NewCheckerProbeResult returns a CheckerProbeResult, with a probe execution status and feedback message.

type CheckerResult

type CheckerResult struct {
	Success       bool                           `json:"success"`
	ProbesResults map[string]*CheckerProbeResult `json:"probes"`
}

CheckerResult is the result of a Checker check. It contains a global status, and a list of CheckerProbeResult corresponding to each probe execution.

type DefaultCheckerFactory

type DefaultCheckerFactory struct{}

DefaultCheckerFactory is the default CheckerFactory implementation.

func (*DefaultCheckerFactory) Create

func (f *DefaultCheckerFactory) Create(options ...CheckerOption) (*Checker, error)

Create returns a new Checker, and accepts a list of CheckerOption. For example:

checker, _ := healthcheck.NewDefaultCheckerFactory().Create(
	healthcheck.WithProbe(NewSomeProbe()),                        // registers for startup, readiness and liveness
	healthcheck.WithProbe(NewOtherProbe(), healthcheck.Liveness), // registers for liveness  only
)

type Options

type Options struct {
	Registrations map[string]*CheckerProbeRegistration
}

Options are options for the CheckerFactory implementations.

func DefaultCheckerOptions

func DefaultCheckerOptions() Options

DefaultCheckerOptions are the default options used in the DefaultCheckerFactory.

type ProbeKind

type ProbeKind int

ProbeKind is an enum for the supported kind of checks.

const (
	Startup ProbeKind = iota
	Liveness
	Readiness
)

func (ProbeKind) String

func (k ProbeKind) String() string

String returns a string representation of the ProbeKind.

Jump to

Keyboard shortcuts

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