writer.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package metrics
  2. import (
  3. "fmt"
  4. "io"
  5. "sort"
  6. "time"
  7. )
  8. // Write sorts writes each metric in the given registry periodically to the
  9. // given io.Writer.
  10. func Write(r Registry, d time.Duration, w io.Writer) {
  11. for _ = range time.Tick(d) {
  12. WriteOnce(r, w)
  13. }
  14. }
  15. // WriteOnce sorts and writes metrics in the given registry to the given
  16. // io.Writer.
  17. func WriteOnce(r Registry, w io.Writer) {
  18. var namedMetrics namedMetricSlice
  19. r.Each(func(name string, i interface{}) {
  20. namedMetrics = append(namedMetrics, namedMetric{name, i})
  21. })
  22. sort.Sort(namedMetrics)
  23. for _, namedMetric := range namedMetrics {
  24. switch metric := namedMetric.m.(type) {
  25. case Counter:
  26. fmt.Fprintf(w, "counter %s\n", namedMetric.name)
  27. fmt.Fprintf(w, " count: %9d\n", metric.Count())
  28. case Gauge:
  29. fmt.Fprintf(w, "gauge %s\n", namedMetric.name)
  30. fmt.Fprintf(w, " value: %9d\n", metric.Value())
  31. case GaugeFloat64:
  32. fmt.Fprintf(w, "gauge %s\n", namedMetric.name)
  33. fmt.Fprintf(w, " value: %f\n", metric.Value())
  34. case Healthcheck:
  35. metric.Check()
  36. fmt.Fprintf(w, "healthcheck %s\n", namedMetric.name)
  37. fmt.Fprintf(w, " error: %v\n", metric.Error())
  38. case Histogram:
  39. h := metric.Snapshot()
  40. ps := h.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
  41. fmt.Fprintf(w, "histogram %s\n", namedMetric.name)
  42. fmt.Fprintf(w, " count: %9d\n", h.Count())
  43. fmt.Fprintf(w, " min: %9d\n", h.Min())
  44. fmt.Fprintf(w, " max: %9d\n", h.Max())
  45. fmt.Fprintf(w, " mean: %12.2f\n", h.Mean())
  46. fmt.Fprintf(w, " stddev: %12.2f\n", h.StdDev())
  47. fmt.Fprintf(w, " median: %12.2f\n", ps[0])
  48. fmt.Fprintf(w, " 75%%: %12.2f\n", ps[1])
  49. fmt.Fprintf(w, " 95%%: %12.2f\n", ps[2])
  50. fmt.Fprintf(w, " 99%%: %12.2f\n", ps[3])
  51. fmt.Fprintf(w, " 99.9%%: %12.2f\n", ps[4])
  52. case Meter:
  53. m := metric.Snapshot()
  54. fmt.Fprintf(w, "meter %s\n", namedMetric.name)
  55. fmt.Fprintf(w, " count: %9d\n", m.Count())
  56. fmt.Fprintf(w, " 1-min rate: %12.2f\n", m.Rate1())
  57. fmt.Fprintf(w, " 5-min rate: %12.2f\n", m.Rate5())
  58. fmt.Fprintf(w, " 15-min rate: %12.2f\n", m.Rate15())
  59. fmt.Fprintf(w, " mean rate: %12.2f\n", m.RateMean())
  60. case Timer:
  61. t := metric.Snapshot()
  62. ps := t.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
  63. fmt.Fprintf(w, "timer %s\n", namedMetric.name)
  64. fmt.Fprintf(w, " count: %9d\n", t.Count())
  65. fmt.Fprintf(w, " min: %9d\n", t.Min())
  66. fmt.Fprintf(w, " max: %9d\n", t.Max())
  67. fmt.Fprintf(w, " mean: %12.2f\n", t.Mean())
  68. fmt.Fprintf(w, " stddev: %12.2f\n", t.StdDev())
  69. fmt.Fprintf(w, " median: %12.2f\n", ps[0])
  70. fmt.Fprintf(w, " 75%%: %12.2f\n", ps[1])
  71. fmt.Fprintf(w, " 95%%: %12.2f\n", ps[2])
  72. fmt.Fprintf(w, " 99%%: %12.2f\n", ps[3])
  73. fmt.Fprintf(w, " 99.9%%: %12.2f\n", ps[4])
  74. fmt.Fprintf(w, " 1-min rate: %12.2f\n", t.Rate1())
  75. fmt.Fprintf(w, " 5-min rate: %12.2f\n", t.Rate5())
  76. fmt.Fprintf(w, " 15-min rate: %12.2f\n", t.Rate15())
  77. fmt.Fprintf(w, " mean rate: %12.2f\n", t.RateMean())
  78. }
  79. }
  80. }
  81. type namedMetric struct {
  82. name string
  83. m interface{}
  84. }
  85. // namedMetricSlice is a slice of namedMetrics that implements sort.Interface.
  86. type namedMetricSlice []namedMetric
  87. func (nms namedMetricSlice) Len() int { return len(nms) }
  88. func (nms namedMetricSlice) Swap(i, j int) { nms[i], nms[j] = nms[j], nms[i] }
  89. func (nms namedMetricSlice) Less(i, j int) bool {
  90. return nms[i].name < nms[j].name
  91. }