syslog.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // +build !windows
  2. package metrics
  3. import (
  4. "fmt"
  5. "log/syslog"
  6. "time"
  7. )
  8. // Output each metric in the given registry to syslog periodically using
  9. // the given syslogger.
  10. func Syslog(r Registry, d time.Duration, w *syslog.Writer) {
  11. for {
  12. r.Each(func(name string, i interface{}) {
  13. switch metric := i.(type) {
  14. case Counter:
  15. w.Info(fmt.Sprintf("counter %s: count: %d", name, metric.Count()))
  16. case Gauge:
  17. w.Info(fmt.Sprintf("gauge %s: value: %d", name, metric.Value()))
  18. case Healthcheck:
  19. metric.Check()
  20. w.Info(fmt.Sprintf("healthcheck %s: error: %v", name, metric.Error()))
  21. case Histogram:
  22. h := metric.Snapshot()
  23. ps := h.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
  24. w.Info(fmt.Sprintf(
  25. "histogram %s: count: %d min: %d max: %d mean: %.2f stddev: %.2f median: %.2f 75%%: %.2f 95%%: %.2f 99%%: %.2f 99.9%%: %.2f",
  26. name,
  27. h.Count(),
  28. h.Min(),
  29. h.Max(),
  30. h.Mean(),
  31. h.StdDev(),
  32. ps[0],
  33. ps[1],
  34. ps[2],
  35. ps[3],
  36. ps[4],
  37. ))
  38. case Meter:
  39. m := metric.Snapshot()
  40. w.Info(fmt.Sprintf(
  41. "meter %s: count: %d 1-min: %.2f 5-min: %.2f 15-min: %.2f mean: %.2f",
  42. name,
  43. m.Count(),
  44. m.Rate1(),
  45. m.Rate5(),
  46. m.Rate15(),
  47. m.RateMean(),
  48. ))
  49. case Timer:
  50. t := metric.Snapshot()
  51. ps := t.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
  52. w.Info(fmt.Sprintf(
  53. "timer %s: count: %d min: %d max: %d mean: %.2f stddev: %.2f median: %.2f 75%%: %.2f 95%%: %.2f 99%%: %.2f 99.9%%: %.2f 1-min: %.2f 5-min: %.2f 15-min: %.2f mean-rate: %.2f",
  54. name,
  55. t.Count(),
  56. t.Min(),
  57. t.Max(),
  58. t.Mean(),
  59. t.StdDev(),
  60. ps[0],
  61. ps[1],
  62. ps[2],
  63. ps[3],
  64. ps[4],
  65. t.Rate1(),
  66. t.Rate5(),
  67. t.Rate15(),
  68. t.RateMean(),
  69. ))
  70. }
  71. })
  72. time.Sleep(d)
  73. }
  74. }