github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/utilities/metrics/syslog.go (about)

     1  //go:build !windows
     2  // +build !windows
     3  
     4  package metrics
     5  
     6  import (
     7  	"fmt"
     8  	"log/syslog"
     9  	"time"
    10  )
    11  
    12  func Syslog(r Registry, d time.Duration, w *syslog.Writer) {
    13  	for range time.Tick(d) {
    14  		r.Each(func(name string, i interface{}) {
    15  			switch metric := i.(type) {
    16  			case Counter:
    17  				w.Info(fmt.Sprintf("counter %s: count: %d", name, metric.Count()))
    18  			case Gauge:
    19  				w.Info(fmt.Sprintf("gauge %s: value: %d", name, metric.Value()))
    20  			case GaugeFloat64:
    21  				w.Info(fmt.Sprintf("gauge %s: value: %f", name, metric.Value()))
    22  			case Healthcheck:
    23  				metric.Check()
    24  				w.Info(fmt.Sprintf("healthcheck %s: error: %v", name, metric.Error()))
    25  			case Histogram:
    26  				h := metric.Snapshot()
    27  				ps := h.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
    28  				w.Info(fmt.Sprintf(
    29  					"histogram %s: count: %d min: %d max: %d mean: %.2f stddev: %.2f median: %.2f 75%%: %.2f 95%%: %.2f 99%%: %.2f 99.9%%: %.2f",
    30  					name,
    31  					h.Count(),
    32  					h.Min(),
    33  					h.Max(),
    34  					h.Mean(),
    35  					h.StdDev(),
    36  					ps[0],
    37  					ps[1],
    38  					ps[2],
    39  					ps[3],
    40  					ps[4],
    41  				))
    42  			case Meter:
    43  				m := metric.Snapshot()
    44  				w.Info(fmt.Sprintf(
    45  					"meter %s: count: %d 1-min: %.2f 5-min: %.2f 15-min: %.2f mean: %.2f",
    46  					name,
    47  					m.Count(),
    48  					m.Rate1(),
    49  					m.Rate5(),
    50  					m.Rate15(),
    51  					m.RateMean(),
    52  				))
    53  			case Timer:
    54  				t := metric.Snapshot()
    55  				ps := t.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
    56  				w.Info(fmt.Sprintf(
    57  					"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",
    58  					name,
    59  					t.Count(),
    60  					t.Min(),
    61  					t.Max(),
    62  					t.Mean(),
    63  					t.StdDev(),
    64  					ps[0],
    65  					ps[1],
    66  					ps[2],
    67  					ps[3],
    68  					ps[4],
    69  					t.Rate1(),
    70  					t.Rate5(),
    71  					t.Rate15(),
    72  					t.RateMean(),
    73  				))
    74  			}
    75  		})
    76  	}
    77  }