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