github.com/calmw/ethereum@v0.1.1/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/calmw/ethereum/log" 12 "github.com/calmw/ethereum/metrics" 13 "github.com/calmw/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) publishCounterFloat64(name string, metric metrics.CounterFloat64) { 104 v := exp.getFloat(name) 105 v.Set(metric.Count()) 106 } 107 108 func (exp *exp) publishGauge(name string, metric metrics.Gauge) { 109 v := exp.getInt(name) 110 v.Set(metric.Value()) 111 } 112 func (exp *exp) publishGaugeFloat64(name string, metric metrics.GaugeFloat64) { 113 exp.getFloat(name).Set(metric.Value()) 114 } 115 116 func (exp *exp) publishHistogram(name string, metric metrics.Histogram) { 117 h := metric.Snapshot() 118 ps := h.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999}) 119 exp.getInt(name + ".count").Set(h.Count()) 120 exp.getFloat(name + ".min").Set(float64(h.Min())) 121 exp.getFloat(name + ".max").Set(float64(h.Max())) 122 exp.getFloat(name + ".mean").Set(h.Mean()) 123 exp.getFloat(name + ".std-dev").Set(h.StdDev()) 124 exp.getFloat(name + ".50-percentile").Set(ps[0]) 125 exp.getFloat(name + ".75-percentile").Set(ps[1]) 126 exp.getFloat(name + ".95-percentile").Set(ps[2]) 127 exp.getFloat(name + ".99-percentile").Set(ps[3]) 128 exp.getFloat(name + ".999-percentile").Set(ps[4]) 129 } 130 131 func (exp *exp) publishMeter(name string, metric metrics.Meter) { 132 m := metric.Snapshot() 133 exp.getInt(name + ".count").Set(m.Count()) 134 exp.getFloat(name + ".one-minute").Set(m.Rate1()) 135 exp.getFloat(name + ".five-minute").Set(m.Rate5()) 136 exp.getFloat(name + ".fifteen-minute").Set(m.Rate15()) 137 exp.getFloat(name + ".mean").Set(m.RateMean()) 138 } 139 140 func (exp *exp) publishTimer(name string, metric metrics.Timer) { 141 t := metric.Snapshot() 142 ps := t.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999}) 143 exp.getInt(name + ".count").Set(t.Count()) 144 exp.getFloat(name + ".min").Set(float64(t.Min())) 145 exp.getFloat(name + ".max").Set(float64(t.Max())) 146 exp.getFloat(name + ".mean").Set(t.Mean()) 147 exp.getFloat(name + ".std-dev").Set(t.StdDev()) 148 exp.getFloat(name + ".50-percentile").Set(ps[0]) 149 exp.getFloat(name + ".75-percentile").Set(ps[1]) 150 exp.getFloat(name + ".95-percentile").Set(ps[2]) 151 exp.getFloat(name + ".99-percentile").Set(ps[3]) 152 exp.getFloat(name + ".999-percentile").Set(ps[4]) 153 exp.getFloat(name + ".one-minute").Set(t.Rate1()) 154 exp.getFloat(name + ".five-minute").Set(t.Rate5()) 155 exp.getFloat(name + ".fifteen-minute").Set(t.Rate15()) 156 exp.getFloat(name + ".mean-rate").Set(t.RateMean()) 157 } 158 159 func (exp *exp) publishResettingTimer(name string, metric metrics.ResettingTimer) { 160 t := metric.Snapshot() 161 ps := t.Percentiles([]float64{50, 75, 95, 99}) 162 exp.getInt(name + ".count").Set(int64(len(t.Values()))) 163 exp.getFloat(name + ".mean").Set(t.Mean()) 164 exp.getInt(name + ".50-percentile").Set(ps[0]) 165 exp.getInt(name + ".75-percentile").Set(ps[1]) 166 exp.getInt(name + ".95-percentile").Set(ps[2]) 167 exp.getInt(name + ".99-percentile").Set(ps[3]) 168 } 169 170 func (exp *exp) syncToExpvar() { 171 exp.registry.Each(func(name string, i interface{}) { 172 switch i := i.(type) { 173 case metrics.Counter: 174 exp.publishCounter(name, i) 175 case metrics.CounterFloat64: 176 exp.publishCounterFloat64(name, i) 177 case metrics.Gauge: 178 exp.publishGauge(name, i) 179 case metrics.GaugeFloat64: 180 exp.publishGaugeFloat64(name, i) 181 case metrics.Histogram: 182 exp.publishHistogram(name, i) 183 case metrics.Meter: 184 exp.publishMeter(name, i) 185 case metrics.Timer: 186 exp.publishTimer(name, i) 187 case metrics.ResettingTimer: 188 exp.publishResettingTimer(name, i) 189 default: 190 panic(fmt.Sprintf("unsupported type for '%s': %T", name, i)) 191 } 192 }) 193 }