github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/metrics/syslog.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  // +build !windows
    19  
    20  package metrics
    21  
    22  import (
    23  	"fmt"
    24  	"log/syslog"
    25  	"time"
    26  )
    27  
    28  // Output each metric in the given registry to syslog periodically using
    29  // the given syslogger.
    30  func Syslog(r Registry, d time.Duration, w *syslog.Writer) {
    31  	for range time.Tick(d) {
    32  		r.Each(func(name string, i interface{}) {
    33  			switch metric := i.(type) {
    34  			case Counter:
    35  				w.Info(fmt.Sprintf("counter %s: count: %d", name, metric.Count()))
    36  			case Gauge:
    37  				w.Info(fmt.Sprintf("gauge %s: value: %d", name, metric.Value()))
    38  			case GaugeFloat64:
    39  				w.Info(fmt.Sprintf("gauge %s: value: %f", name, metric.Value()))
    40  			case Healthcheck:
    41  				metric.Check()
    42  				w.Info(fmt.Sprintf("healthcheck %s: error: %v", name, metric.Error()))
    43  			case Histogram:
    44  				h := metric.Snapshot()
    45  				ps := h.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
    46  				w.Info(fmt.Sprintf(
    47  					"histogram %s: count: %d min: %d max: %d mean: %.2f stddev: %.2f median: %.2f 75%%: %.2f 95%%: %.2f 99%%: %.2f 99.9%%: %.2f",
    48  					name,
    49  					h.Count(),
    50  					h.Min(),
    51  					h.Max(),
    52  					h.Mean(),
    53  					h.StdDev(),
    54  					ps[0],
    55  					ps[1],
    56  					ps[2],
    57  					ps[3],
    58  					ps[4],
    59  				))
    60  			case Meter:
    61  				m := metric.Snapshot()
    62  				w.Info(fmt.Sprintf(
    63  					"meter %s: count: %d 1-min: %.2f 5-min: %.2f 15-min: %.2f mean: %.2f",
    64  					name,
    65  					m.Count(),
    66  					m.Rate1(),
    67  					m.Rate5(),
    68  					m.Rate15(),
    69  					m.RateMean(),
    70  				))
    71  			case Timer:
    72  				t := metric.Snapshot()
    73  				ps := t.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
    74  				w.Info(fmt.Sprintf(
    75  					"timer %s: count: %d min: %d max: %d mean: %.2f stddev: %.2f median: %.2f 75%%: %.2f 95%%: %.2f 99%%: %.2f 99.9%%: %.2f 1-min: %.2f 5-min: %.2f 15-min: %.2f mean-rate: %.2f",
    76  					name,
    77  					t.Count(),
    78  					t.Min(),
    79  					t.Max(),
    80  					t.Mean(),
    81  					t.StdDev(),
    82  					ps[0],
    83  					ps[1],
    84  					ps[2],
    85  					ps[3],
    86  					ps[4],
    87  					t.Rate1(),
    88  					t.Rate5(),
    89  					t.Rate15(),
    90  					t.RateMean(),
    91  				))
    92  			}
    93  		})
    94  	}
    95  }