github.com/calmw/ethereum@v0.1.1/metrics/writer.go (about) 1 package metrics 2 3 import ( 4 "fmt" 5 "io" 6 "sort" 7 "time" 8 ) 9 10 // Write sorts writes each metric in the given registry periodically to the 11 // given io.Writer. 12 func Write(r Registry, d time.Duration, w io.Writer) { 13 for range time.Tick(d) { 14 WriteOnce(r, w) 15 } 16 } 17 18 // WriteOnce sorts and writes metrics in the given registry to the given 19 // io.Writer. 20 func WriteOnce(r Registry, w io.Writer) { 21 var namedMetrics namedMetricSlice 22 r.Each(func(name string, i interface{}) { 23 namedMetrics = append(namedMetrics, namedMetric{name, i}) 24 }) 25 26 sort.Sort(namedMetrics) 27 for _, namedMetric := range namedMetrics { 28 switch metric := namedMetric.m.(type) { 29 case Counter: 30 fmt.Fprintf(w, "counter %s\n", namedMetric.name) 31 fmt.Fprintf(w, " count: %9d\n", metric.Count()) 32 case CounterFloat64: 33 fmt.Fprintf(w, "counter %s\n", namedMetric.name) 34 fmt.Fprintf(w, " count: %f\n", metric.Count()) 35 case Gauge: 36 fmt.Fprintf(w, "gauge %s\n", namedMetric.name) 37 fmt.Fprintf(w, " value: %9d\n", metric.Value()) 38 case GaugeFloat64: 39 fmt.Fprintf(w, "gauge %s\n", namedMetric.name) 40 fmt.Fprintf(w, " value: %f\n", metric.Value()) 41 case Healthcheck: 42 metric.Check() 43 fmt.Fprintf(w, "healthcheck %s\n", namedMetric.name) 44 fmt.Fprintf(w, " error: %v\n", metric.Error()) 45 case Histogram: 46 h := metric.Snapshot() 47 ps := h.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999}) 48 fmt.Fprintf(w, "histogram %s\n", namedMetric.name) 49 fmt.Fprintf(w, " count: %9d\n", h.Count()) 50 fmt.Fprintf(w, " min: %9d\n", h.Min()) 51 fmt.Fprintf(w, " max: %9d\n", h.Max()) 52 fmt.Fprintf(w, " mean: %12.2f\n", h.Mean()) 53 fmt.Fprintf(w, " stddev: %12.2f\n", h.StdDev()) 54 fmt.Fprintf(w, " median: %12.2f\n", ps[0]) 55 fmt.Fprintf(w, " 75%%: %12.2f\n", ps[1]) 56 fmt.Fprintf(w, " 95%%: %12.2f\n", ps[2]) 57 fmt.Fprintf(w, " 99%%: %12.2f\n", ps[3]) 58 fmt.Fprintf(w, " 99.9%%: %12.2f\n", ps[4]) 59 case Meter: 60 m := metric.Snapshot() 61 fmt.Fprintf(w, "meter %s\n", namedMetric.name) 62 fmt.Fprintf(w, " count: %9d\n", m.Count()) 63 fmt.Fprintf(w, " 1-min rate: %12.2f\n", m.Rate1()) 64 fmt.Fprintf(w, " 5-min rate: %12.2f\n", m.Rate5()) 65 fmt.Fprintf(w, " 15-min rate: %12.2f\n", m.Rate15()) 66 fmt.Fprintf(w, " mean rate: %12.2f\n", m.RateMean()) 67 case Timer: 68 t := metric.Snapshot() 69 ps := t.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999}) 70 fmt.Fprintf(w, "timer %s\n", namedMetric.name) 71 fmt.Fprintf(w, " count: %9d\n", t.Count()) 72 fmt.Fprintf(w, " min: %9d\n", t.Min()) 73 fmt.Fprintf(w, " max: %9d\n", t.Max()) 74 fmt.Fprintf(w, " mean: %12.2f\n", t.Mean()) 75 fmt.Fprintf(w, " stddev: %12.2f\n", t.StdDev()) 76 fmt.Fprintf(w, " median: %12.2f\n", ps[0]) 77 fmt.Fprintf(w, " 75%%: %12.2f\n", ps[1]) 78 fmt.Fprintf(w, " 95%%: %12.2f\n", ps[2]) 79 fmt.Fprintf(w, " 99%%: %12.2f\n", ps[3]) 80 fmt.Fprintf(w, " 99.9%%: %12.2f\n", ps[4]) 81 fmt.Fprintf(w, " 1-min rate: %12.2f\n", t.Rate1()) 82 fmt.Fprintf(w, " 5-min rate: %12.2f\n", t.Rate5()) 83 fmt.Fprintf(w, " 15-min rate: %12.2f\n", t.Rate15()) 84 fmt.Fprintf(w, " mean rate: %12.2f\n", t.RateMean()) 85 } 86 } 87 } 88 89 type namedMetric struct { 90 name string 91 m interface{} 92 } 93 94 // namedMetricSlice is a slice of namedMetrics that implements sort.Interface. 95 type namedMetricSlice []namedMetric 96 97 func (nms namedMetricSlice) Len() int { return len(nms) } 98 99 func (nms namedMetricSlice) Swap(i, j int) { nms[i], nms[j] = nms[j], nms[i] } 100 101 func (nms namedMetricSlice) Less(i, j int) bool { 102 return nms[i].name < nms[j].name 103 }