log.go 2.9 KB

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