json.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package metrics
  2. import (
  3. "encoding/json"
  4. "io"
  5. "time"
  6. )
  7. // MarshalJSON returns a byte slice containing a JSON representation of all
  8. // the metrics in the Registry.
  9. func (r *StandardRegistry) MarshalJSON() ([]byte, error) {
  10. return json.Marshal(r.Dump())
  11. }
  12. // Dump all the metrics in the Registry
  13. func (r *StandardRegistry) Dump() map[string]map[string]interface{} {
  14. data := make(map[string]map[string]interface{})
  15. r.Each(func(name string, i interface{}) {
  16. values := make(map[string]interface{})
  17. switch metric := i.(type) {
  18. case Counter:
  19. values["count"] = metric.Count()
  20. case Gauge:
  21. values["value"] = metric.Value()
  22. case GaugeFloat64:
  23. values["value"] = metric.Value()
  24. case Healthcheck:
  25. values["error"] = nil
  26. metric.Check()
  27. if err := metric.Error(); nil != err {
  28. values["error"] = metric.Error().Error()
  29. }
  30. case Histogram:
  31. h := metric.Snapshot()
  32. ps := h.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
  33. values["count"] = h.Count()
  34. values["min"] = h.Min()
  35. values["max"] = h.Max()
  36. values["mean"] = h.Mean()
  37. values["stddev"] = h.StdDev()
  38. values["median"] = ps[0]
  39. values["75%"] = ps[1]
  40. values["95%"] = ps[2]
  41. values["99%"] = ps[3]
  42. values["99.9%"] = ps[4]
  43. case Meter:
  44. m := metric.Snapshot()
  45. values["count"] = m.Count()
  46. values["1m.rate"] = m.Rate1()
  47. values["5m.rate"] = m.Rate5()
  48. values["15m.rate"] = m.Rate15()
  49. values["mean.rate"] = m.RateMean()
  50. case Timer:
  51. t := metric.Snapshot()
  52. ps := t.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
  53. values["count"] = t.Count()
  54. values["min"] = t.Min()
  55. values["max"] = t.Max()
  56. values["mean"] = t.Mean()
  57. values["stddev"] = t.StdDev()
  58. values["median"] = ps[0]
  59. values["75%"] = ps[1]
  60. values["95%"] = ps[2]
  61. values["99%"] = ps[3]
  62. values["99.9%"] = ps[4]
  63. values["1m.rate"] = t.Rate1()
  64. values["5m.rate"] = t.Rate5()
  65. values["15m.rate"] = t.Rate15()
  66. values["mean.rate"] = t.RateMean()
  67. }
  68. data[name] = values
  69. })
  70. return data
  71. }
  72. // WriteJSON writes metrics from the given registry periodically to the
  73. // specified io.Writer as JSON.
  74. func WriteJSON(r Registry, d time.Duration, w io.Writer) {
  75. for _ = range time.Tick(d) {
  76. WriteJSONOnce(r, w)
  77. }
  78. }
  79. // WriteJSONOnce writes metrics from the given registry to the specified
  80. // io.Writer as JSON.
  81. func WriteJSONOnce(r Registry, w io.Writer) {
  82. json.NewEncoder(w).Encode(r)
  83. }
  84. func (p *PrefixedRegistry) MarshalJSON() ([]byte, error) {
  85. return json.Marshal(p.underlying)
  86. }