fxhealthcheck

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2024 License: MIT Imports: 4 Imported by: 1

README

Fx Health Check Module

ci go report codecov Deps PkgGoDev

Fx module for healthcheck.

Installation

go get github.com/ankorstore/yokai/fxhealthcheck

Documentation

Loading

To load the module in your Fx application:

package main

import (
	"context"
	"fmt"

	"github.com/ankorstore/yokai/fxhealthcheck"
	"github.com/ankorstore/yokai/healthcheck"
	"go.uber.org/fx"
)

func main() {
	fx.New(
		fxhealthcheck.FxHealthcheckModule,             // load the module
		fx.Invoke(func(checker *healthcheck.Checker) { // invoke the checker for liveness checks
			fmt.Printf("checker result: %v", checker.Check(context.Background(), healthcheck.Liveness))
		}),
	).Run()
}
Registration

This module provides the possibility to register several CheckerProbe implementations, and organise them for startup, liveness and / or readiness checks.

They will be then collected and given by Fx to the Checker, made available in the Fx container.

This is done via the AsCheckerProbe() function:

package main

import (
	"context"
	"fmt"

	"github.com/ankorstore/yokai/fxhealthcheck"
	"github.com/ankorstore/yokai/healthcheck"
	"go.uber.org/fx"
)

// example 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")
}

// example failure probe
type FailureProbe struct{}

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

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

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

// usage
func main() {
	fx.New(
		fxhealthcheck.FxHealthcheckModule, // load the module
		fx.Provide(
			fxhealthcheck.AsCheckerProbe(NewSuccessProbe),                       // register the SuccessProbe probe for startup, liveness and readiness checks
			fxhealthcheck.AsCheckerProbe(NewFailureProbe, healthcheck.Liveness), // register the FailureProbe probe for liveness checks only
		),
		fx.Invoke(func(checker *healthcheck.Checker) { // invoke the checker
			ctx := context.Background()

			fmt.Printf("startup: %v", checker.Check(ctx, healthcheck.Startup).Success)     // startup: true
			fmt.Printf("liveness: %v", checker.Check(ctx, healthcheck.Liveness).Success)   // liveness: false
			fmt.Printf("readiness: %v", checker.Check(ctx, healthcheck.Readiness).Success) // readiness: true
		}),
	).Run()
}
Override

By default, the healthcheck.Checker is created by the DefaultCheckerFactory.

If needed, you can provide your own factory and override the module:

package main

import (
	"context"

	"github.com/ankorstore/yokai/fxhealthcheck"
	"github.com/ankorstore/yokai/healthcheck"
	"go.uber.org/fx"
)

type CustomCheckerFactory struct{}

func NewCustomCheckerFactory() healthcheck.CheckerFactory {
	return &CustomCheckerFactory{}
}

func (f *CustomCheckerFactory) Create(options ...healthcheck.CheckerOption) (*healthcheck.Checker, error) {
	return &healthcheck.Checker{...}, nil
}

func main() {
	fx.New(
		fxhealthcheck.FxHealthcheckModule,             // load the module
		fx.Decorate(NewCustomCheckerFactory),          // override the module with a custom factory
		fx.Invoke(func(checker *healthcheck.Checker) { // invoke the custom checker for readiness checks
			checker.Check(context.Background(), healthcheck.Readiness)
		}),
	).Run()
}

Documentation

Index

Constants

View Source
const ModuleName = "healthcheck"

ModuleName is the module name.

Variables

FxHealthcheckModule is the Fx healthcheck module.

Functions

func AsCheckerProbe

func AsCheckerProbe(p any, kinds ...healthcheck.ProbeKind) fx.Option

AsCheckerProbe registers a healthcheck.CheckerProbe into Fx.

func GetReturnType

func GetReturnType(target any) string

GetReturnType returns the return type of a target.

func GetType

func GetType(target any) string

GetType returns the type of a target.

func NewFxChecker

func NewFxChecker(p FxCheckerParam) (*healthcheck.Checker, error)

NewFxChecker returns a new healthcheck.Checker.

Types

type CheckerProbeDefinition

type CheckerProbeDefinition interface {
	ReturnType() string
	Kinds() []healthcheck.ProbeKind
}

CheckerProbeDefinition is the interface for probes definitions.

func NewCheckerProbeDefinition

func NewCheckerProbeDefinition(returnType string, kinds ...healthcheck.ProbeKind) CheckerProbeDefinition

NewCheckerProbeDefinition returns a new CheckerProbeDefinition.

type CheckerProbeRegistry

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

CheckerProbeRegistry is the registry collecting probes and their definitions.

func NewFxCheckerProbeRegistry

func NewFxCheckerProbeRegistry(p FxCheckerProbeRegistryParam) *CheckerProbeRegistry

NewFxCheckerProbeRegistry returns as new CheckerProbeRegistry.

func (*CheckerProbeRegistry) ResolveCheckerProbesRegistrations

func (r *CheckerProbeRegistry) ResolveCheckerProbesRegistrations() ([]*healthcheck.CheckerProbeRegistration, error)

ResolveCheckerProbesRegistrations resolves healthcheck.CheckerProbeRegistration from their definitions.

type FxCheckerParam

type FxCheckerParam struct {
	fx.In
	Factory  healthcheck.CheckerFactory
	Registry *CheckerProbeRegistry
}

FxCheckerParam allows injection of the required dependencies in NewFxChecker.

type FxCheckerProbeRegistryParam

type FxCheckerProbeRegistryParam struct {
	fx.In
	Probes      []healthcheck.CheckerProbe `group:"healthcheck-probes"`
	Definitions []CheckerProbeDefinition   `group:"healthcheck-probes-definitions"`
}

FxCheckerProbeRegistryParam allows injection of the required dependencies in NewFxCheckerProbeRegistry.

Jump to

Keyboard shortcuts

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