Skip to content

Commit

Permalink
feat: add info option (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolascb authored Jul 18, 2023
1 parent 952b703 commit 51f74c5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ 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),

Expand Down
5 changes: 4 additions & 1 deletion check.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
type (
checkerConfig struct {
timeout time.Duration
systemInfo map[string]string
checks map[string]*Check
maxErrMsgLen uint
cacheTTL time.Duration
Expand Down Expand Up @@ -84,6 +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"`
// Status is the aggregated system availability status.
Status AvailabilityStatus `json:"status"`
// Details contains health information for all checked components.
Expand Down Expand Up @@ -312,7 +315,7 @@ func (ck *defaultChecker) mapStateToCheckerResult() CheckerResult {
}
}

return CheckerResult{Status: status, Details: checkResults}
return CheckerResult{Status: status, Details: checkResults, Info: ck.cfg.systemInfo}
}

func isCacheExpired(cacheDuration time.Duration, state *CheckState) bool {
Expand Down
8 changes: 8 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,11 @@ func WithInterceptors(interceptors ...Interceptor) CheckerOption {
cfg.interceptors = interceptors
}
}

// 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 {
return func(cfg *checkerConfig) {
cfg.systemInfo = values
}
}
7 changes: 7 additions & 0 deletions examples/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ 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),

Expand Down

0 comments on commit 51f74c5

Please sign in to comment.