Files
Sebastian UnterschützandClaude Opus 5.5 5a76702dba feat: heartbeat monitors pinged by cron jobs and scripts
A heartbeat monitor has a secret ping URL instead of a target and
regions. Jobs call it with GET, HEAD or POST after each run, or /fail
with an optional message. When no ping arrives within the interval plus
a grace period, a failure is recorded and evaluated like a checker
result, so confirmations, maintenance and notifications apply. One API
replica at a time checks deadlines. The URL is shown only to members who
may edit the monitor, since it can report the job healthy.

Closes #27

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-22 11:36:14 +01:00

138 lines
3.3 KiB
Go

// Package check runs a single uptime check and validates check settings.
//
// The API validates monitor settings with Validate before storing them, and
// checkers execute them with Run, so both sides agree on what a setting
// means.
package check
import (
"context"
"encoding/json"
"errors"
"time"
)
// Type names a kind of check. The values are stored in the database.
type Type string
// Check types.
const (
HTTP Type = "http"
TCP Type = "tcp"
ICMP Type = "icmp"
DNS Type = "dns"
TLS Type = "tls"
// Heartbeat monitors are pinged by the monitored job; no checker runs
// them.
Heartbeat Type = "heartbeat"
)
// Types lists every check type.
var Types = []Type{HTTP, TCP, ICMP, DNS, TLS, Heartbeat}
// Passive reports whether t is reported to us rather than run by checkers.
func Passive(t Type) bool { return t == Heartbeat }
// Spec is everything needed to run one check.
type Spec struct {
Type Type
Target string
Timeout time.Duration
Config json.RawMessage
// AllowPrivate permits targets on private networks. It is false for
// shared checkers in the cloud edition.
AllowPrivate bool
}
// Outcome is the result of one check run.
type Outcome struct {
OK bool
Latency time.Duration
// StatusCode is the HTTP status, or 0 for other types.
StatusCode int
// Error explains a failure in a form suitable for showing to customers.
Error string
}
// ErrInvalid is wrapped by Validate errors. Its message is the code shown to
// API clients, e.g. "invalid_target".
type ErrInvalid struct{ Code string }
func (e ErrInvalid) Error() string { return "check: " + e.Code }
func invalid(code string) error { return ErrInvalid{Code: code} }
type runner interface {
run(ctx context.Context, s Spec) Outcome
}
type validator interface {
// validate checks target and config and returns the config with
// defaults applied.
validate(target string, config json.RawMessage) (json.RawMessage, error)
}
type impl interface {
runner
validator
}
var impls = map[Type]impl{
HTTP: httpCheck{},
TCP: tcpCheck{},
ICMP: icmpCheck{},
DNS: dnsCheck{},
TLS: tlsCheck{},
Heartbeat: heartbeatCheck{},
}
// Validate checks target and config for type t and returns the normalized
// config. Errors are ErrInvalid.
func Validate(t Type, target string, config json.RawMessage) (json.RawMessage, error) {
im, ok := impls[t]
if !ok {
return nil, invalid("invalid_type")
}
if len(config) == 0 || string(config) == "null" {
config = json.RawMessage("{}")
}
return im.validate(target, config)
}
// Run executes s. It never panics on bad input; problems become a failed
// Outcome.
func Run(ctx context.Context, s Spec) Outcome {
im, ok := impls[s.Type]
if !ok {
return Outcome{Error: "unknown check type"}
}
ctx, cancel := context.WithTimeout(ctx, s.Timeout)
defer cancel()
start := time.Now()
o := im.run(ctx, s)
if o.Latency == 0 {
o.Latency = time.Since(start)
}
if !o.OK && o.Error == "" {
o.Error = "check failed"
}
if errors.Is(ctx.Err(), context.DeadlineExceeded) && !o.OK {
o.Error = "timeout after " + s.Timeout.String()
}
return o
}
func decode[T any](config json.RawMessage) (T, error) {
var v T
if err := json.Unmarshal(config, &v); err != nil {
return v, invalid("invalid_config")
}
return v, nil
}
func encode(v any) json.RawMessage {
b, _ := json.Marshal(v)
return b
}