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
|
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
|
// NewHealthcheck constructs a new Healthcheck which will use the given
|
||||||
// function to update its status.
|
// function to update its status.
|
||||||
func NewHealthcheck(f func(Healthcheck)) Healthcheck {
|
func NewHealthcheck(f func(*Healthcheck)) *Healthcheck {
|
||||||
if !Enabled {
|
return &Healthcheck{nil, f}
|
||||||
return NilHealthcheck{}
|
|
||||||
}
|
|
||||||
return &StandardHealthcheck{nil, f}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NilHealthcheck is a no-op.
|
// Healthcheck is the standard implementation of a Healthcheck and
|
||||||
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
|
|
||||||
// stores the status and a function to call to update the status.
|
// stores the status and a function to call to update the status.
|
||||||
type StandardHealthcheck struct {
|
type Healthcheck struct {
|
||||||
err error
|
err error
|
||||||
f func(Healthcheck)
|
f func(*Healthcheck)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check runs the healthcheck function to update the healthcheck's status.
|
// Check runs the healthcheck function to update the healthcheck's status.
|
||||||
func (h *StandardHealthcheck) Check() {
|
func (h *Healthcheck) Check() {
|
||||||
h.f(h)
|
h.f(h)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Error returns the healthcheck's status, which will be nil if it is healthy.
|
// 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
|
return h.err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Healthy marks the healthcheck as healthy.
|
// Healthy marks the healthcheck as healthy.
|
||||||
func (h *StandardHealthcheck) Healthy() {
|
func (h *Healthcheck) Healthy() {
|
||||||
h.err = nil
|
h.err = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unhealthy marks the healthcheck as unhealthy. The error is stored and
|
// Unhealthy marks the healthcheck as unhealthy. The error is stored and
|
||||||
// may be retrieved by the Error method.
|
// may be retrieved by the Error method.
|
||||||
func (h *StandardHealthcheck) Unhealthy(err error) {
|
func (h *Healthcheck) Unhealthy(err error) {
|
||||||
h.err = err
|
h.err = err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -36,10 +36,6 @@ func LogScaled(r Registry, freq time.Duration, scale time.Duration, l Logger) {
|
||||||
case *GaugeInfo:
|
case *GaugeInfo:
|
||||||
l.Printf("gauge %s\n", name)
|
l.Printf("gauge %s\n", name)
|
||||||
l.Printf(" value: %s\n", metric.Snapshot().Value())
|
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:
|
case Histogram:
|
||||||
h := metric.Snapshot()
|
h := metric.Snapshot()
|
||||||
ps := h.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
|
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.
|
// RunHealthchecks run all registered healthchecks.
|
||||||
func (r *StandardRegistry) RunHealthchecks() {
|
func (r *StandardRegistry) RunHealthchecks() {
|
||||||
r.metrics.Range(func(key, value any) bool {
|
r.metrics.Range(func(key, value any) bool {
|
||||||
if h, ok := value.(Healthcheck); ok {
|
if h, ok := value.(*Healthcheck); ok {
|
||||||
h.Check()
|
h.Check()
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
|
|
@ -157,7 +157,7 @@ func (r *StandardRegistry) GetAll() map[string]map[string]interface{} {
|
||||||
values["value"] = metric.Snapshot().Value()
|
values["value"] = metric.Snapshot().Value()
|
||||||
case *GaugeFloat64:
|
case *GaugeFloat64:
|
||||||
values["value"] = metric.Snapshot().Value()
|
values["value"] = metric.Snapshot().Value()
|
||||||
case Healthcheck:
|
case *Healthcheck:
|
||||||
values["error"] = nil
|
values["error"] = nil
|
||||||
metric.Check()
|
metric.Check()
|
||||||
if err := metric.Error(); nil != err {
|
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) {
|
func (r *StandardRegistry) loadOrRegister(name string, i interface{}) (interface{}, bool, bool) {
|
||||||
switch i.(type) {
|
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:
|
default:
|
||||||
return nil, false, false
|
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()))
|
w.Info(fmt.Sprintf("gauge %s: value: %f", name, metric.Snapshot().Value()))
|
||||||
case *GaugeInfo:
|
case *GaugeInfo:
|
||||||
w.Info(fmt.Sprintf("gauge %s: value: %s", name, metric.Snapshot().Value()))
|
w.Info(fmt.Sprintf("gauge %s: value: %s", name, metric.Snapshot().Value()))
|
||||||
case Healthcheck:
|
case *Healthcheck:
|
||||||
metric.Check()
|
metric.Check()
|
||||||
w.Info(fmt.Sprintf("healthcheck %s: error: %v", name, metric.Error()))
|
w.Info(fmt.Sprintf("healthcheck %s: error: %v", name, metric.Error()))
|
||||||
case Histogram:
|
case Histogram:
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,7 @@ func WriteOnce(r Registry, w io.Writer) {
|
||||||
case *GaugeInfo:
|
case *GaugeInfo:
|
||||||
fmt.Fprintf(w, "gauge %s\n", namedMetric.name)
|
fmt.Fprintf(w, "gauge %s\n", namedMetric.name)
|
||||||
fmt.Fprintf(w, " value: %s\n", metric.Snapshot().Value().String())
|
fmt.Fprintf(w, " value: %s\n", metric.Snapshot().Value().String())
|
||||||
case Healthcheck:
|
case *Healthcheck:
|
||||||
metric.Check()
|
metric.Check()
|
||||||
fmt.Fprintf(w, "healthcheck %s\n", namedMetric.name)
|
fmt.Fprintf(w, "healthcheck %s\n", namedMetric.name)
|
||||||
fmt.Fprintf(w, " error: %v\n", metric.Error())
|
fmt.Fprintf(w, " error: %v\n", metric.Error())
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue