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