exp.go 5.3 KB

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