log.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package metrics
  2. import (
  3. "time"
  4. )
  5. type Logger interface {
  6. Printf(format string, v ...interface{})
  7. }
  8. // Log outputs each metric in the given registry periodically using the given logger.
  9. func Log(r Registry, freq time.Duration, l Logger) {
  10. LogScaled(r, freq, time.Nanosecond, l)
  11. }
  12. // LogOnCue outputs each metric in the given registry on demand through the channel
  13. // using the given logger
  14. func LogOnCue(r Registry, ch chan interface{}, l Logger) {
  15. LogScaledOnCue(r, ch, time.Nanosecond, l)
  16. }
  17. // LogScaled outputs each metric in the given registry periodically using the given
  18. // logger. Print timings in `scale` units (eg time.Millisecond) rather than nanos.
  19. func LogScaled(r Registry, freq time.Duration, scale time.Duration, l Logger) {
  20. ch := make(chan interface{})
  21. go func(channel chan interface{}) {
  22. for _ = range time.Tick(freq) {
  23. channel <- struct{}{}
  24. }
  25. }(ch)
  26. LogScaledOnCue(r, ch, scale, l)
  27. }
  28. // LogScaledOnCue outputs each metric in the given registry on demand through the channel
  29. // using the given logger. Print timings in `scale` units (eg time.Millisecond) rather
  30. // than nanos.
  31. func LogScaledOnCue(r Registry, ch chan interface{}, scale time.Duration, l Logger) {
  32. du := float64(scale)
  33. duSuffix := scale.String()[1:]
  34. for _ = range ch {
  35. r.Each(func(name string, i interface{}) {
  36. switch metric := i.(type) {
  37. case Counter:
  38. l.Printf("counter %s\n", name)
  39. l.Printf(" count: %9d\n", metric.Count())
  40. case Gauge:
  41. l.Printf("gauge %s\n", name)
  42. l.Printf(" value: %9d\n", metric.Value())
  43. case GaugeFloat64:
  44. l.Printf("gauge %s\n", name)
  45. l.Printf(" value: %f\n", metric.Value())
  46. case Healthcheck:
  47. metric.Check()
  48. l.Printf("healthcheck %s\n", name)
  49. l.Printf(" error: %v\n", metric.Error())
  50. case Histogram:
  51. h := metric.Snapshot()
  52. ps := h.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
  53. l.Printf("histogram %s\n", name)
  54. l.Printf(" count: %9d\n", h.Count())
  55. l.Printf(" min: %9d\n", h.Min())
  56. l.Printf(" max: %9d\n", h.Max())
  57. l.Printf(" mean: %12.2f\n", h.Mean())
  58. l.Printf(" stddev: %12.2f\n", h.StdDev())
  59. l.Printf(" median: %12.2f\n", ps[0])
  60. l.Printf(" 75%%: %12.2f\n", ps[1])
  61. l.Printf(" 95%%: %12.2f\n", ps[2])
  62. l.Printf(" 99%%: %12.2f\n", ps[3])
  63. l.Printf(" 99.9%%: %12.2f\n", ps[4])
  64. case Meter:
  65. m := metric.Snapshot()
  66. l.Printf("meter %s\n", name)
  67. l.Printf(" count: %9d\n", m.Count())
  68. l.Printf(" 1-min rate: %12.2f\n", m.Rate1())
  69. l.Printf(" 5-min rate: %12.2f\n", m.Rate5())
  70. l.Printf(" 15-min rate: %12.2f\n", m.Rate15())
  71. l.Printf(" mean rate: %12.2f\n", m.RateMean())
  72. case Timer:
  73. t := metric.Snapshot()
  74. ps := t.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
  75. l.Printf("timer %s\n", name)
  76. l.Printf(" count: %9d\n", t.Count())
  77. l.Printf(" min: %12.2f%s\n", float64(t.Min())/du, duSuffix)
  78. l.Printf(" max: %12.2f%s\n", float64(t.Max())/du, duSuffix)
  79. l.Printf(" mean: %12.2f%s\n", t.Mean()/du, duSuffix)
  80. l.Printf(" stddev: %12.2f%s\n", t.StdDev()/du, duSuffix)
  81. l.Printf(" median: %12.2f%s\n", ps[0]/du, duSuffix)
  82. l.Printf(" 75%%: %12.2f%s\n", ps[1]/du, duSuffix)
  83. l.Printf(" 95%%: %12.2f%s\n", ps[2]/du, duSuffix)
  84. l.Printf(" 99%%: %12.2f%s\n", ps[3]/du, duSuffix)
  85. l.Printf(" 99.9%%: %12.2f%s\n", ps[4]/du, duSuffix)
  86. l.Printf(" 1-min rate: %12.2f\n", t.Rate1())
  87. l.Printf(" 5-min rate: %12.2f\n", t.Rate5())
  88. l.Printf(" 15-min rate: %12.2f\n", t.Rate15())
  89. l.Printf(" mean rate: %12.2f\n", t.RateMean())
  90. }
  91. })
  92. }
  93. }