syslog.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 m := i.(type) {
  14. case Counter:
  15. w.Info(fmt.Sprintf("counter %s: count: %d", name, m.Count()))
  16. case Gauge:
  17. w.Info(fmt.Sprintf("gauge %s: value: %d", name, m.Value()))
  18. case Healthcheck:
  19. m.Check()
  20. w.Info(fmt.Sprintf("healthcheck %s: error: %v", name, m.Error()))
  21. case Histogram:
  22. ps := m.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
  23. w.Info(fmt.Sprintf(
  24. "histogram %s: count: %d min: %d max: %d mean: %.2f stddev: %.2f median: %.2f 75%%: %.2f 95%%: %.2f 99%%: %.2f 99.9%%: %.2f",
  25. name,
  26. m.Count(),
  27. m.Min(),
  28. m.Max(),
  29. m.Mean(),
  30. m.StdDev(),
  31. ps[0],
  32. ps[1],
  33. ps[2],
  34. ps[3],
  35. ps[4],
  36. ))
  37. case Meter:
  38. w.Info(fmt.Sprintf(
  39. "meter %s: count: %d 1-min: %.2f 5-min: %.2f 15-min: %.2f mean: %.2f",
  40. name,
  41. m.Count(),
  42. m.Rate1(),
  43. m.Rate5(),
  44. m.Rate15(),
  45. m.RateMean(),
  46. ))
  47. case Timer:
  48. ps := m.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
  49. w.Info(fmt.Sprintf(
  50. "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",
  51. name,
  52. m.Count(),
  53. m.Min(),
  54. m.Max(),
  55. m.Mean(),
  56. m.StdDev(),
  57. ps[0],
  58. ps[1],
  59. ps[2],
  60. ps[3],
  61. ps[4],
  62. m.Rate1(),
  63. m.Rate5(),
  64. m.Rate15(),
  65. m.RateMean(),
  66. ))
  67. }
  68. })
  69. time.Sleep(d)
  70. }
  71. }