github.com/ethereum/go-ethereum@v1.14.3/metrics/README.md (about)

     1  go-metrics
     2  ==========
     3  
     4  ![travis build status](https://travis-ci.org/rcrowley/go-metrics.svg?branch=master)
     5  
     6  Go port of Coda Hale's Metrics library: <https://github.com/dropwizard/metrics>.
     7  
     8  Documentation: <https://godoc.org/github.com/rcrowley/go-metrics>.
     9  
    10  Usage
    11  -----
    12  
    13  Create and update metrics:
    14  
    15  ```go
    16  c := metrics.NewCounter()
    17  metrics.Register("foo", c)
    18  c.Inc(47)
    19  
    20  g := metrics.NewGauge()
    21  metrics.Register("bar", g)
    22  g.Update(47)
    23  
    24  r := NewRegistry()
    25  g := metrics.NewRegisteredFunctionalGauge("cache-evictions", r, func() int64 { return cache.getEvictionsCount() })
    26  
    27  s := metrics.NewExpDecaySample(1028, 0.015) // or metrics.NewUniformSample(1028)
    28  h := metrics.NewHistogram(s)
    29  metrics.Register("baz", h)
    30  h.Update(47)
    31  
    32  m := metrics.NewMeter()
    33  metrics.Register("quux", m)
    34  m.Mark(47)
    35  
    36  t := metrics.NewTimer()
    37  metrics.Register("bang", t)
    38  t.Time(func() {})
    39  t.Update(47)
    40  ```
    41  
    42  Register() is not threadsafe. For threadsafe metric registration use
    43  GetOrRegister:
    44  
    45  ```go
    46  t := metrics.GetOrRegisterTimer("account.create.latency", nil)
    47  t.Time(func() {})
    48  t.Update(47)
    49  ```
    50  
    51  **NOTE:** Be sure to unregister short-lived meters and timers otherwise they will
    52  leak memory:
    53  
    54  ```go
    55  // Will call Stop() on the Meter to allow for garbage collection
    56  metrics.Unregister("quux")
    57  // Or similarly for a Timer that embeds a Meter
    58  metrics.Unregister("bang")
    59  ```
    60  
    61  Periodically log every metric in human-readable form to standard error:
    62  
    63  ```go
    64  go metrics.Log(metrics.DefaultRegistry, 5 * time.Second, log.New(os.Stderr, "metrics: ", log.Lmicroseconds))
    65  ```
    66  
    67  Periodically log every metric in slightly-more-parseable form to syslog:
    68  
    69  ```go
    70  w, _ := syslog.Dial("unixgram", "/dev/log", syslog.LOG_INFO, "metrics")
    71  go metrics.Syslog(metrics.DefaultRegistry, 60e9, w)
    72  ```
    73  
    74  Periodically emit every metric to Graphite using the [Graphite client](https://github.com/cyberdelia/go-metrics-graphite):
    75  
    76  ```go
    77  
    78  import "github.com/cyberdelia/go-metrics-graphite"
    79  
    80  addr, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:2003")
    81  go graphite.Graphite(metrics.DefaultRegistry, 10e9, "metrics", addr)
    82  ```
    83  
    84  Periodically emit every metric into InfluxDB:
    85  
    86  **NOTE:** this has been pulled out of the library due to constant fluctuations
    87  in the InfluxDB API. In fact, all client libraries are on their way out. see
    88  issues [#121](https://github.com/rcrowley/go-metrics/issues/121) and
    89  [#124](https://github.com/rcrowley/go-metrics/issues/124) for progress and details.
    90  
    91  ```go
    92  import "github.com/vrischmann/go-metrics-influxdb"
    93  
    94  go influxdb.InfluxDB(metrics.DefaultRegistry,
    95    10e9, 
    96    "127.0.0.1:8086", 
    97    "database-name", 
    98    "username", 
    99    "password"
   100  )
   101  ```
   102  
   103  Periodically emit every metric to StatHat:
   104  
   105  ```go
   106  import "github.com/rcrowley/go-metrics/stathat"
   107  
   108  go stathat.Stathat(metrics.DefaultRegistry, 10e9, "example@example.com")
   109  ```
   110  
   111  Maintain all metrics along with expvars at `/debug/metrics`:
   112  
   113  This uses the same mechanism as [the official expvar](https://golang.org/pkg/expvar/)
   114  but exposed under `/debug/metrics`, which shows a json representation of all your usual expvars
   115  as well as all your go-metrics.
   116  
   117  
   118  ```go
   119  import "github.com/rcrowley/go-metrics/exp"
   120  
   121  exp.Exp(metrics.DefaultRegistry)
   122  ```
   123  
   124  Installation
   125  ------------
   126  
   127  ```sh
   128  go get github.com/rcrowley/go-metrics
   129  ```
   130  
   131  StatHat support additionally requires their Go client:
   132  
   133  ```sh
   134  go get github.com/stathat/go
   135  ```
   136  
   137  Publishing Metrics
   138  ------------------
   139  
   140  Clients are available for the following destinations:
   141  
   142  * Graphite - https://github.com/cyberdelia/go-metrics-graphite
   143  * InfluxDB - https://github.com/vrischmann/go-metrics-influxdb
   144  * Ganglia - https://github.com/appscode/metlia
   145  * Prometheus - https://github.com/deathowl/go-metrics-prometheus
   146  * DataDog - https://github.com/syntaqx/go-metrics-datadog
   147  * SignalFX - https://github.com/pascallouisperez/go-metrics-signalfx