github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/metrics/log.go (about)

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