3 `package metrics` provides a set of uniform interfaces for service instrumentation.
5 [counters](http://prometheus.io/docs/concepts/metric_types/#counter),
6 [gauges](http://prometheus.io/docs/concepts/metric_types/#gauge), and
7 [histograms](http://prometheus.io/docs/concepts/metric_types/#histogram),
8 and provides adapters to popular metrics packages, like
9 [expvar](https://golang.org/pkg/expvar),
10 [StatsD](https://github.com/etsy/statsd), and
11 [Prometheus](https://prometheus.io).
15 Code instrumentation is absolutely essential to achieve
16 [observability](https://speakerdeck.com/mattheath/observability-in-micro-service-architectures)
17 into a distributed system.
18 Metrics and instrumentation tools have coalesced around a few well-defined idioms.
19 `package metrics` provides a common, minimal interface those idioms for service authors.
23 A simple counter, exported via expvar.
27 "github.com/go-kit/kit/metrics"
28 "github.com/go-kit/kit/metrics/expvar"
32 var myCount metrics.Counter
33 myCount = expvar.NewCounter("my_count")
38 A histogram for request duration,
39 exported via a Prometheus summary with dynamically-computed quantiles.
45 stdprometheus "github.com/prometheus/client_golang/prometheus"
47 "github.com/go-kit/kit/metrics"
48 "github.com/go-kit/kit/metrics/prometheus"
52 var dur metrics.Histogram = prometheus.NewSummaryFrom(stdprometheus.SummaryOpts{
53 Namespace: "myservice",
55 Name: "request_duration_seconds",
56 Help: "Total time spent serving requests.",
61 func handleRequest(dur metrics.Histogram) {
62 defer func(begin time.Time) { dur.Observe(time.Since(begin).Seconds()) }(time.Now())
67 A gauge for the number of goroutines currently running, exported via StatsD.
76 "github.com/go-kit/kit/metrics"
77 "github.com/go-kit/kit/metrics/statsd"
81 statsd := statsd.New("foo_svc.", log.NewNopLogger())
82 report := time.NewTicker(5 * time.Second)
84 go statsd.SendLoop(report.C, "tcp", "statsd.internal:8125")
85 goroutines := statsd.NewGauge("goroutine_count")
86 go exportGoroutines(goroutines)
90 func exportGoroutines(g metrics.Gauge) {
91 for range time.Tick(time.Second) {
92 g.Set(float64(runtime.NumGoroutine()))
97 For more information, see [the package documentation](https://godoc.org/github.com/go-kit/kit/metrics).