gitlab.com/aquachain/aquachain@v1.17.16-rc3.0.20221018032414-e3ddf1e1c055/common/metrics/log.go (about)

     1  // Copyright 2018 The aquachain Authors
     2  // This file is part of the aquachain library.
     3  //
     4  // The aquachain 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 aquachain 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 aquachain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package metrics
    18  
    19  import (
    20  	"time"
    21  )
    22  
    23  type Logger interface {
    24  	Printf(format string, v ...interface{})
    25  }
    26  
    27  func Log(r Registry, freq time.Duration, l Logger) {
    28  	LogScaled(r, freq, time.Nanosecond, l)
    29  }
    30  
    31  // Output each metric in the given registry periodically using the given
    32  // logger. Print timings in `scale` units (eg time.Millisecond) rather than nanos.
    33  func LogScaled(r Registry, freq time.Duration, scale time.Duration, l Logger) {
    34  	du := float64(scale)
    35  	duSuffix := scale.String()[1:]
    36  
    37  	for range time.Tick(freq) {
    38  		r.Each(func(name string, i interface{}) {
    39  			switch metric := i.(type) {
    40  			case Counter:
    41  				l.Printf("counter %s\n", name)
    42  				l.Printf("  count:       %9d\n", metric.Count())
    43  			case Gauge:
    44  				l.Printf("gauge %s\n", name)
    45  				l.Printf("  value:       %9d\n", metric.Value())
    46  			case GaugeFloat64:
    47  				l.Printf("gauge %s\n", name)
    48  				l.Printf("  value:       %f\n", metric.Value())
    49  			case Healthcheck:
    50  				metric.Check()
    51  				l.Printf("healthcheck %s\n", name)
    52  				l.Printf("  error:       %v\n", metric.Error())
    53  			case Histogram:
    54  				h := metric.Snapshot()
    55  				ps := h.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
    56  				l.Printf("histogram %s\n", name)
    57  				l.Printf("  count:       %9d\n", h.Count())
    58  				l.Printf("  min:         %9d\n", h.Min())
    59  				l.Printf("  max:         %9d\n", h.Max())
    60  				l.Printf("  mean:        %12.2f\n", h.Mean())
    61  				l.Printf("  stddev:      %12.2f\n", h.StdDev())
    62  				l.Printf("  median:      %12.2f\n", ps[0])
    63  				l.Printf("  75%%:         %12.2f\n", ps[1])
    64  				l.Printf("  95%%:         %12.2f\n", ps[2])
    65  				l.Printf("  99%%:         %12.2f\n", ps[3])
    66  				l.Printf("  99.9%%:       %12.2f\n", ps[4])
    67  			case Meter:
    68  				m := metric.Snapshot()
    69  				l.Printf("meter %s\n", name)
    70  				l.Printf("  count:       %9d\n", m.Count())
    71  				l.Printf("  1-min rate:  %12.2f\n", m.Rate1())
    72  				l.Printf("  5-min rate:  %12.2f\n", m.Rate5())
    73  				l.Printf("  15-min rate: %12.2f\n", m.Rate15())
    74  				l.Printf("  mean rate:   %12.2f\n", m.RateMean())
    75  			case Timer:
    76  				t := metric.Snapshot()
    77  				ps := t.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
    78  				l.Printf("timer %s\n", name)
    79  				l.Printf("  count:       %9d\n", t.Count())
    80  				l.Printf("  min:         %12.2f%s\n", float64(t.Min())/du, duSuffix)
    81  				l.Printf("  max:         %12.2f%s\n", float64(t.Max())/du, duSuffix)
    82  				l.Printf("  mean:        %12.2f%s\n", t.Mean()/du, duSuffix)
    83  				l.Printf("  stddev:      %12.2f%s\n", t.StdDev()/du, duSuffix)
    84  				l.Printf("  median:      %12.2f%s\n", ps[0]/du, duSuffix)
    85  				l.Printf("  75%%:         %12.2f%s\n", ps[1]/du, duSuffix)
    86  				l.Printf("  95%%:         %12.2f%s\n", ps[2]/du, duSuffix)
    87  				l.Printf("  99%%:         %12.2f%s\n", ps[3]/du, duSuffix)
    88  				l.Printf("  99.9%%:       %12.2f%s\n", ps[4]/du, duSuffix)
    89  				l.Printf("  1-min rate:  %12.2f\n", t.Rate1())
    90  				l.Printf("  5-min rate:  %12.2f\n", t.Rate5())
    91  				l.Printf("  15-min rate: %12.2f\n", t.Rate15())
    92  				l.Printf("  mean rate:   %12.2f\n", t.RateMean())
    93  			}
    94  		})
    95  	}
    96  }