log.go 2.9 KB

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