github.com/codysnider/go-ethereum@v1.10.18-0.20220420071915-14f4ae99222a/metrics/exp/exp.go (about)

     1  // Hook go-metrics into expvar
     2  // on any /debug/metrics request, load all vars from the registry into expvar, and execute regular expvar handler
     3  package exp
     4  
     5  import (
     6  	"expvar"
     7  	"fmt"
     8  	"net/http"
     9  	"sync"
    10  
    11  	"github.com/ethereum/go-ethereum/log"
    12  	"github.com/ethereum/go-ethereum/metrics"
    13  	"github.com/ethereum/go-ethereum/metrics/prometheus"
    14  )
    15  
    16  type exp struct {
    17  	expvarLock sync.Mutex // expvar panics if you try to register the same var twice, so we must probe it safely
    18  	registry   metrics.Registry
    19  }
    20  
    21  func (exp *exp) expHandler(w http.ResponseWriter, r *http.Request) {
    22  	// load our variables into expvar
    23  	exp.syncToExpvar()
    24  
    25  	// now just run the official expvar handler code (which is not publicly callable, so pasted inline)
    26  	w.Header().Set("Content-Type", "application/json; charset=utf-8")
    27  	fmt.Fprintf(w, "{\n")
    28  	first := true
    29  	expvar.Do(func(kv expvar.KeyValue) {
    30  		if !first {
    31  			fmt.Fprintf(w, ",\n")
    32  		}
    33  		first = false
    34  		fmt.Fprintf(w, "%q: %s", kv.Key, kv.Value)
    35  	})
    36  	fmt.Fprintf(w, "\n}\n")
    37  }
    38  
    39  // Exp will register an expvar powered metrics handler with http.DefaultServeMux on "/debug/vars"
    40  func Exp(r metrics.Registry) {
    41  	h := ExpHandler(r)
    42  	// this would cause a panic:
    43  	// panic: http: multiple registrations for /debug/vars
    44  	// http.HandleFunc("/debug/vars", e.expHandler)
    45  	// haven't found an elegant way, so just use a different endpoint
    46  	http.Handle("/debug/metrics", h)
    47  	http.Handle("/debug/metrics/prometheus", prometheus.Handler(r))
    48  }
    49  
    50  // ExpHandler will return an expvar powered metrics handler.
    51  func ExpHandler(r metrics.Registry) http.Handler {
    52  	e := exp{sync.Mutex{}, r}
    53  	return http.HandlerFunc(e.expHandler)
    54  }
    55  
    56  // Setup starts a dedicated metrics server at the given address.
    57  // This function enables metrics reporting separate from pprof.
    58  func Setup(address string) {
    59  	m := http.NewServeMux()
    60  	m.Handle("/debug/metrics", ExpHandler(metrics.DefaultRegistry))
    61  	m.Handle("/debug/metrics/prometheus", prometheus.Handler(metrics.DefaultRegistry))
    62  	log.Info("Starting metrics server", "addr", fmt.Sprintf("http://%s/debug/metrics", address))
    63  	go func() {
    64  		if err := http.ListenAndServe(address, m); err != nil {
    65  			log.Error("Failure in running metrics server", "err", err)
    66  		}
    67  	}()
    68  }
    69  
    70  func (exp *exp) getInt(name string) *expvar.Int {
    71  	var v *expvar.Int
    72  	exp.expvarLock.Lock()
    73  	p := expvar.Get(name)
    74  	if p != nil {
    75  		v = p.(*expvar.Int)
    76  	} else {
    77  		v = new(expvar.Int)
    78  		expvar.Publish(name, v)
    79  	}
    80  	exp.expvarLock.Unlock()
    81  	return v
    82  }
    83  
    84  func (exp *exp) getFloat(name string) *expvar.Float {
    85  	var v *expvar.Float
    86  	exp.expvarLock.Lock()
    87  	p := expvar.Get(name)
    88  	if p != nil {
    89  		v = p.(*expvar.Float)
    90  	} else {
    91  		v = new(expvar.Float)
    92  		expvar.Publish(name, v)
    93  	}
    94  	exp.expvarLock.Unlock()
    95  	return v
    96  }
    97  
    98  func (exp *exp) publishCounter(name string, metric metrics.Counter) {
    99  	v := exp.getInt(name)
   100  	v.Set(metric.Count())
   101  }
   102  
   103  func (exp *exp) publishGauge(name string, metric metrics.Gauge) {
   104  	v := exp.getInt(name)
   105  	v.Set(metric.Value())
   106  }
   107  func (exp *exp) publishGaugeFloat64(name string, metric metrics.GaugeFloat64) {
   108  	exp.getFloat(name).Set(metric.Value())
   109  }
   110  
   111  func (exp *exp) publishHistogram(name string, metric metrics.Histogram) {
   112  	h := metric.Snapshot()
   113  	ps := h.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
   114  	exp.getInt(name + ".count").Set(h.Count())
   115  	exp.getFloat(name + ".min").Set(float64(h.Min()))
   116  	exp.getFloat(name + ".max").Set(float64(h.Max()))
   117  	exp.getFloat(name + ".mean").Set(h.Mean())
   118  	exp.getFloat(name + ".std-dev").Set(h.StdDev())
   119  	exp.getFloat(name + ".50-percentile").Set(ps[0])
   120  	exp.getFloat(name + ".75-percentile").Set(ps[1])
   121  	exp.getFloat(name + ".95-percentile").Set(ps[2])
   122  	exp.getFloat(name + ".99-percentile").Set(ps[3])
   123  	exp.getFloat(name + ".999-percentile").Set(ps[4])
   124  }
   125  
   126  func (exp *exp) publishMeter(name string, metric metrics.Meter) {
   127  	m := metric.Snapshot()
   128  	exp.getInt(name + ".count").Set(m.Count())
   129  	exp.getFloat(name + ".one-minute").Set(m.Rate1())
   130  	exp.getFloat(name + ".five-minute").Set(m.Rate5())
   131  	exp.getFloat(name + ".fifteen-minute").Set(m.Rate15())
   132  	exp.getFloat(name + ".mean").Set(m.RateMean())
   133  }
   134  
   135  func (exp *exp) publishTimer(name string, metric metrics.Timer) {
   136  	t := metric.Snapshot()
   137  	ps := t.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
   138  	exp.getInt(name + ".count").Set(t.Count())
   139  	exp.getFloat(name + ".min").Set(float64(t.Min()))
   140  	exp.getFloat(name + ".max").Set(float64(t.Max()))
   141  	exp.getFloat(name + ".mean").Set(t.Mean())
   142  	exp.getFloat(name + ".std-dev").Set(t.StdDev())
   143  	exp.getFloat(name + ".50-percentile").Set(ps[0])
   144  	exp.getFloat(name + ".75-percentile").Set(ps[1])
   145  	exp.getFloat(name + ".95-percentile").Set(ps[2])
   146  	exp.getFloat(name + ".99-percentile").Set(ps[3])
   147  	exp.getFloat(name + ".999-percentile").Set(ps[4])
   148  	exp.getFloat(name + ".one-minute").Set(t.Rate1())
   149  	exp.getFloat(name + ".five-minute").Set(t.Rate5())
   150  	exp.getFloat(name + ".fifteen-minute").Set(t.Rate15())
   151  	exp.getFloat(name + ".mean-rate").Set(t.RateMean())
   152  }
   153  
   154  func (exp *exp) publishResettingTimer(name string, metric metrics.ResettingTimer) {
   155  	t := metric.Snapshot()
   156  	ps := t.Percentiles([]float64{50, 75, 95, 99})
   157  	exp.getInt(name + ".count").Set(int64(len(t.Values())))
   158  	exp.getFloat(name + ".mean").Set(t.Mean())
   159  	exp.getInt(name + ".50-percentile").Set(ps[0])
   160  	exp.getInt(name + ".75-percentile").Set(ps[1])
   161  	exp.getInt(name + ".95-percentile").Set(ps[2])
   162  	exp.getInt(name + ".99-percentile").Set(ps[3])
   163  }
   164  
   165  func (exp *exp) syncToExpvar() {
   166  	exp.registry.Each(func(name string, i interface{}) {
   167  		switch i := i.(type) {
   168  		case metrics.Counter:
   169  			exp.publishCounter(name, i)
   170  		case metrics.Gauge:
   171  			exp.publishGauge(name, i)
   172  		case metrics.GaugeFloat64:
   173  			exp.publishGaugeFloat64(name, i)
   174  		case metrics.Histogram:
   175  			exp.publishHistogram(name, i)
   176  		case metrics.Meter:
   177  			exp.publishMeter(name, i)
   178  		case metrics.Timer:
   179  			exp.publishTimer(name, i)
   180  		case metrics.ResettingTimer:
   181  			exp.publishResettingTimer(name, i)
   182  		default:
   183  			panic(fmt.Sprintf("unsupported type for '%s': %T", name, i))
   184  		}
   185  	})
   186  }