github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/utilities/metrics/writer.go (about)

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