mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 18:02:24 +00:00
metrics: fix healthcheck
This commit is contained in:
parent
0d706d62fa
commit
05d2af672e
5 changed files with 14 additions and 44 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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})
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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())
|
||||
|
|
|
|||
Loading…
Reference in a new issue