feat(check): add http, tcp, tls, dns and icmp checks
Validate normalizes settings for the API; Run executes them on checkers. Targets on private networks are refused unless explicitly allowed. Refs #12 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
bc8e934568
commit
55559c8b0a
6 files changed
+710
-1
No files matched your search
@@ -0,0 +1,129 @@
|
||||
// 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"
|
||||
)
|
||||
|
||||
// Types lists every check type.
|
||||
var Types = []Type{HTTP, TCP, ICMP, DNS, TLS}
|
||||
|
||||
// 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{},
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
Reference in new issue
Block a user