exp.go 5.3 KB

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