syslog.go 1.9 KB

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