metrics: fix healthcheck

This commit is contained in:
Martin Holst Swende 2024-11-26 21:52:07 +01:00
parent 0d706d62fa
commit 05d2af672e
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
5 changed files with 14 additions and 44 deletions

View file

@ -1,61 +1,35 @@
package metrics
// Healthcheck holds an error value describing an arbitrary up/down status.
type Healthcheck interface {
Check()
Error() error
Healthy()
Unhealthy(error)
}
// NewHealthcheck constructs a new Healthcheck which will use the given
// function to update its status.
func NewHealthcheck(f func(Healthcheck)) Healthcheck {
if !Enabled {
return NilHealthcheck{}
}
return &StandardHealthcheck{nil, f}
func NewHealthcheck(f func(*Healthcheck)) *Healthcheck {
return &Healthcheck{nil, f}
}
// NilHealthcheck is a no-op.
type NilHealthcheck struct{}
// Check is a no-op.
func (NilHealthcheck) Check() {}
// Error is a no-op.
func (NilHealthcheck) Error() error { return nil }
// Healthy is a no-op.
func (NilHealthcheck) Healthy() {}
// Unhealthy is a no-op.
func (NilHealthcheck) Unhealthy(error) {}
// StandardHealthcheck is the standard implementation of a Healthcheck and
// Healthcheck is the standard implementation of a Healthcheck and
// stores the status and a function to call to update the status.
type StandardHealthcheck struct {
type Healthcheck struct {
err error
f func(Healthcheck)
f func(*Healthcheck)
}
// Check runs the healthcheck function to update the healthcheck's status.
func (h *StandardHealthcheck) Check() {
func (h *Healthcheck) Check() {
h.f(h)
}
// Error returns the healthcheck's status, which will be nil if it is healthy.
func (h *StandardHealthcheck) Error() error {
func (h *Healthcheck) Error() error {
return h.err
}
// Healthy marks the healthcheck as healthy.
func (h *StandardHealthcheck) Healthy() {
func (h *Healthcheck) Healthy() {
h.err = nil
}
// Unhealthy marks the healthcheck as unhealthy. The error is stored and
// may be retrieved by the Error method.
func (h *StandardHealthcheck) Unhealthy(err error) {
func (h *Healthcheck) Unhealthy(err error) {
h.err = err
}

View file

@ -36,10 +36,6 @@ func LogScaled(r Registry, freq time.Duration, scale time.Duration, l Logger) {
case *GaugeInfo:
l.Printf("gauge %s\n", name)
l.Printf(" value: %s\n", metric.Snapshot().Value())
case Healthcheck:
metric.Check()
l.Printf("healthcheck %s\n", name)
l.Printf(" error: %v\n", metric.Error())
case Histogram:
h := metric.Snapshot()
ps := h.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})

View file

@ -136,7 +136,7 @@ func (r *StandardRegistry) Register(name string, i interface{}) error {
// RunHealthchecks run all registered healthchecks.
func (r *StandardRegistry) RunHealthchecks() {
r.metrics.Range(func(key, value any) bool {
if h, ok := value.(Healthcheck); ok {
if h, ok := value.(*Healthcheck); ok {
h.Check()
}
return true
@ -157,7 +157,7 @@ func (r *StandardRegistry) GetAll() map[string]map[string]interface{} {
values["value"] = metric.Snapshot().Value()
case *GaugeFloat64:
values["value"] = metric.Snapshot().Value()
case Healthcheck:
case *Healthcheck:
values["error"] = nil
metric.Check()
if err := metric.Error(); nil != err {
@ -214,7 +214,7 @@ func (r *StandardRegistry) Unregister(name string) {
func (r *StandardRegistry) loadOrRegister(name string, i interface{}) (interface{}, bool, bool) {
switch i.(type) {
case *Counter, *CounterFloat64, *Gauge, *GaugeFloat64, *GaugeInfo, Healthcheck, Histogram, Meter, Timer, ResettingTimer:
case *Counter, *CounterFloat64, *Gauge, *GaugeFloat64, *GaugeInfo, *Healthcheck, Histogram, Meter, Timer, ResettingTimer:
default:
return nil, false, false
}

View file

@ -25,7 +25,7 @@ func Syslog(r Registry, d time.Duration, w *syslog.Writer) {
w.Info(fmt.Sprintf("gauge %s: value: %f", name, metric.Snapshot().Value()))
case *GaugeInfo:
w.Info(fmt.Sprintf("gauge %s: value: %s", name, metric.Snapshot().Value()))
case Healthcheck:
case *Healthcheck:
metric.Check()
w.Info(fmt.Sprintf("healthcheck %s: error: %v", name, metric.Error()))
case Histogram:

View file

@ -41,7 +41,7 @@ func WriteOnce(r Registry, w io.Writer) {
case *GaugeInfo:
fmt.Fprintf(w, "gauge %s\n", namedMetric.name)
fmt.Fprintf(w, " value: %s\n", metric.Snapshot().Value().String())
case Healthcheck:
case *Healthcheck:
metric.Check()
fmt.Fprintf(w, "healthcheck %s\n", namedMetric.name)
fmt.Fprintf(w, " error: %v\n", metric.Error())