health

package
v0.0.0-...-8102ba9 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package health provides health check API handlers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handler

func Handler(
	logger *slog.Logger,
	checker Checker,
	startTime time.Time,
	version string,
	metrics MetricsProvider,
	subComponents map[string]SubComponentInfo,
	signingKey string,
	customRoles map[string][]string,
) []func(e *echo.Echo)

Handler returns health route registration functions.

Types

type AgentDetail

type AgentDetail struct {
	Hostname   string
	Labels     string
	Registered string
}

AgentDetail holds per-agent registration info.

type AgentMetrics

type AgentMetrics struct {
	Total  int
	Ready  int
	Agents []AgentDetail
}

AgentMetrics holds agent fleet statistics.

type Checker

type Checker interface {
	CheckHealth(ctx context.Context) error
}

Checker checks the health of a dependency.

type ClosureMetricsProvider

type ClosureMetricsProvider struct {
	NATSInfoFn          func(ctx context.Context) (*NATSMetrics, error)
	StreamInfoFn        func(ctx context.Context) ([]StreamMetrics, error)
	KVInfoFn            func(ctx context.Context) ([]KVMetrics, error)
	ObjectStoreInfoFn   func(ctx context.Context) ([]ObjectStoreMetrics, error)
	JobStatsFn          func(ctx context.Context) (*JobMetrics, error)
	AgentStatsFn        func(ctx context.Context) (*AgentMetrics, error)
	ComponentRegistryFn func(ctx context.Context) ([]ComponentEntry, error)
}

ClosureMetricsProvider implements MetricsProvider using function closures.

func (*ClosureMetricsProvider) GetAgentStats

func (p *ClosureMetricsProvider) GetAgentStats(
	ctx context.Context,
) (*AgentMetrics, error)

GetAgentStats delegates to the AgentStatsFn closure.

func (*ClosureMetricsProvider) GetComponentRegistry

func (p *ClosureMetricsProvider) GetComponentRegistry(
	ctx context.Context,
) ([]ComponentEntry, error)

GetComponentRegistry delegates to the ComponentRegistryFn closure. Returns nil, nil when the closure is not configured.

func (*ClosureMetricsProvider) GetJobStats

func (p *ClosureMetricsProvider) GetJobStats(
	ctx context.Context,
) (*JobMetrics, error)

GetJobStats delegates to the JobStatsFn closure.

func (*ClosureMetricsProvider) GetKVInfo

func (p *ClosureMetricsProvider) GetKVInfo(
	ctx context.Context,
) ([]KVMetrics, error)

GetKVInfo delegates to the KVInfoFn closure.

func (*ClosureMetricsProvider) GetNATSInfo

func (p *ClosureMetricsProvider) GetNATSInfo(
	ctx context.Context,
) (*NATSMetrics, error)

GetNATSInfo delegates to the NATSInfoFn closure.

func (*ClosureMetricsProvider) GetObjectStoreInfo

func (p *ClosureMetricsProvider) GetObjectStoreInfo(
	ctx context.Context,
) ([]ObjectStoreMetrics, error)

GetObjectStoreInfo delegates to the ObjectStoreInfoFn closure.

func (*ClosureMetricsProvider) GetStreamInfo

func (p *ClosureMetricsProvider) GetStreamInfo(
	ctx context.Context,
) ([]StreamMetrics, error)

GetStreamInfo delegates to the StreamInfoFn closure.

type ComponentEntry

type ComponentEntry struct {
	Type          string
	Hostname      string
	Status        string
	Conditions    []string
	Age           string
	CPUPercent    float64
	MemBytes      int64
	SubComponents map[string]SubComponentInfo
}

ComponentEntry holds unified component registration details for the registry.

type ConsumerDetail

type ConsumerDetail struct {
	Name        string
	Pending     uint64
	AckPending  int
	Redelivered int
}

ConsumerDetail holds per-consumer information.

type ConsumerMetrics

type ConsumerMetrics struct {
	Total     int
	Consumers []ConsumerDetail
}

ConsumerMetrics holds JetStream consumer statistics.

type Health

