health

package
v2.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 2, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package health provides health utilities.

Index

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

func HealthTimestampFromContext(ctx context.Context) (time.Time, bool)

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

Check runs the basic health check.

func (*BasicChecker) Name

func (c *BasicChecker) Name() string

Name returns the checker name.

type CompositeChecker

type CompositeChecker struct {
	// contains filtered or unexported fields
}

CompositeChecker implements a composite health check that combines multiple checks.

func (*CompositeChecker) Check

Check runs the composite health check.

func (*CompositeChecker) Name

func (c *CompositeChecker) Name() string

Name returns the checker name.

type Config

type Config struct {
	RefreshInterval time.Duration
	CacheDuration   time.Duration
}

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

Check runs the custom health check.

func (*CustomChecker) Name

func (c *CustomChecker) Name() string

Name returns the checker name.

type DatabaseChecker

type DatabaseChecker struct {
	// contains filtered or unexported fields
}

DatabaseChecker implements a database health check.

func (*DatabaseChecker) Check

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

type DurationLoader interface {
	Duration(key string, def time.Duration) time.Duration
}

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

Check runs the HTTP health check.

func (*HTTPChecker) Name

func (c *HTTPChecker) Name() string

Name returns the checker name.

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

func (h *Handler) Middleware() func(http.Handler) http.Handler

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

type HealthPaths struct {
	Liveness       string
	Readiness      string
	Health         string
	DetailedHealth string
}

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

Check runs the memory usage health check.

func (*MemoryChecker) Name

func (c *MemoryChecker) Name() string

Name returns the checker name.

type PaymentProviderChecker

type PaymentProviderChecker struct {
	// contains filtered or unexported fields
}

PaymentProviderChecker implements a health check for payment providers.

func (*PaymentProviderChecker) Check

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.

func (*Scheduler) Start

func (s *Scheduler) Start(ctx context.Context)

Start runs the scheduler until the context is canceled.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL