|
| 1 | +# Fx Health Check Module |
| 2 | + |
| 3 | +[](https://github.com/ankorstore/yokai/actions/workflows/fxhealthcheck-ci.yml) |
| 4 | +[](https://goreportcard.com/report/github.com/ankorstore/yokai/fxhealthcheck) |
| 5 | +[](https://app.codecov.io/gh/ankorstore/yokai/tree/main/fxhealthcheck) |
| 6 | +[](https://deps.dev/go/github.com%2Fankorstore%2Fyokai%2Ffxhealthcheck) |
| 7 | +[](https://pkg.go.dev/github.com/ankorstore/yokai/fxhealthcheck) |
| 8 | + |
| 9 | +> [Fx](https://uber-go.github.io/fx/) module for [healthcheck](https://github.com/ankorstore/yokai/tree/main/healthcheck). |
| 10 | +
|
| 11 | +<!-- TOC --> |
| 12 | + |
| 13 | +* [Installation](#installation) |
| 14 | +* [Documentation](#documentation) |
| 15 | + * [Loading](#loading) |
| 16 | + * [Registration](#registration) |
| 17 | + * [Override](#override) |
| 18 | + |
| 19 | +<!-- TOC --> |
| 20 | + |
| 21 | +## Installation |
| 22 | + |
| 23 | +```shell |
| 24 | +go get github.com/ankorstore/yokai/fxhealthcheck |
| 25 | +``` |
| 26 | + |
| 27 | +## Documentation |
| 28 | + |
| 29 | +### Loading |
| 30 | + |
| 31 | +To load the module in your Fx application: |
| 32 | + |
| 33 | +```go |
| 34 | +package main |
| 35 | + |
| 36 | +import ( |
| 37 | + "context" |
| 38 | + "fmt" |
| 39 | + |
| 40 | + "github.com/ankorstore/yokai/fxhealthcheck" |
| 41 | + "github.com/ankorstore/yokai/healthcheck" |
| 42 | + "go.uber.org/fx" |
| 43 | +) |
| 44 | + |
| 45 | +func main() { |
| 46 | + fx.New( |
| 47 | + fxhealthcheck.FxHealthcheckModule, // load the module |
| 48 | + fx.Invoke(func(checker *healthcheck.Checker) { // invoke the checker for liveness checks |
| 49 | + fmt.Printf("checker result: %v", checker.Check(context.Background(), healthcheck.Liveness)) |
| 50 | + }), |
| 51 | + ).Run() |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +### Registration |
| 56 | + |
| 57 | +This module provides the possibility to register |
| 58 | +several [CheckerProbe](https://github.com/ankorstore/yokai/blob/main/healthcheck/probe.go) implementations, and organise |
| 59 | +them for `startup`, `liveness` and / |
| 60 | +or `readiness` [checks](https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/). |
| 61 | + |
| 62 | +They will be then collected and given by Fx to |
| 63 | +the [Checker](https://github.com/ankorstore/yokai/blob/main/healthcheck/checker.go), made available in the Fx container. |
| 64 | + |
| 65 | +This is done via the `AsCheckerProbe()` function: |
| 66 | + |
| 67 | +```go |
| 68 | +package main |
| 69 | + |
| 70 | +import ( |
| 71 | + "context" |
| 72 | + "fmt" |
| 73 | + |
| 74 | + "github.com/ankorstore/yokai/fxhealthcheck" |
| 75 | + "github.com/ankorstore/yokai/healthcheck" |
| 76 | + "go.uber.org/fx" |
| 77 | +) |
| 78 | + |
| 79 | +// example success probe |
| 80 | +type SuccessProbe struct{} |
| 81 | + |
| 82 | +func NewSuccessProbe() *SuccessProbe { |
| 83 | + return &SuccessProbe{} |
| 84 | +} |
| 85 | + |
| 86 | +func (p *SuccessProbe) Name() string { |
| 87 | + return "successProbe" |
| 88 | +} |
| 89 | + |
| 90 | +func (p *SuccessProbe) Check(ctx context.Context) *healthcheck.CheckerProbeResult { |
| 91 | + return healthcheck.NewCheckerProbeResult(true, "some success") |
| 92 | +} |
| 93 | + |
| 94 | +// example failure probe |
| 95 | +type FailureProbe struct{} |
| 96 | + |
| 97 | +func NewFailureProbe() *FailureProbe { |
| 98 | + return &FailureProbe{} |
| 99 | +} |
| 100 | + |
| 101 | +func (p *FailureProbe) Name() string { |
| 102 | + return "someProbe" |
| 103 | +} |
| 104 | + |
| 105 | +func (p *FailureProbe) Check(ctx context.Context) *healthcheck.CheckerProbeResult { |
| 106 | + return healthcheck.NewCheckerProbeResult(false, "some failure") |
| 107 | +} |
| 108 | + |
| 109 | +// usage |
| 110 | +func main() { |
| 111 | + fx.New( |
| 112 | + fxhealthcheck.FxHealthcheckModule, // load the module |
| 113 | + fx.Provide( |
| 114 | + fxhealthcheck.AsCheckerProbe(NewSuccessProbe), // register the SuccessProbe probe for startup, liveness and readiness checks |
| 115 | + fxhealthcheck.AsCheckerProbe(NewFailureProbe, healthcheck.Liveness), // register the FailureProbe probe for liveness checks only |
| 116 | + ), |
| 117 | + fx.Invoke(func(checker *healthcheck.Checker) { // invoke the checker |
| 118 | + ctx := context.Background() |
| 119 | + |
| 120 | + fmt.Printf("startup: %v", checker.Check(ctx, healthcheck.Startup).Success) // startup: true |
| 121 | + fmt.Printf("liveness: %v", checker.Check(ctx, healthcheck.Liveness).Success) // liveness: false |
| 122 | + fmt.Printf("readiness: %v", checker.Check(ctx, healthcheck.Readiness).Success) // readiness: true |
| 123 | + }), |
| 124 | + ).Run() |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +### Override |
| 129 | + |
| 130 | +By default, the `healthcheck.Checker` is created by |
| 131 | +the [DefaultCheckerFactory](https://github.com/ankorstore/yokai/blob/main/healthcheck/factory.go). |
| 132 | + |
| 133 | +If needed, you can provide your own factory and override the module: |
| 134 | + |
| 135 | +```go |
| 136 | +package main |
| 137 | + |
| 138 | +import ( |
| 139 | + "context" |
| 140 | + |
| 141 | + "github.com/ankorstore/yokai/fxhealthcheck" |
| 142 | + "github.com/ankorstore/yokai/healthcheck" |
| 143 | + "go.uber.org/fx" |
| 144 | +) |
| 145 | + |
| 146 | +type CustomCheckerFactory struct{} |
| 147 | + |
| 148 | +func NewCustomCheckerFactory() healthcheck.CheckerFactory { |
| 149 | + return &CustomCheckerFactory{} |
| 150 | +} |
| 151 | + |
| 152 | +func (f *CustomCheckerFactory) Create(options ...healthcheck.CheckerOption) (*healthcheck.Checker, error) { |
| 153 | + return &healthcheck.Checker{...}, nil |
| 154 | +} |
| 155 | + |
| 156 | +func main() { |
| 157 | + fx.New( |
| 158 | + fxhealthcheck.FxHealthcheckModule, // load the module |
| 159 | + fx.Decorate(NewCustomCheckerFactory), // override the module with a custom factory |
| 160 | + fx.Invoke(func(checker *healthcheck.Checker) { // invoke the custom checker for readiness checks |
| 161 | + checker.Check(context.Background(), healthcheck.Readiness) |
| 162 | + }), |
| 163 | + ).Run() |
| 164 | +} |
| 165 | +``` |
0 commit comments