github.com/amazechain/amc@v0.1.3/internal/metrics/log.go (about)

     1  // Copyright 2022 The AmazeChain Authors
     2  // This file is part of the AmazeChain library.
     3  //
     4  // The AmazeChain library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The AmazeChain library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package metrics
    18  
    19  import (
    20  	"github.com/c2h5oh/datasize"
    21  	"github.com/rcrowley/go-metrics"
    22  	"time"
    23  )
    24  
    25  type Logger interface {
    26  	Printf(format string, v ...interface{})
    27  }
    28  
    29  func Log(r metrics.Registry, freq time.Duration, l Logger) {
    30  	LogScaled(r, freq, time.Nanosecond, l)
    31  }
    32  
    33  // Output each metric in the given registry periodically using the given
    34  // logger. Print timings in `scale` units (eg time.Millisecond) rather than nanos.
    35  func LogScaled(r metrics.Registry, freq time.Duration, scale time.Duration, l Logger) {
    36  	du := float64(scale)
    37  	duSuffix := scale.String()[1:]
    38  
    39  	for range time.Tick(freq) {
    40  		r.Each(func(name string, i interface{}) {
    41  			switch metric := i.(type) {
    42  			case metrics.Counter:
    43  				l.Printf("counter %s\n", name)
    44  				l.Printf("  count:       %9d\n", metric.Count())
    45  			case metrics.Gauge:
    46  				l.Printf("gauge %s\n", name)
    47  				l.Printf("  value:       %9d\n", metric.Value())
    48  			case metrics.GaugeFloat64:
    49  				l.Printf("gauge %s\n", name)
    50  				l.Printf("  value:       %f\n", metric.Value())
    51  			case metrics.Healthcheck:
    52  				metric.Check()
    53  				l.Printf("healthcheck %s\n", name)
    54  				l.Printf("  error:       %v\n", metric.Error())
    55  			case metrics.Histogram:
    56  				h := metric.Snapshot()
    57  				ps := h.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
    58  				l.Printf("histogram %s\n", name)
    59  				l.Printf("  count:       %9d\n", h.Count())
    60  				l.Printf("  min:         %9d\n", h.Min())
    61  				l.Printf("  max:         %9d\n", h.Max())
    62  				l.Printf("  mean:        %12.2f\n", h.Mean())
    63  				l.Printf("  stddev:      %12.2f\n", h.StdDev())
    64  				l.Printf("  median:      %12.2f\n", ps[0])
    65  				l.Printf("  75%%:         %12.2f\n", ps[1])
    66  				l.Printf("  95%%:         %12.2f\n", ps[2])
    67  				l.Printf("  99%%:         %12.2f\n", ps[3])
    68  				l.Printf("  99.9%%:       %12.2f\n", ps[4])
    69  			case metrics.Meter:
    70  				m := metric.Snapshot()
    71  				l.Printf("meter %s", name)
    72  				l.Printf("  count:       %.2fMB", datasize.ByteSize(m.Count()).MBytes())
    73  				l.Printf("  1-min rat:  %.2fKB/s", datasize.ByteSize(m.Rate1()).KBytes())
    74  				l.Printf("  5-min rate:  %.2fKB/s", datasize.ByteSize(m.Rate5()).KBytes())
    75  				l.Printf("  15-min rate: %.2fKB/s", datasize.ByteSize(m.Rate15()).KBytes())
    76  				l.Printf("  mean rate:   %.2fKB/s", datasize.ByteSize(m.RateMean()).KBytes())
    77  			case metrics.Timer:
    78  				t := metric.Snapshot()
    79  				ps := t.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
    80  				l.Printf("timer %s\n", name)
    81  				l.Printf("  count:       %9d\n", t.Count())
    82  				l.Printf("  min:         %12.2f%s\n", float64(t.Min())/du, duSuffix)
    83  				l.Printf("  max:         %12.2f%s\n", float64(t.Max())/du, duSuffix)
    84  				l.Printf("  mean:        %12.2f%s\n", t.Mean()/du, duSuffix)
    85  				l.Printf("  stddev:      %12.2f%s\n", t.StdDev()/du, duSuffix)
    86  				l.Printf("  median:      %12.2f%s\n", ps[0]/du, duSuffix)
    87  				l.Printf("  75%%:         %12.2f%s\n", ps[1]/du, duSuffix)
    88  				l.Printf("  95%%:         %12.2f%s\n", ps[2]/du, duSuffix)
    89  				l.Printf("  99%%:         %12.2f%s\n", ps[3]/du, duSuffix)
    90  				l.Printf("  99.9%%:       %12.2f%s\n", ps[4]/du, duSuffix)
    91  				l.Printf("  1-min rate:  %12.2f\n", t.Rate1())
    92  				l.Printf("  5-min rate:  %12.2f\n", t.Rate5())
    93  				l.Printf("  15-min rate: %12.2f\n", t.Rate15())
    94  				l.Printf("  mean rate:   %12.2f\n", t.RateMean())
    95  			}
    96  		})
    97  	}
    98  }