Documentation
¶
Overview ¶
Package health provides health utilities.
Index ¶
- func HealthStatusFromContext(ctx context.Context) (ports.HealthStatus, bool)
- func HealthTimestampFromContext(ctx context.Context) (time.Time, bool)
- func New() ports.HealthManager
- func NewBasicChecker() ports.HealthChecker
- func NewCompositeChecker(name string, checkers ...ports.HealthChecker) ports.HealthChecker
- func NewCustomChecker(name string, ...) ports.HealthChecker
- func NewCustomCheckerWithTimeout(name string, timeout time.Duration, ...) ports.HealthChecker
- func NewDatabaseChecker(pool ports.DatabasePool) ports.HealthChecker
- func NewHTTPChecker(name, url string, opts ...HTTPCheckerOption) ports.HealthChecker
- func NewMemoryChecker(maxMemoryMB int64) ports.HealthChecker
- func NewPaymentProviderChecker(provider ports.PaymentProvider, opts ...PaymentProviderCheckerOption) ports.HealthChecker
- func NewWithConfig(config ports.HealthCheckConfig) ports.HealthManager
- type BasicChecker
- type CompositeChecker
- type Config
- type CustomChecker
- type DatabaseChecker
- type DetailedHealthResponse
- type DurationLoader
- type HTTPChecker
- type HTTPCheckerOption
- func WithHTTPClient(client *http.Client) HTTPCheckerOption
- func WithHTTPFailureStatus(status ports.HealthStatus) HTTPCheckerOption
- func WithHTTPHeader(key, value string) HTTPCheckerOption
- func WithHTTPHeaders(headers map[string]string) HTTPCheckerOption
- func WithHTTPMethod(method string) HTTPCheckerOption
- func WithHTTPSuccessStatuses(statuses ...int) HTTPCheckerOption
- func WithHTTPTimeout(timeout time.Duration) HTTPCheckerOption
- type Handler
- func (h *Handler) DetailedHealthHandler(w http.ResponseWriter, r *http.Request)
- func (h *Handler) HealthHandler(w http.ResponseWriter, r *http.Request)
- func (h *Handler) LivenessHandler(w http.ResponseWriter, r *http.Request)
- func (h *Handler) Middleware() func(http.Handler) http.Handler
- func (h *Handler) ReadinessHandler(w http.ResponseWriter, r *http.Request)
- func (h *Handler) RegisterCustomRoutes(router interface{ ... }, paths HealthPaths)
- func (h *Handler) RegisterRoutes(router interface{ ... })
- type HealthPaths
- type HealthResponse
- type HealthResult
- type HealthSummary
- type Manager
- func (m *Manager) GetDetailedHealth(ctx context.Context) ports.DetailedHealthResponse
- func (m *Manager) GetHealth(ctx context.Context) ports.HealthResponse
- func (m *Manager) GetLiveness(ctx context.Context) ports.HealthResult
- func (m *Manager) GetReadiness(ctx context.Context) ports.HealthResult
- func (m *Manager) RefreshAll(ctx context.Context) ports.DetailedHealthResponse
- func (m *Manager) RegisterChecker(checker ports.HealthChecker)
- func (m *Manager) RegisterCheckers(checkers ...ports.HealthChecker)
- type MemoryChecker
- type PaymentProviderChecker
- type PaymentProviderCheckerOption
- type RefreshManager
- type Scheduler
- type SchedulerConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HealthStatusFromContext ¶
func HealthStatusFromContext(ctx context.Context) (ports.HealthStatus, bool)
HealthStatusFromContext extracts health status from request context.
func HealthTimestampFromContext ¶
HealthTimestampFromContext extracts health timestamp from request context.
func New ¶
func New() ports.HealthManager
New creates a new health manager with default configuration.
func NewBasicChecker ¶
func NewBasicChecker() ports.HealthChecker
NewBasicChecker returns a health checker that always reports healthy.
func NewCompositeChecker ¶
func NewCompositeChecker(name string, checkers ...ports.HealthChecker) ports.HealthChecker
NewCompositeChecker returns a checker that aggregates other checkers.
func NewCustomChecker ¶
func NewCustomChecker(name string, checkFunc func(ctx context.Context) (ports.HealthStatus, string, interface{})) ports.HealthChecker
NewCustomChecker returns a checker that calls the provided function.
func NewCustomCheckerWithTimeout ¶
func NewCustomCheckerWithTimeout(name string, timeout time.Duration, checkFunc func(ctx context.Context) (ports.HealthStatus, string, interface{})) ports.HealthChecker
NewCustomCheckerWithTimeout returns a checker with a custom timeout.
func NewDatabaseChecker ¶
func NewDatabaseChecker(pool ports.DatabasePool) ports.HealthChecker
NewDatabaseChecker returns a database ping health checker.
func NewHTTPChecker ¶
func NewHTTPChecker(name, url string, opts ...HTTPCheckerOption) ports.HealthChecker
NewHTTPChecker returns a checker that probes an HTTP endpoint.
func NewMemoryChecker ¶
func NewMemoryChecker(maxMemoryMB int64) ports.HealthChecker
NewMemoryChecker returns a memory usage health checker.
func NewPaymentProviderChecker ¶
func NewPaymentProviderChecker(provider ports.PaymentProvider, opts ...PaymentProviderCheckerOption) ports.HealthChecker
NewPaymentProviderChecker returns a checker for payment providers.
func NewWithConfig ¶
func NewWithConfig(config ports.HealthCheckConfig) ports.HealthManager
NewWithConfig creates a new health manager with custom configuration.
Types ¶
type BasicChecker ¶
type BasicChecker struct{}
BasicChecker implements a basic health check that always returns healthy.
func (*BasicChecker) Check ¶
func (c *BasicChecker) Check(_ context.Context) ports.HealthResult
Check runs the basic health check.
type CompositeChecker ¶
type CompositeChecker struct {
// contains filtered or unexported fields
}
CompositeChecker implements a composite health check that combines multiple checks.
func (*CompositeChecker) Check ¶
func (c *CompositeChecker) Check(ctx context.Context) ports.HealthResult
Check runs the composite health check.
func (*CompositeChecker) Name ¶
func (c *CompositeChecker) Name() string
Name returns the checker name.
type Config ¶
Config describes periodic health refresh settings.
func LoadConfig ¶
func LoadConfig(loader DurationLoader) Config
LoadConfig reads health cache config from environment.
type CustomChecker ¶
type CustomChecker struct {
// contains filtered or unexported fields
}
CustomChecker implements a custom health check with a function.
func (*CustomChecker) Check ¶
func (c *CustomChecker) Check(ctx context.Context) ports.HealthResult
Check runs the custom health check.
type DatabaseChecker ¶
type DatabaseChecker struct {
// contains filtered or unexported fields
}
DatabaseChecker implements a database health check.
func (*DatabaseChecker) Check ¶
func (c *DatabaseChecker) Check(ctx context.Context) ports.HealthResult
Check runs the database health check.
func (*DatabaseChecker) Name ¶
func (c *DatabaseChecker) Name() string
Name returns the checker name.
type DetailedHealthResponse ¶
type DetailedHealthResponse struct {
Status string `json:"status" example:"healthy"`
Timestamp time.Time `json:"timestamp"`
Checks map[string]HealthResult `json:"checks"`
Summary HealthSummary `json:"summary"`
}
DetailedHealthResponse represents a detailed health response for Swagger documentation.
type DurationLoader ¶
DurationLoader supplies duration values for configuration.
type HTTPChecker ¶
type HTTPChecker struct {
// contains filtered or unexported fields
}
HTTPChecker implements a basic HTTP health check.
func (*HTTPChecker) Check ¶
func (c *HTTPChecker) Check(ctx context.Context) ports.HealthResult
Check runs the HTTP health check.
type HTTPCheckerOption ¶
type HTTPCheckerOption func(*HTTPChecker)
HTTPCheckerOption configures HTTPChecker behavior.
func WithHTTPClient ¶
func WithHTTPClient(client *http.Client) HTTPCheckerOption
WithHTTPClient sets the HTTP client for the checker.
func WithHTTPFailureStatus ¶
func WithHTTPFailureStatus(status ports.HealthStatus) HTTPCheckerOption
WithHTTPFailureStatus sets the health status when the check fails.
func WithHTTPHeader ¶
func WithHTTPHeader(key, value string) HTTPCheckerOption
WithHTTPHeader sets a header to send with the request.
func WithHTTPHeaders ¶
func WithHTTPHeaders(headers map[string]string) HTTPCheckerOption
WithHTTPHeaders sets multiple headers to send with the request.
func WithHTTPMethod ¶
func WithHTTPMethod(method string) HTTPCheckerOption
WithHTTPMethod sets the HTTP method.
func WithHTTPSuccessStatuses ¶
func WithHTTPSuccessStatuses(statuses ...int) HTTPCheckerOption
WithHTTPSuccessStatuses defines acceptable status codes.
func WithHTTPTimeout ¶
func WithHTTPTimeout(timeout time.Duration) HTTPCheckerOption
WithHTTPTimeout sets the request timeout.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler provides HTTP handlers for health endpoints.
func NewBasicHandler ¶
func NewBasicHandler() *Handler
NewBasicHandler builds a handler with just the basic checker.
func NewDefaultHandler ¶
func NewDefaultHandler(pool ports.DatabasePool) *Handler
NewDefaultHandler builds a handler with standard checkers.
func NewHandler ¶
func NewHandler(manager ports.HealthManager) *Handler
NewHandler creates a new health handler.
func (*Handler) DetailedHealthHandler ¶
func (h *Handler) DetailedHealthHandler(w http.ResponseWriter, r *http.Request)
DetailedHealthHandler handles detailed health checks. @Summary Detailed health check @Description Returns detailed health information including individual checks @Tags health @Accept json @Produce json @Success 200 {object} DetailedHealthResponse "Detailed health information" @Failure 503 {object} DetailedHealthResponse "Application is unhealthy" @Router /health/detailed [get]
func (*Handler) HealthHandler ¶
func (h *Handler) HealthHandler(w http.ResponseWriter, r *http.Request)
HealthHandler handles basic health checks. @Summary Health check @Description Returns the basic health status of the application @Tags health @Accept json @Produce json @Success 200 {object} map[string]interface{} "Application is healthy" @Failure 503 {object} map[string]interface{} "Application is unhealthy" @Router /healthz [get]
func (*Handler) LivenessHandler ¶
func (h *Handler) LivenessHandler(w http.ResponseWriter, r *http.Request)
LivenessHandler handles liveness checks. @Summary Liveness probe @Description Returns the liveness status of the application @Tags health @Accept json @Produce json @Success 200 {object} map[string]interface{} "Application is alive" @Failure 503 {object} map[string]interface{} "Application is not alive" @Router /livez [get]
func (*Handler) Middleware ¶
Middleware creates a middleware that adds health information to requests.
func (*Handler) ReadinessHandler ¶
func (h *Handler) ReadinessHandler(w http.ResponseWriter, r *http.Request)
ReadinessHandler handles readiness checks. @Summary Readiness probe @Description Returns the readiness status of the application @Tags health @Accept json @Produce json @Success 200 {object} map[string]interface{} "Application is ready" @Failure 503 {object} map[string]interface{} "Application is not ready" @Router /readyz [get]
func (*Handler) RegisterCustomRoutes ¶
func (h *Handler) RegisterCustomRoutes(router interface { Get(pattern string, h http.HandlerFunc) }, paths HealthPaths)
RegisterCustomRoutes registers health endpoints with custom paths.
func (*Handler) RegisterRoutes ¶
func (h *Handler) RegisterRoutes(router interface { Get(pattern string, h http.HandlerFunc) })
RegisterRoutes registers all health endpoints on the given router.
type HealthPaths ¶
HealthPaths defines custom paths for health endpoints.
func DefaultHealthPaths ¶
func DefaultHealthPaths() HealthPaths
DefaultHealthPaths returns the default health endpoint paths.
type HealthResponse ¶
type HealthResponse struct {
Status string `json:"status" example:"healthy"`
Timestamp time.Time `json:"timestamp"`
Message string `json:"message,omitempty"`
}
HealthResponse represents a health check response for Swagger documentation.
type HealthResult ¶
type HealthResult struct {
Status string `json:"status" example:"healthy"`
Message string `json:"message,omitempty"`
Details interface{} `json:"details,omitempty"`
Timestamp time.Time `json:"timestamp"`
Duration int64 `json:"duration,omitempty"` // Duration in nanoseconds
}
HealthResult represents a single health check result for Swagger documentation.
type HealthSummary ¶
type HealthSummary struct {
Total int `json:"total" example:"3"`
Healthy int `json:"healthy" example:"3"`
Unhealthy int `json:"unhealthy" example:"0"`
Degraded int `json:"degraded" example:"0"`
Unknown int `json:"unknown" example:"0"`
}
HealthSummary provides a summary of all health checks for Swagger documentation.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager implements ports.HealthManager for managing health checks.
func NewManagerWithConfig ¶
func NewManagerWithConfig(config ports.HealthCheckConfig) *Manager
NewManagerWithConfig creates a new health manager and returns the concrete type.
func (*Manager) GetDetailedHealth ¶
func (m *Manager) GetDetailedHealth(ctx context.Context) ports.DetailedHealthResponse
GetDetailedHealth performs detailed health checks.
func (*Manager) GetHealth ¶
func (m *Manager) GetHealth(ctx context.Context) ports.HealthResponse
GetHealth performs basic health checks.
func (*Manager) GetLiveness ¶
func (m *Manager) GetLiveness(ctx context.Context) ports.HealthResult
GetLiveness performs liveness checks.
func (*Manager) GetReadiness ¶
func (m *Manager) GetReadiness(ctx context.Context) ports.HealthResult
GetReadiness performs readiness checks.
func (*Manager) RefreshAll ¶
func (m *Manager) RefreshAll(ctx context.Context) ports.DetailedHealthResponse
RefreshAll runs all registered checks and updates the cache.
func (*Manager) RegisterChecker ¶
func (m *Manager) RegisterChecker(checker ports.HealthChecker)
RegisterChecker registers a single health checker.
func (*Manager) RegisterCheckers ¶
func (m *Manager) RegisterCheckers(checkers ...ports.HealthChecker)
RegisterCheckers registers multiple health checkers.
type MemoryChecker ¶
type MemoryChecker struct {
// contains filtered or unexported fields
}
MemoryChecker implements a memory usage health check.
func (*MemoryChecker) Check ¶
func (c *MemoryChecker) Check(_ context.Context) ports.HealthResult
Check runs the memory usage health check.
type PaymentProviderChecker ¶
type PaymentProviderChecker struct {
// contains filtered or unexported fields
}
PaymentProviderChecker implements a health check for payment providers.
func (*PaymentProviderChecker) Check ¶
func (c *PaymentProviderChecker) Check(ctx context.Context) ports.HealthResult
Check runs the payment provider health check.
func (*PaymentProviderChecker) Name ¶
func (c *PaymentProviderChecker) Name() string
Name returns the checker name.
type PaymentProviderCheckerOption ¶
type PaymentProviderCheckerOption func(*PaymentProviderChecker)
PaymentProviderCheckerOption configures PaymentProviderChecker behavior.
func WithPaymentProviderFailureStatus ¶
func WithPaymentProviderFailureStatus(status ports.HealthStatus) PaymentProviderCheckerOption
WithPaymentProviderFailureStatus sets the failure status.
func WithPaymentProviderName ¶
func WithPaymentProviderName(name string) PaymentProviderCheckerOption
WithPaymentProviderName overrides the checker name.
type RefreshManager ¶
type RefreshManager interface {
RefreshAll(ctx context.Context) ports.DetailedHealthResponse
}
RefreshManager allows scheduling cached health refreshes.
type Scheduler ¶
type Scheduler struct {
// contains filtered or unexported fields
}
Scheduler periodically refreshes health checks to keep cache warm.
func NewScheduler ¶
func NewScheduler(manager RefreshManager, config SchedulerConfig) *Scheduler
NewScheduler creates a scheduler that refreshes all health checks on an interval.
type SchedulerConfig ¶
type SchedulerConfig struct {
Interval time.Duration
Logger ports.Logger
OnUpdate func(ctx context.Context, result ports.DetailedHealthResponse)
OnStatusChange func(ctx context.Context, prev, next ports.HealthStatus, result ports.DetailedHealthResponse)
}
SchedulerConfig configures periodic health refreshes.