Documentation
¶
Overview ¶
Package health provides a unified health check toolkit for Go services. It includes health check interfaces, probe implementations, multi-probe aggregation, and HTTP handlers compatible with both Fiber and net/http.
Index ¶
- func FiberHandler(aggregator *Aggregator) fiber.Handler
- func FiberLivenessHandler(serviceName string) fiber.Handler
- func FiberReadinessHandler(aggregator *Aggregator) fiber.Handler
- func HTTPStatusCode(status Status) int
- func Handler(aggregator *Aggregator) http.HandlerFunc
- func LivenessHandler(serviceName string) http.HandlerFunc
- func ReadinessHandler(aggregator *Aggregator) http.HandlerFunc
- func SimpleFiberHandler(serviceName string) fiber.Handler
- func SimpleHandler(serviceName string) http.HandlerFunc
- type AggregatedResult
- type Aggregator
- func (a *Aggregator) AddChecker(checker Checker) *Aggregator
- func (a *Aggregator) AddCheckers(checkers ...Checker) *Aggregator
- func (a *Aggregator) Check(ctx context.Context) AggregatedResult
- func (a *Aggregator) CheckSequential(ctx context.Context) AggregatedResult
- func (a *Aggregator) Config() Config
- func (a *Aggregator) GetCheckerNames() []string
- func (a *Aggregator) RemoveChecker(name string) *Aggregator
- func (a *Aggregator) SetConfig(config Config)
- type CheckResult
- type Checker
- type CheckerFunc
- type Config
- func (c *Config) IsCritical(name string) bool
- func (c *Config) IsIPAllowed(ipStr string) bool
- func (c *Config) IsTrustedProxy(ipStr string) bool
- func (c Config) WithChecks(include bool) Config
- func (c Config) WithCriticalChecks(checks []string) Config
- func (c Config) WithDetails(include bool) Config
- func (c Config) WithIPWhitelist(ips []string) Config
- func (c Config) WithServiceName(name string) Config
- func (c Config) WithTimeout(timeout time.Duration) Config
- func (c Config) WithTrustedProxies(ips []string) Config
- type CustomChecker
- type DBChecker
- type DisabledChecker
- type HTTPChecker
- func (c *HTTPChecker) Check(ctx context.Context) CheckResult
- func (c *HTTPChecker) Name() string
- func (c *HTTPChecker) WithClient(client *http.Client) *HTTPChecker
- func (c *HTTPChecker) WithExpectedCode(code int) *HTTPChecker
- func (c *HTTPChecker) WithMethod(method string) *HTTPChecker
- func (c *HTTPChecker) WithTimeout(timeout time.Duration) *HTTPChecker
- type RedisChecker
- type Status
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FiberHandler ¶
func FiberHandler(aggregator *Aggregator) fiber.Handler
FiberHandler returns a Fiber handler for health checks
func FiberLivenessHandler ¶
FiberLivenessHandler returns a simple Fiber liveness check handler
func FiberReadinessHandler ¶
func FiberReadinessHandler(aggregator *Aggregator) fiber.Handler
FiberReadinessHandler returns a Fiber readiness check handler
func HTTPStatusCode ¶
HTTPStatusCode returns the appropriate HTTP status code for a health status
func Handler ¶
func Handler(aggregator *Aggregator) http.HandlerFunc
Handler returns a standard library HTTP handler for health checks
func LivenessHandler ¶
func LivenessHandler(serviceName string) http.HandlerFunc
LivenessHandler returns a simple liveness check handler (for Kubernetes) Always returns 200 OK if the service is running
func ReadinessHandler ¶
func ReadinessHandler(aggregator *Aggregator) http.HandlerFunc
ReadinessHandler returns a readiness check handler (for Kubernetes) Returns 200 OK only if all critical checks pass
func SimpleFiberHandler ¶
SimpleFiberHandler returns a minimal Fiber health check handler
func SimpleHandler ¶
func SimpleHandler(serviceName string) http.HandlerFunc
SimpleHandler returns a minimal health check handler without aggregator Useful for simple services that just need to report they're running
Types ¶
type AggregatedResult ¶
type AggregatedResult struct {
// Status is the overall health status
Status Status `json:"status"`
// Service is the name of the service
Service string `json:"service"`
// Checks contains individual check results
Checks map[string]CheckResult `json:"checks,omitempty"`
// Timestamp is when the aggregation was performed
Timestamp time.Time `json:"timestamp"`
// TotalLatency is the total time taken for all checks
TotalLatency time.Duration `json:"-"`
}
AggregatedResult represents the combined result of multiple health checks
func (AggregatedResult) IsDegraded ¶
func (r AggregatedResult) IsDegraded() bool
IsDegraded returns true if any check failed but service is still functional
func (AggregatedResult) IsHealthy ¶
func (r AggregatedResult) IsHealthy() bool
IsHealthy returns true if the overall status is healthy
func (AggregatedResult) MarshalJSON ¶
func (r AggregatedResult) MarshalJSON() ([]byte, error)
MarshalJSON customizes JSON marshaling for AggregatedResult
type Aggregator ¶
type Aggregator struct {
// contains filtered or unexported fields
}
Aggregator manages multiple health checkers and aggregates their results
func NewAggregator ¶
func NewAggregator(config Config) *Aggregator
NewAggregator creates a new health check aggregator
func (*Aggregator) AddChecker ¶
func (a *Aggregator) AddChecker(checker Checker) *Aggregator
AddChecker adds a health checker to the aggregator
func (*Aggregator) AddCheckers ¶
func (a *Aggregator) AddCheckers(checkers ...Checker) *Aggregator
AddCheckers adds multiple health checkers to the aggregator
func (*Aggregator) Check ¶
func (a *Aggregator) Check(ctx context.Context) AggregatedResult
Check performs all health checks in parallel and aggregates the results
func (*Aggregator) CheckSequential ¶
func (a *Aggregator) CheckSequential(ctx context.Context) AggregatedResult
CheckSequential performs all health checks sequentially Useful when parallel execution might cause issues
func (*Aggregator) Config ¶
func (a *Aggregator) Config() Config
Config returns the current configuration
func (*Aggregator) GetCheckerNames ¶
func (a *Aggregator) GetCheckerNames() []string
GetCheckerNames returns the names of all registered checkers
func (*Aggregator) RemoveChecker ¶
func (a *Aggregator) RemoveChecker(name string) *Aggregator
RemoveChecker removes a checker by name
func (*Aggregator) SetConfig ¶
func (a *Aggregator) SetConfig(config Config)
SetConfig updates the configuration
type CheckResult ¶
type CheckResult struct {
// Name is the identifier of the checked component
Name string `json:"name"`
// Status is the health status
Status Status `json:"status"`
// Latency is the time taken to perform the check
Latency time.Duration `json:"-"`
// Error contains error details if the check failed
Error string `json:"error,omitempty"`
// Message contains additional information
Message string `json:"message,omitempty"`
// Timestamp is when the check was performed
Timestamp time.Time `json:"timestamp"`
// Metadata contains additional check-specific data
Metadata map[string]any `json:"metadata,omitempty"`
}
CheckResult represents the result of a single health check
func (CheckResult) MarshalJSON ¶
func (r CheckResult) MarshalJSON() ([]byte, error)
MarshalJSON customizes JSON marshaling for CheckResult
type Checker ¶
type Checker interface {
// Name returns the name of the checker
Name() string
// Check performs the health check and returns the result
Check(ctx context.Context) CheckResult
}
Checker is the interface that health check probes must implement
type CheckerFunc ¶
type CheckerFunc struct {
// contains filtered or unexported fields
}
CheckerFunc is a function adapter for Checker interface
func NewCheckerFunc ¶
func NewCheckerFunc(name string, fn func(ctx context.Context) CheckResult) *CheckerFunc
NewCheckerFunc creates a new CheckerFunc with the given name and function
func (*CheckerFunc) Check ¶
func (c *CheckerFunc) Check(ctx context.Context) CheckResult
Check performs the health check
type Config ¶
type Config struct {
// ServiceName is the name of the service for identification
ServiceName string
// Timeout is the default timeout for health checks
Timeout time.Duration
// IPWhitelist is a list of IP addresses/CIDRs allowed to access health endpoints
// If empty, all IPs are allowed
IPWhitelist []string
// TrustedProxies is a list of proxy IPs/CIDRs that are allowed to supply
// X-Forwarded-For or X-Real-IP headers.
TrustedProxies []string
// IncludeDetails controls whether to include detailed check results in response
// Set to false in production to hide internal details
IncludeDetails bool
// IncludeChecks controls whether to include individual check results
IncludeChecks bool
// CriticalChecks is a list of check names that are critical
// If any critical check fails, the overall status is unhealthy
// Non-critical check failures result in degraded status
CriticalChecks []string
// contains filtered or unexported fields
}
Config holds the configuration for health check handlers
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a Config with sensible defaults
func (*Config) IsCritical ¶
IsCritical checks if a check name is in the critical list
func (*Config) IsIPAllowed ¶
IsIPAllowed checks if the given IP is allowed by the whitelist Returns true if whitelist is empty (all IPs allowed)
func (*Config) IsTrustedProxy ¶ added in v1.1.0
IsTrustedProxy checks if the given IP belongs to a trusted proxy list
func (Config) WithChecks ¶
WithChecks sets whether to include individual checks
func (Config) WithCriticalChecks ¶
WithCriticalChecks sets the list of critical checks
func (Config) WithDetails ¶
WithDetails sets whether to include details
func (Config) WithIPWhitelist ¶
WithIPWhitelist sets the IP whitelist
func (Config) WithServiceName ¶
WithServiceName sets the service name
func (Config) WithTimeout ¶
WithTimeout sets the timeout
func (Config) WithTrustedProxies ¶ added in v1.1.0
WithTrustedProxies sets the trusted proxies list
type CustomChecker ¶
type CustomChecker struct {
// contains filtered or unexported fields
}
CustomChecker allows creating health checkers from functions
func NewCustomChecker ¶
func NewCustomChecker(name string, checkFn func(ctx context.Context) error) *CustomChecker
NewCustomChecker creates a new custom health checker
func (*CustomChecker) Check ¶
func (c *CustomChecker) Check(ctx context.Context) CheckResult
Check performs the custom health check
func (*CustomChecker) WithMetadata ¶
func (c *CustomChecker) WithMetadata(metadata map[string]any) *CustomChecker
WithMetadata sets static metadata for the check result
func (*CustomChecker) WithTimeout ¶
func (c *CustomChecker) WithTimeout(timeout time.Duration) *CustomChecker
WithTimeout sets the timeout for custom checks
type DBChecker ¶
type DBChecker struct {
// contains filtered or unexported fields
}
DBChecker checks database connectivity
func NewDBChecker ¶
NewDBChecker creates a new database health checker
func NewDBCheckerWithName ¶
NewDBCheckerWithName creates a new database health checker with custom name
type DisabledChecker ¶
type DisabledChecker struct {
// contains filtered or unexported fields
}
DisabledChecker always returns disabled status Useful for optional dependencies that are not configured
func NewDisabledChecker ¶
func NewDisabledChecker(name string) *DisabledChecker
NewDisabledChecker creates a new disabled checker
func (*DisabledChecker) Check ¶
func (c *DisabledChecker) Check(_ context.Context) CheckResult
Check returns a disabled status
func (*DisabledChecker) Name ¶
func (c *DisabledChecker) Name() string
Name returns the checker name
func (*DisabledChecker) WithMessage ¶
func (c *DisabledChecker) WithMessage(message string) *DisabledChecker
WithMessage sets the message for the disabled status
type HTTPChecker ¶
type HTTPChecker struct {
// contains filtered or unexported fields
}
HTTPChecker checks HTTP endpoint availability
func NewHTTPChecker ¶
func NewHTTPChecker(name, url string) *HTTPChecker
NewHTTPChecker creates a new HTTP health checker
func (*HTTPChecker) Check ¶
func (c *HTTPChecker) Check(ctx context.Context) CheckResult
Check performs the HTTP health check
func (*HTTPChecker) WithClient ¶
func (c *HTTPChecker) WithClient(client *http.Client) *HTTPChecker
WithClient sets a custom HTTP client
func (*HTTPChecker) WithExpectedCode ¶
func (c *HTTPChecker) WithExpectedCode(code int) *HTTPChecker
WithExpectedCode sets the expected HTTP status code
func (*HTTPChecker) WithMethod ¶
func (c *HTTPChecker) WithMethod(method string) *HTTPChecker
WithMethod sets the HTTP method
func (*HTTPChecker) WithTimeout ¶
func (c *HTTPChecker) WithTimeout(timeout time.Duration) *HTTPChecker
WithTimeout sets the timeout for HTTP checks
type RedisChecker ¶
type RedisChecker struct {
// contains filtered or unexported fields
}
RedisChecker checks Redis connectivity
func NewRedisChecker ¶
func NewRedisChecker(client *redis.Client) *RedisChecker
NewRedisChecker creates a new Redis health checker
func NewRedisCheckerWithName ¶
func NewRedisCheckerWithName(name string, client *redis.Client) *RedisChecker
NewRedisCheckerWithName creates a new Redis health checker with custom name
func (*RedisChecker) Check ¶
func (c *RedisChecker) Check(ctx context.Context) CheckResult
Check performs the Redis health check
func (*RedisChecker) WithTimeout ¶
func (c *RedisChecker) WithTimeout(timeout time.Duration) *RedisChecker
WithTimeout sets the timeout for Redis checks
type Status ¶
type Status string
Status represents the health status of a component
const ( // StatusHealthy indicates the component is healthy StatusHealthy Status = "ok" // StatusUnhealthy indicates the component is unhealthy StatusUnhealthy Status = "unhealthy" // StatusDegraded indicates the component is partially healthy StatusDegraded Status = "degraded" // StatusDisabled indicates the component is disabled StatusDisabled Status = "disabled" // StatusUnknown indicates the component status cannot be determined StatusUnknown Status = "unknown" )