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