github.com/theQRL/go-zond@v0.1.1/metrics/log.go (about) 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.Snapshot().Count()) 27 case CounterFloat64: 28 l.Printf("counter %s\n", name) 29 l.Printf(" count: %f\n", metric.Snapshot().Count()) 30 case Gauge: 31 l.Printf("gauge %s\n", name) 32 l.Printf(" value: %9d\n", metric.Snapshot().Value()) 33 case GaugeFloat64: 34 l.Printf("gauge %s\n", name) 35 l.Printf(" value: %f\n", metric.Snapshot().Value()) 36 case GaugeInfo: 37 l.Printf("gauge %s\n", name) 38 l.Printf(" value: %s\n", metric.Snapshot().Value()) 39 case Healthcheck: 40 metric.Check() 41 l.Printf("healthcheck %s\n", name) 42 l.Printf(" error: %v\n", metric.Error()) 43 case Histogram: 44 h := metric.Snapshot() 45 ps := h.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999}) 46 l.Printf("histogram %s\n", name) 47 l.Printf(" count: %9d\n", h.Count()) 48 l.Printf(" min: %9d\n", h.Min()) 49 l.Printf(" max: %9d\n", h.Max()) 50 l.Printf(" mean: %12.2f\n", h.Mean()) 51 l.Printf(" stddev: %12.2f\n", h.StdDev()) 52 l.Printf(" median: %12.2f\n", ps[0]) 53 l.Printf(" 75%%: %12.2f\n", ps[1]) 54 l.Printf(" 95%%: %12.2f\n", ps[2]) 55 l.Printf(" 99%%: %12.2f\n", ps[3]) 56 l.Printf(" 99.9%%: %12.2f\n", ps[4]) 57 case Meter: 58 m := metric.Snapshot() 59 l.Printf("meter %s\n", name) 60 l.Printf(" count: %9d\n", m.Count()) 61 l.Printf(" 1-min rate: %12.2f\n", m.Rate1()) 62 l.Printf(" 5-min rate: %12.2f\n", m.Rate5()) 63 l.Printf(" 15-min rate: %12.2f\n", m.Rate15()) 64 l.Printf(" mean rate: %12.2f\n", m.RateMean()) 65 case Timer: 66 t := metric.Snapshot() 67 ps := t.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999}) 68 l.Printf("timer %s\n", name) 69 l.Printf(" count: %9d\n", t.Count()) 70 l.Printf(" min: %12.2f%s\n", float64(t.Min())/du, duSuffix) 71 l.Printf(" max: %12.2f%s\n", float64(t.Max())/du, duSuffix) 72 l.Printf(" mean: %12.2f%s\n", t.Mean()/du, duSuffix) 73 l.Printf(" stddev: %12.2f%s\n", t.StdDev()/du, duSuffix) 74 l.Printf(" median: %12.2f%s\n", ps[0]/du, duSuffix) 75 l.Printf(" 75%%: %12.2f%s\n", ps[1]/du, duSuffix) 76 l.Printf(" 95%%: %12.2f%s\n", ps[2]/du, duSuffix) 77 l.Printf(" 99%%: %12.2f%s\n", ps[3]/du, duSuffix) 78 l.Printf(" 99.9%%: %12.2f%s\n", ps[4]/du, duSuffix) 79 l.Printf(" 1-min rate: %12.2f\n", t.Rate1()) 80 l.Printf(" 5-min rate: %12.2f\n", t.Rate5()) 81 l.Printf(" 15-min rate: %12.2f\n", t.Rate15()) 82 l.Printf(" mean rate: %12.2f\n", t.RateMean()) 83 } 84 }) 85 } 86 }