Skip to content

Commit

Permalink
Add minor refactoring and documentation for system info
Browse files Browse the repository at this point in the history
  • Loading branch information
alexliesenfeld committed Jul 18, 2023
1 parent 51f74c5 commit d460d1e
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 24 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ func main() {
// Create a new Checker.
checker := health.NewChecker(

// Set service info
health.WithInfo(map[string]string{
"system": "health-example",
"version": "v0.0.8",
"env": "dev",
// Set service information to be included in all check results (refer to the "info" field of the JSON response body).
health.WithInfo(map[string]any{
"version": "v0.0.1",
"environment": "production",
"SHA": "0719f0e2275e07077237ddb55c86ad2e52543555",
}),

// Set the time-to-live for our cache to 1 second (default).
Expand Down
6 changes: 3 additions & 3 deletions check.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
type (
checkerConfig struct {
timeout time.Duration
systemInfo map[string]string
systemInfo map[string]interface{}
checks map[string]*Check
maxErrMsgLen uint
cacheTTL time.Duration
Expand Down Expand Up @@ -85,8 +85,8 @@ type (
// CheckerResult holds the aggregated system availability status and
// detailed information about the individual checks.
CheckerResult struct {
// Info represent a static values about your service
Info map[string]string `json:"info,omitempty"`
// Info contains additional information about this health result.
Info map[string]interface{} `json:"info,omitempty"`
// Status is the aggregated system availability status.
Status AvailabilityStatus `json:"status"`
// Details contains health information for all checked components.
Expand Down
13 changes: 8 additions & 5 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func WithCacheDuration(duration time.Duration) CheckerOption {

// WithCheck adds a new health check that contributes to the overall service availability status.
// This check will be triggered each time Checker.Check is called (i.e., for each HTTP request).
// If health checks are expensive or you expect a bigger amount of requests on your the health endpoint,
// If health checks are expensive, or you expect a higher amount of requests on the health endpoint,
// consider using WithPeriodicCheck instead.
func WithCheck(check Check) CheckerOption {
return func(cfg *checkerConfig) {
Expand All @@ -179,7 +179,7 @@ func WithCheck(check Check) CheckerOption {
// WithPeriodicCheck adds a new health check that contributes to the overall service availability status.
// The health check will be performed on a fixed schedule and will not be executed for each HTTP request
// (as in contrast to WithCheck). This allows to process a much higher number of HTTP requests without
// actually calling the checked services too often or to execute long running checks.
// actually calling the checked services too often or to execute long-running checks.
// This way Checker.Check (and the health endpoint) always returns the last result of the periodic check.
func WithPeriodicCheck(refreshPeriod time.Duration, initialDelay time.Duration, check Check) CheckerOption {
return func(cfg *checkerConfig) {
Expand All @@ -198,9 +198,12 @@ func WithInterceptors(interceptors ...Interceptor) CheckerOption {
}
}

// WithInfo sets values that will be displayed in the "info" property of the response payload
// you can use this option if you want to set information about your system or something like that
func WithInfo(values map[string]string) CheckerOption {
// WithInfo sets values that will be available in every health check result. For example, you can use this option
// if you want to set information about your system that will be returned in every health check result, such as
// version number, Git SHA, build date, etc. These values will be available in CheckerResult.Info. If you use the
// default HTTP handler of this library (see NewHandler) or convert the CheckerResult to JSON on your own,
// these values will be available in the "info" field.
func WithInfo(values map[string]interface{}) CheckerOption {
return func(cfg *checkerConfig) {
cfg.systemInfo = values
}
Expand Down
2 changes: 1 addition & 1 deletion examples/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/alexliesenfeld/health/examples

go 1.13
go 1.18

require (
github.com/InVisionApp/go-health v2.1.0+incompatible
Expand Down
4 changes: 2 additions & 2 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
Expand Down
8 changes: 7 additions & 1 deletion examples/showcase/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ func main() {
// call and do some pre- and post-processing, having the check state and check function result at hand.
health.WithInterceptors(interceptors.BasicLogger()),

// Set service info that will be included in all check results.
health.WithInfo(map[string]any{
"version": "v0.0.8",
"environment": "production",
}),

// A simple successFunc to see if a fake database connection is up.
health.WithCheck(health.Check{
// Each check gets a unique name.
Expand Down Expand Up @@ -125,7 +131,7 @@ func main() {
// We Create a new http.Handler that provides health successFunc information
// serialized as a JSON string via HTTP.
http.Handle("/health", handler)
http.ListenAndServe(":3000", nil)
http.ListenAndServe(":8001", nil)
}

func onComponentStatusChanged(_ context.Context, name string, state health.CheckState) {
Expand Down
15 changes: 8 additions & 7 deletions examples/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,20 @@ func main() {
// Create a new Checker.
checker := health.NewChecker(

// Set service info
health.WithInfo(map[string]string{
"system": "health-example",
"version": "v0.0.8",
"env": "dev",
}),

// Set the time-to-live for our cache to 1 second (default).
health.WithCacheDuration(1*time.Second),

// Configure a global timeout that will be applied to all checks.
health.WithTimeout(10*time.Second),

// Set service info that will be included in all check results.
// Will be available in the "info" field of the JSON response body.
health.WithInfo(map[string]any{
"version": "v0.0.1",
"environment": "production",
"SHA": "0719f0e2275e07077237ddb55c86ad2e52543555",
}),

// A check configuration to see if our database connection is up.
// The check function will be executed for each HTTP request.
health.WithCheck(health.Check{
Expand Down

0 comments on commit d460d1e

Please sign in to comment.