// 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" // JSON checks an HTTP API's JSON response against assertions. JSON Type = "json" // Prometheus compares the result of a PromQL query with a threshold. Prometheus Type = "prometheus" // 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, JSON, Prometheus, 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 // Capture records Evidence of HTTP exchanges. Capture 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 // Evidence is set when Spec.Capture was and the check made HTTP // requests. Evidence *Evidence } // 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{}, JSON: jsonCheck{}, Prometheus: prometheusCheck{}, 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() var rec *recorder if s.Capture { rec = newRecorder() ctx = context.WithValue(ctx, recorderKey{}, rec) } 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() } if rec != nil { o.Evidence = rec.evidence(o) } 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 }