github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/metrics/syslog.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //+建设!窗户
    10  
    11  package metrics
    12  
    13  import (
    14  	"fmt"
    15  	"log/syslog"
    16  	"time"
    17  )
    18  
    19  //使用以下命令定期将给定注册表中的每个度量输出到syslog
    20  //给定的系统记录器。
    21  func Syslog(r Registry, d time.Duration, w *syslog.Writer) {
    22  	for range time.Tick(d) {
    23  		r.Each(func(name string, i interface{}) {
    24  			switch metric := i.(type) {
    25  			case Counter:
    26  				w.Info(fmt.Sprintf("counter %s: count: %d", name, metric.Count()))
    27  			case Gauge:
    28  				w.Info(fmt.Sprintf("gauge %s: value: %d", name, metric.Value()))
    29  			case GaugeFloat64:
    30  				w.Info(fmt.Sprintf("gauge %s: value: %f", name, metric.Value()))
    31  			case Healthcheck:
    32  				metric.Check()
    33  				w.Info(fmt.Sprintf("healthcheck %s: error: %v", name, metric.Error()))
    34  			case Histogram:
    35  				h := metric.Snapshot()
    36  				ps := h.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
    37  				w.Info(fmt.Sprintf(
    38  					"histogram %s: count: %d min: %d max: %d mean: %.2f stddev: %.2f median: %.2f 75%%: %.2f 95%%: %.2f 99%%: %.2f 99.9%%: %.2f",
    39  					name,
    40  					h.Count(),
    41  					h.Min(),
    42  					h.Max(),
    43  					h.Mean(),
    44  					h.StdDev(),
    45  					ps[0],
    46  					ps[1],
    47  					ps[2],
    48  					ps[3],
    49  					ps[4],
    50  				))
    51  			case Meter:
    52  				m := metric.Snapshot()
    53  				w.Info(fmt.Sprintf(
    54  					"meter %s: count: %d 1-min: %.2f 5-min: %.2f 15-min: %.2f mean: %.2f",
    55  					name,
    56  					m.Count(),
    57  					m.Rate1(),
    58  					m.Rate5(),
    59  					m.Rate15(),
    60  					m.RateMean(),
    61  				))
    62  			case Timer:
    63  				t := metric.Snapshot()
    64  				ps := t.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
    65  				w.Info(fmt.Sprintf(
    66  					"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",
    67  					name,
    68  					t.Count(),
    69  					t.Min(),
    70  					t.Max(),
    71  					t.Mean(),
    72  					t.StdDev(),
    73  					ps[0],
    74  					ps[1],
    75  					ps[2],
    76  					ps[3],
    77  					ps[4],
    78  					t.Rate1(),
    79  					t.Rate5(),
    80  					t.Rate15(),
    81  					t.RateMean(),
    82  				))
    83  			}
    84  		})
    85  	}
    86  }