2022-03-18 03:00:54 +00:00
|
|
|
// Copyright (c) 2022 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package prober implements a simple blackbox prober. Each probe runs
|
|
|
|
// in its own goroutine, and run results are recorded as Prometheus
|
|
|
|
// metrics.
|
|
|
|
package prober
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-03-22 20:45:11 +00:00
|
|
|
"encoding/json"
|
2022-03-18 03:00:54 +00:00
|
|
|
"errors"
|
2022-03-22 20:45:11 +00:00
|
|
|
"expvar"
|
2022-03-18 03:00:54 +00:00
|
|
|
"fmt"
|
2022-03-22 20:45:11 +00:00
|
|
|
"io"
|
2022-03-18 03:00:54 +00:00
|
|
|
"log"
|
2022-03-22 20:45:11 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2022-03-18 03:00:54 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-03-20 04:39:50 +00:00
|
|
|
// ProbeFunc is a function that probes something and reports whether
|
|
|
|
// the probe succeeded. The provided context's deadline must be obeyed
|
|
|
|
// for correct probe scheduling.
|
2022-03-20 03:58:35 +00:00
|
|
|
type ProbeFunc func(context.Context) error
|
2022-03-18 03:00:54 +00:00
|
|
|
|
|
|
|
// a Prober manages a set of probes and keeps track of their results.
|
|
|
|
type Prober struct {
|
|
|
|
// Time-related functions that get faked out during tests.
|
|
|
|
now func() time.Time
|
|
|
|
newTicker func(time.Duration) ticker
|
|
|
|
|
2022-03-20 04:39:50 +00:00
|
|
|
mu sync.Mutex // protects all following fields
|
|
|
|
probes map[string]*Probe
|
2022-03-18 03:00:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new Prober.
|
|
|
|
func New() *Prober {
|
|
|
|
return newForTest(time.Now, newRealTicker)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newForTest(now func() time.Time, newTicker func(time.Duration) ticker) *Prober {
|
|
|
|
return &Prober{
|
2022-03-22 20:45:11 +00:00
|
|
|
now: now,
|
|
|
|
newTicker: newTicker,
|
|
|
|
probes: map[string]*Probe{},
|
2022-03-18 03:00:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expvar returns the metrics for running probes.
|
2022-03-22 20:45:11 +00:00
|
|
|
func (p *Prober) Expvar() expvar.Var {
|
|
|
|
return varExporter{p}
|
2022-03-18 03:00:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run executes fun every interval, and exports probe results under probeName.
|
|
|
|
//
|
|
|
|
// Registering a probe under an already-registered name panics.
|
2022-03-22 20:45:11 +00:00
|
|
|
func (p *Prober) Run(name string, interval time.Duration, labels map[string]string, fun ProbeFunc) *Probe {
|
2022-03-18 03:00:54 +00:00
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
2022-03-20 04:39:50 +00:00
|
|
|
if _, ok := p.probes[name]; ok {
|
|
|
|
panic(fmt.Sprintf("probe named %q already registered", name))
|
|
|
|
}
|
2022-03-18 03:00:54 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2022-03-20 04:39:50 +00:00
|
|
|
ticker := p.newTicker(interval)
|
|
|
|
probe := &Probe{
|
|
|
|
prober: p,
|
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
stopped: make(chan struct{}),
|
|
|
|
|
|
|
|
name: name,
|
|
|
|
doProbe: fun,
|
|
|
|
interval: interval,
|
|
|
|
tick: ticker,
|
2022-03-22 20:45:11 +00:00
|
|
|
labels: labels,
|
2022-03-18 03:00:54 +00:00
|
|
|
}
|
2022-03-20 04:39:50 +00:00
|
|
|
p.probes[name] = probe
|
|
|
|
go probe.loop()
|
|
|
|
return probe
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Prober) unregister(probe *Probe) {
|
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
name := probe.name
|
|
|
|
delete(p.probes, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reports the number of registered probes. For tests only.
|
|
|
|
func (p *Prober) activeProbes() int {
|
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
return len(p.probes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Probe is a probe that healthchecks something and updates Prometheus
|
|
|
|
// metrics with the results.
|
|
|
|
type Probe struct {
|
|
|
|
prober *Prober
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc // run to initiate shutdown
|
|
|
|
stopped chan struct{} // closed when shutdown is complete
|
|
|
|
|
|
|
|
name string
|
|
|
|
doProbe ProbeFunc
|
|
|
|
interval time.Duration
|
|
|
|
tick ticker
|
2022-03-22 20:45:11 +00:00
|
|
|
labels map[string]string
|
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
start time.Time // last time doProbe started
|
|
|
|
end time.Time // last time doProbe returned
|
|
|
|
result bool // whether the last doProbe call succeeded
|
2022-03-20 04:39:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close shuts down the Probe and unregisters it from its Prober.
|
|
|
|
// It is safe to Run a new probe of the same name after Close returns.
|
|
|
|
func (p *Probe) Close() error {
|
|
|
|
p.cancel()
|
|
|
|
<-p.stopped
|
|
|
|
p.prober.unregister(p)
|
|
|
|
return nil
|
2022-03-18 03:00:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// probeLoop invokes runProbe on fun every interval. The first probe
|
|
|
|
// is run after interval.
|
2022-03-20 04:39:50 +00:00
|
|
|
func (p *Probe) loop() {
|
|
|
|
defer close(p.stopped)
|
2022-03-18 03:00:54 +00:00
|
|
|
|
2022-03-20 01:41:27 +00:00
|
|
|
// Do a first probe right away, so that the prober immediately exports results for everything.
|
2022-03-20 04:39:50 +00:00
|
|
|
p.run()
|
2022-03-18 03:00:54 +00:00
|
|
|
for {
|
|
|
|
select {
|
2022-03-20 04:39:50 +00:00
|
|
|
case <-p.tick.Chan():
|
|
|
|
p.run()
|
|
|
|
case <-p.ctx.Done():
|
2022-03-18 03:00:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-20 04:39:50 +00:00
|
|
|
// run invokes fun and records the results.
|
2022-03-18 03:00:54 +00:00
|
|
|
//
|
|
|
|
// fun is invoked with a timeout slightly less than interval, so that
|
|
|
|
// the probe either succeeds or fails before the next cycle is
|
|
|
|
// scheduled to start.
|
2022-03-20 04:39:50 +00:00
|
|
|
func (p *Probe) run() {
|
2022-03-22 20:45:11 +00:00
|
|
|
start := p.recordStart()
|
2022-03-18 03:00:54 +00:00
|
|
|
defer func() {
|
|
|
|
// Prevent a panic within one probe function from killing the
|
|
|
|
// entire prober, so that a single buggy probe doesn't destroy
|
|
|
|
// our entire ability to monitor anything. A panic is recorded
|
|
|
|
// as a probe failure, so panicking probes will trigger an
|
|
|
|
// alert for debugging.
|
|
|
|
if r := recover(); r != nil {
|
2022-03-20 04:39:50 +00:00
|
|
|
log.Printf("probe %s panicked: %v", p.name, r)
|
2022-03-22 20:45:11 +00:00
|
|
|
p.recordEnd(start, errors.New("panic"))
|
2022-03-18 03:00:54 +00:00
|
|
|
}
|
|
|
|
}()
|
2022-03-20 04:39:50 +00:00
|
|
|
timeout := time.Duration(float64(p.interval) * 0.8)
|
|
|
|
ctx, cancel := context.WithTimeout(p.ctx, timeout)
|
2022-03-18 03:00:54 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2022-03-20 04:39:50 +00:00
|
|
|
err := p.doProbe(ctx)
|
2022-03-22 20:45:11 +00:00
|
|
|
p.recordEnd(start, err)
|
2022-03-18 03:00:54 +00:00
|
|
|
if err != nil {
|
2022-03-20 04:39:50 +00:00
|
|
|
log.Printf("probe %s: %v", p.name, err)
|
2022-03-18 03:00:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 20:45:11 +00:00
|
|
|
func (p *Probe) recordStart() time.Time {
|
2022-03-20 04:39:50 +00:00
|
|
|
st := p.prober.now()
|
2022-03-22 20:45:11 +00:00
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
p.start = st
|
2022-03-18 03:00:54 +00:00
|
|
|
return st
|
|
|
|
}
|
|
|
|
|
2022-03-22 20:45:11 +00:00
|
|
|
func (p *Probe) recordEnd(start time.Time, err error) {
|
2022-03-20 04:39:50 +00:00
|
|
|
end := p.prober.now()
|
2022-03-22 20:45:11 +00:00
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
p.end = end
|
|
|
|
p.result = err == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type varExporter struct {
|
|
|
|
p *Prober
|
|
|
|
}
|
|
|
|
|
|
|
|
// probeInfo is the state of a Probe. Used in expvar-format debug
|
|
|
|
// data.
|
|
|
|
type probeInfo struct {
|
|
|
|
Labels map[string]string
|
|
|
|
Start time.Time
|
|
|
|
End time.Time
|
|
|
|
Latency string // as a string because time.Duration doesn't encode readably to JSON
|
|
|
|
Result bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// String implements expvar.Var, returning the prober's state as an
|
|
|
|
// encoded JSON map of probe name to its probeInfo.
|
|
|
|
func (v varExporter) String() string {
|
|
|
|
out := map[string]probeInfo{}
|
|
|
|
|
|
|
|
v.p.mu.Lock()
|
|
|
|
probes := make([]*Probe, 0, len(v.p.probes))
|
|
|
|
for _, probe := range v.p.probes {
|
|
|
|
probes = append(probes, probe)
|
|
|
|
}
|
|
|
|
v.p.mu.Unlock()
|
|
|
|
|
|
|
|
for _, probe := range probes {
|
|
|
|
probe.mu.Lock()
|
|
|
|
inf := probeInfo{
|
|
|
|
Labels: probe.labels,
|
|
|
|
Start: probe.start,
|
|
|
|
End: probe.end,
|
|
|
|
Result: probe.result,
|
|
|
|
}
|
|
|
|
if probe.end.After(probe.start) {
|
|
|
|
inf.Latency = probe.end.Sub(probe.start).String()
|
|
|
|
}
|
|
|
|
out[probe.name] = inf
|
|
|
|
probe.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
bs, err := json.Marshal(out)
|
2022-03-18 03:00:54 +00:00
|
|
|
if err != nil {
|
2022-03-22 20:45:11 +00:00
|
|
|
return fmt.Sprintf(`{"error": %q}`, err)
|
|
|
|
}
|
|
|
|
return string(bs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WritePrometheus writes the the state of all probes to w.
|
|
|
|
//
|
|
|
|
// For each probe, WritePrometheus exports 5 variables:
|
2022-08-02 16:33:46 +00:00
|
|
|
// - <prefix>_interval_secs, how frequently the probe runs.
|
|
|
|
// - <prefix>_start_secs, when the probe last started running, in seconds since epoch.
|
|
|
|
// - <prefix>_end_secs, when the probe last finished running, in seconds since epoch.
|
|
|
|
// - <prefix>_latency_millis, how long the last probe cycle took, in
|
|
|
|
// milliseconds. This is just (end_secs-start_secs) in an easier to
|
|
|
|
// graph form.
|
|
|
|
// - <prefix>_result, 1 if the last probe succeeded, 0 if it failed.
|
2022-03-22 20:45:11 +00:00
|
|
|
//
|
|
|
|
// Each probe has a set of static key/value labels (defined once at
|
|
|
|
// probe creation), which are added as Prometheus metric labels to
|
|
|
|
// that probe's variables.
|
|
|
|
func (v varExporter) WritePrometheus(w io.Writer, prefix string) {
|
|
|
|
v.p.mu.Lock()
|
|
|
|
probes := make([]*Probe, 0, len(v.p.probes))
|
|
|
|
for _, probe := range v.p.probes {
|
|
|
|
probes = append(probes, probe)
|
|
|
|
}
|
|
|
|
v.p.mu.Unlock()
|
|
|
|
|
|
|
|
sort.Slice(probes, func(i, j int) bool {
|
|
|
|
return probes[i].name < probes[j].name
|
|
|
|
})
|
|
|
|
for _, probe := range probes {
|
|
|
|
probe.mu.Lock()
|
|
|
|
keys := make([]string, 0, len(probe.labels))
|
|
|
|
for k := range probe.labels {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
var sb strings.Builder
|
|
|
|
fmt.Fprintf(&sb, "name=%q", probe.name)
|
|
|
|
for _, k := range keys {
|
|
|
|
fmt.Fprintf(&sb, ",%s=%q", k, probe.labels[k])
|
|
|
|
}
|
|
|
|
labels := sb.String()
|
|
|
|
|
|
|
|
fmt.Fprintf(w, "%s_interval_secs{%s} %f\n", prefix, labels, probe.interval.Seconds())
|
|
|
|
if !probe.start.IsZero() {
|
|
|
|
fmt.Fprintf(w, "%s_start_secs{%s} %d\n", prefix, labels, probe.start.Unix())
|
|
|
|
}
|
|
|
|
if !probe.end.IsZero() {
|
|
|
|
fmt.Fprintf(w, "%s_end_secs{%s} %d\n", prefix, labels, probe.end.Unix())
|
|
|
|
// Start is always present if end is.
|
|
|
|
fmt.Fprintf(w, "%s_latency_millis{%s} %d\n", prefix, labels, probe.end.Sub(probe.start).Milliseconds())
|
|
|
|
if probe.result {
|
|
|
|
fmt.Fprintf(w, "%s_result{%s} 1\n", prefix, labels)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, "%s_result{%s} 0\n", prefix, labels)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
probe.mu.Unlock()
|
2022-03-18 03:00:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ticker wraps a time.Ticker in a way that can be faked for tests.
|
|
|
|
type ticker interface {
|
|
|
|
Chan() <-chan time.Time
|
|
|
|
Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
type realTicker struct {
|
|
|
|
*time.Ticker
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *realTicker) Chan() <-chan time.Time {
|
|
|
|
return t.Ticker.C
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRealTicker(d time.Duration) ticker {
|
|
|
|
return &realTicker{time.NewTicker(d)}
|
|
|
|
}
|