writer.go 3.5 KB

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