type Health struct {
	// Checker performs dependency health checks.
	Checker Checker
	// StartTime records when the server started.
	StartTime time.Time
	// Version is the application version string.
	Version string
	// Metrics provides system metrics (optional, can be nil).
	Metrics MetricsProvider
	// SubComponents reports the status of internal services.
	SubComponents map[string]SubComponentInfo

	// MetricsRefreshInterval overrides the default metrics cache TTL
	// for the background refresh goroutine. Zero uses metricsCacheTTL.
	MetricsRefreshInterval time.Duration
	// contains filtered or unexported fields
}

Health implementation of the Health APIs operations.

func New

func New(
	logger *slog.Logger,
	checker Checker,
	startTime time.Time,
	version string,
	metrics MetricsProvider,
	subComponents map[string]SubComponentInfo,
) *Health

New factory to create a new instance.

func (*Health) GetHealth

GetHealth liveness probe — always returns 200 if the process is running.

func (*Health) GetHealthReady

GetHealthReady readiness probe — returns 200 when dependencies are reachable.

func (*Health) GetHealthStatus

GetHealthStatus returns per-component health status with system metrics (authenticated).

func (*Health) StartMetricsRefresh

func (h *Health) StartMetricsRefresh(
	ctx context.Context,
)

StartMetricsRefresh starts a background goroutine that refreshes the metrics cache on a fixed interval. This ensures the health endpoint always serves from cache and never blocks on NATS queries.

type JobMetrics

type JobMetrics struct {
	Total       int
	Unprocessed int
	Processing  int
	Completed   int
	Failed      int
	DLQ         int
}

JobMetrics holds job queue statistics.

type KVMetrics

type KVMetrics struct {
	Name  string
	Keys  int
	Bytes uint64
}

KVMetrics holds KV bucket statistics.

type MetricsProvider

type MetricsProvider interface {
	GetNATSInfo(ctx context.Context) (*NATSMetrics, error)
	GetStreamInfo(ctx context.Context) ([]StreamMetrics, error)
	GetKVInfo(ctx context.Context) ([]KVMetrics, error)
	GetObjectStoreInfo(ctx context.Context) ([]ObjectStoreMetrics, error)
	GetJobStats(ctx context.Context) (*JobMetrics, error)
	GetAgentStats(ctx context.Context) (*AgentMetrics, error)
	GetComponentRegistry(ctx context.Context) ([]ComponentEntry, error)
}

MetricsProvider retrieves system metrics for the status endpoint.

type NATSChecker

type NATSChecker struct {
	// NATSCheck verifies NATS connectivity.
	NATSCheck func() error
	// KVCheck verifies KV bucket accessibility.
	KVCheck func() error
}

NATSChecker checks NATS and KV bucket connectivity.

func (*NATSChecker) CheckHealth

func (c *NATSChecker) CheckHealth(
	_ context.Context,
) error

CheckHealth runs all dependency checks and returns the first error.

func (*NATSChecker) CheckKV

func (c *NATSChecker) CheckKV() error

CheckKV runs only the KV bucket check.

func (*NATSChecker) CheckNATS

func (c *NATSChecker) CheckNATS() error

CheckNATS runs only the NATS connectivity check.

type NATSMetrics

type NATSMetrics struct {
	URL     string
	Version string
}

NATSMetrics holds NATS connection information.

type ObjectStoreMetrics

type ObjectStoreMetrics struct {
	Name string
	Size uint64
}

ObjectStoreMetrics holds Object Store bucket statistics.

type StreamMetrics

type StreamMetrics struct {
	Name      string
	Messages  uint64
	Bytes     uint64
	Consumers int
}

StreamMetrics holds JetStream stream statistics.

type SubComponentInfo

type SubComponentInfo struct {
	Status  string
	Address string // Empty means no network endpoint.
}

SubComponentInfo holds the status and optional address of a sub-component.

Directories

Path Synopsis
Package gen contains generated code for the health API.
Package gen contains generated code for the health API.
Package mocks provides mock implementations for testing.
Package mocks provides mock implementations for testing.

Jump to

Keyboard shortcuts

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