Documentation
¶
Overview ¶
Package heartbeat sends an outbound dead-man ping so an external monitor alerts when the control plane stops, which no in-box alert can do.
Index ¶
- Constants
- Variables
- func HostOf(raw string) string
- func NewClient(allowPrivate bool, timeout time.Duration) *http.Client
- func Scrub(err error) error
- func ValidateURL(raw string, allowPrivate bool) (*url.URL, error)
- type Config
- type Health
- type Options
- type Secrets
- type Service
- type StateStore
- type Status
- type Update
Constants ¶
const ( EnvURL = "APP_HEARTBEAT_URL" EnvFailURL = "APP_HEARTBEAT_FAIL_URL" EnvInterval = "APP_HEARTBEAT_INTERVAL" EnvTimeout = "APP_HEARTBEAT_TIMEOUT" EnvBody = "APP_HEARTBEAT_BODY" EnvMethod = "APP_HEARTBEAT_METHOD" EnvAllowPrivate = "APP_HEARTBEAT_ALLOW_PRIVATE" EnvReconcileMul = "APP_HEARTBEAT_RECONCILE_WINDOWS" )
Environment variables that configure and lock the heartbeat settings.
const ( ResultOK = "ok" ResultFailSent = "fail_reported" ResultUnhealthy = "unhealthy" ResultSendFailed = "error" )
Result words recorded after each tick.
const ( SourceEnv = "env" SourceDashboard = "dashboard" )
Source says where the active ping URL comes from.
Variables ¶
var ( // ErrNoSecrets means no master key is configured, so a URL cannot be stored. ErrNoSecrets = errors.New("heartbeat: no secrets store configured") // ErrEnvManaged means APP_HEARTBEAT_URL is set and owns the configuration. ErrEnvManaged = errors.New("heartbeat: configured by " + EnvURL) )
Functions ¶
func HostOf ¶
HostOf returns only the host of raw, the one part of a heartbeat URL safe to show or log.
func NewClient ¶
NewClient is the guarded HTTP client for off-host pings: no proxy, no private destinations unless allowed, same-host redirects only.
Types ¶
type Config ¶
type Config struct {
URL string
FailURL string
Interval time.Duration
Timeout time.Duration
Method string
BodyOff bool
AllowPrivate bool
// ReconcileWindows is how many reconcile intervals may pass without a pass before the loop counts as stalled.
ReconcileWindows int
}
Config is the env-derived heartbeat configuration.
type Health ¶
Health reports whether the control plane is fit to claim it is alive, with a short reason word when not.
type Options ¶
type Options struct {
Config Config
Secrets Secrets
State StateStore
Health Health
Logger *slog.Logger
Version string
UserAgent string
}
Options carries the service's collaborators.
type Secrets ¶
type Secrets interface {
SetValue(ctx context.Context, serviceName, envKey, plaintext string) error
Resolve(ctx context.Context, serviceName, envKey string) (string, error)
Exists(ctx context.Context, serviceName, envKey string) (bool, error)
DeleteValue(ctx context.Context, serviceName, envKey string) error
}
Secrets is the slice of the secrets manager the heartbeat needs.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service pings the configured URL while the control plane is healthy.
func (*Service) AgentSilenceURL ¶
AgentSilenceURL returns the stored agent silence URL, or "" when none is set.
func (*Service) Apply ¶
Apply validates and stores an Update, then pings at once so the monitor sees the new URL.
func (*Service) Run ¶
Run pings every interval until ctx ends. It re-reads the target each tick so dashboard edits apply without a restart.
type StateStore ¶
type StateStore interface {
GetHeartbeatState(ctx context.Context) (store.HeartbeatState, error)
SaveHeartbeatState(ctx context.Context, s store.HeartbeatState) error
}
StateStore persists the last ping outcome and the body toggle.
type Status ¶
type Status struct {
Configured bool `json:"configured"`
Source string `json:"source,omitempty"`
Host string `json:"host,omitempty"`
FailHost string `json:"fail_host,omitempty"`
IntervalSecs int `json:"interval_seconds"`
BodyEnabled bool `json:"body_enabled"`
CanStoreURL bool `json:"can_store_url"`
LastPingAt time.Time `json:"last_ping_at,omitzero"`
LastResult string `json:"last_result,omitempty"`
LastError string `json:"last_error,omitempty"`
// AgentSilenceHost is the host agents ping when the control plane goes quiet; empty when unset.
AgentSilenceHost string `json:"agent_silence_host,omitempty"`
}
Status is the API view of the heartbeat; it never carries a URL, only hosts.