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