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