blob: 2614a0a33eb70e85d30e4b5e6c755f5ec1d281e0 [file] [log] [blame]
Scott Bakered4efab2020-01-13 19:12:25 -08001package metrics
2
3import (
4 "time"
5)
6
7type Logger interface {
8 Printf(format string, v ...interface{})
9}
10
divyadesai19009132020-03-04 12:58:08 +000011// Log outputs each metric in the given registry periodically using the given logger.
Scott Bakered4efab2020-01-13 19:12:25 -080012func Log(r Registry, freq time.Duration, l Logger) {
13 LogScaled(r, freq, time.Nanosecond, l)
14}
15
divyadesai19009132020-03-04 12:58:08 +000016// LogOnCue outputs each metric in the given registry on demand through the channel
17// using the given logger
18func LogOnCue(r Registry, ch chan interface{}, l Logger) {
19 LogScaledOnCue(r, ch, time.Nanosecond, l)
20}
21
22// LogScaled outputs each metric in the given registry periodically using the given
Scott Bakered4efab2020-01-13 19:12:25 -080023// logger. Print timings in `scale` units (eg time.Millisecond) rather than nanos.
24func LogScaled(r Registry, freq time.Duration, scale time.Duration, l Logger) {
divyadesai19009132020-03-04 12:58:08 +000025 ch := make(chan interface{})
26 go func(channel chan interface{}) {
27 for _ = range time.Tick(freq) {
28 channel <- struct{}{}
29 }
30 }(ch)
31 LogScaledOnCue(r, ch, scale, l)
32}
33
34// LogScaledOnCue outputs each metric in the given registry on demand through the channel
35// using the given logger. Print timings in `scale` units (eg time.Millisecond) rather
36// than nanos.
37func LogScaledOnCue(r Registry, ch chan interface{}, scale time.Duration, l Logger) {
Scott Bakered4efab2020-01-13 19:12:25 -080038 du := float64(scale)
39 duSuffix := scale.String()[1:]
40
divyadesai19009132020-03-04 12:58:08 +000041 for _ = range ch {
Scott Bakered4efab2020-01-13 19:12:25 -080042 r.Each(func(name string, i interface{}) {
43 switch metric := i.(type) {
44 case Counter:
45 l.Printf("counter %s\n", name)
46 l.Printf(" count: %9d\n", metric.Count())
47 case Gauge:
48 l.Printf("gauge %s\n", name)
49 l.Printf(" value: %9d\n", metric.Value())
50 case GaugeFloat64:
51 l.Printf("gauge %s\n", name)
52 l.Printf(" value: %f\n", metric.Value())
53 case Healthcheck:
54 metric.Check()
55 l.Printf("healthcheck %s\n", name)
56 l.Printf(" error: %v\n", metric.Error())
57 case Histogram:
58 h := metric.Snapshot()
59 ps := h.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
60 l.Printf("histogram %s\n", name)
61 l.Printf(" count: %9d\n", h.Count())
62 l.Printf(" min: %9d\n", h.Min())
63 l.Printf(" max: %9d\n", h.Max())
64 l.Printf(" mean: %12.2f\n", h.Mean())
65 l.Printf(" stddev: %12.2f\n", h.StdDev())
66 l.Printf(" median: %12.2f\n", ps[0])
67 l.Printf(" 75%%: %12.2f\n", ps[1])
68 l.Printf(" 95%%: %12.2f\n", ps[2])
69 l.Printf(" 99%%: %12.2f\n", ps[3])
70 l.Printf(" 99.9%%: %12.2f\n", ps[4])
71 case Meter:
72 m := metric.Snapshot()
73 l.Printf("meter %s\n", name)
74 l.Printf(" count: %9d\n", m.Count())
75 l.Printf(" 1-min rate: %12.2f\n", m.Rate1())
76 l.Printf(" 5-min rate: %12.2f\n", m.Rate5())
77 l.Printf(" 15-min rate: %12.2f\n", m.Rate15())
78 l.Printf(" mean rate: %12.2f\n", m.RateMean())
79 case Timer:
80 t := metric.Snapshot()
81 ps := t.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
82 l.Printf("timer %s\n", name)
83 l.Printf(" count: %9d\n", t.Count())
84 l.Printf(" min: %12.2f%s\n", float64(t.Min())/du, duSuffix)
85 l.Printf(" max: %12.2f%s\n", float64(t.Max())/du, duSuffix)
86 l.Printf(" mean: %12.2f%s\n", t.Mean()/du, duSuffix)
87 l.Printf(" stddev: %12.2f%s\n", t.StdDev()/du, duSuffix)
88 l.Printf(" median: %12.2f%s\n", ps[0]/du, duSuffix)
89 l.Printf(" 75%%: %12.2f%s\n", ps[1]/du, duSuffix)
90 l.Printf(" 95%%: %12.2f%s\n", ps[2]/du, duSuffix)
91 l.Printf(" 99%%: %12.2f%s\n", ps[3]/du, duSuffix)
92 l.Printf(" 99.9%%: %12.2f%s\n", ps[4]/du, duSuffix)
93 l.Printf(" 1-min rate: %12.2f\n", t.Rate1())
94 l.Printf(" 5-min rate: %12.2f\n", t.Rate5())
95 l.Printf(" 15-min rate: %12.2f\n", t.Rate15())
96 l.Printf(" mean rate: %12.2f\n", t.RateMean())
97 }
98 })
99 }
100}