github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/utilities/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  func LogScaled(r Registry, freq time.Duration, scale time.Duration, l Logger) {
    16  	du := float64(scale)
    17  	duSuffix := scale.String()[1:]
    18  
    19  	for range time.Tick(freq) {
    20  		r.Each(func(name string, i interface{}) {
    21  			switch metric := i.(type) {
    22  			case Counter:
    23  				l.Printf("counter %s\n", name)
    24  				l.Printf("  count:       %9d\n", metric.Count())
    25  			case Gauge:
    26  				l.Printf("gauge %s\n", name)
    27  				l.Printf("  value:       %9d\n", metric.Value())
    28  			case GaugeFloat64:
    29  				l.Printf("gauge %s\n", name)
    30  				l.Printf("  value:       %f\n", metric.Value())
    31  			case Healthcheck:
    32  				metric.Check()
    33  				l.Printf("healthcheck %s\n", name)
    34  				l.Printf("  error:       %v\n", metric.Error())
    35  			case Histogram:
    36  				h := metric.Snapshot()
    37  				ps := h.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
    38  				l.Printf("histogram %s\n", name)
    39  				l.Printf("  count:       %9d\n", h.Count())
    40  				l.Printf("  min:         %9d\n", h.Min())
    41  				l.Printf("  max:         %9d\n", h.Max())
    42  				l.Printf("  mean:        %12.2f\n", h.Mean())
    43  				l.Printf("  stddev:      %12.2f\n", h.StdDev())
    44  				l.Printf("  median:      %12.2f\n", ps[0])
    45  				l.Printf("  75%%:         %12.2f\n", ps[1])
    46  				l.Printf("  95%%:         %12.2f\n", ps[2])
    47  				l.Printf("  99%%:         %12.2f\n", ps[3])
    48  				l.Printf("  99.9%%:       %12.2f\n", ps[4])
    49  			case Meter:
    50  				m := metric.Snapshot()
    51  				l.Printf("meter %s\n", name)
    52  				l.Printf("  count:       %9d\n", m.Count())
    53  				l.Printf("  1-min rate:  %12.2f\n", m.Rate1())
    54  				l.Printf("  5-min rate:  %12.2f\n", m.Rate5())
    55  				l.Printf("  15-min rate: %12.2f\n", m.Rate15())
    56  				l.Printf("  mean rate:   %12.2f\n", m.RateMean())
    57  			case Timer:
    58  				t := metric.Snapshot()
    59  				ps := t.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
    60  				l.Printf("timer %s\n", name)
    61  				l.Printf("  count:       %9d\n", t.Count())
    62  				l.Printf("  min:         %12.2f%s\n", float64(t.Min())/du, duSuffix)
    63  				l.Printf("  max:         %12.2f%s\n", float64(t.Max())/du, duSuffix)
    64  				l.Printf("  mean:        %12.2f%s\n", t.Mean()/du, duSuffix)
    65  				l.Printf("  stddev:      %12.2f%s\n", t.StdDev()/du, duSuffix)
    66  				l.Printf("  median:      %12.2f%s\n", ps[0]/du, duSuffix)
    67  				l.Printf("  75%%:         %12.2f%s\n", ps[1]/du, duSuffix)
    68  				l.Printf("  95%%:         %12.2f%s\n", ps[2]/du, duSuffix)
    69  				l.Printf("  99%%:         %12.2f%s\n", ps[3]/du, duSuffix)
    70  				l.Printf("  99.9%%:       %12.2f%s\n", ps[4]/du, duSuffix)
    71  				l.Printf("  1-min rate:  %12.2f\n", t.Rate1())
    72  				l.Printf("  5-min rate:  %12.2f\n", t.Rate5())
    73  				l.Printf("  15-min rate: %12.2f\n", t.Rate15())
    74  				l.Printf("  mean rate:   %12.2f\n", t.RateMean())
    75  			}
    76  		})
    77  	}
    78  }