cmd
initial implementation of GaugeFloat64
exp
PrefixRegistries of PrefixedRegistries
librato
Merge pull request #114 from pressly/master
stathat
initial implementation of GaugeFloat64
.gitignore
never-read debugging tool.
.travis.yml
add go 1.15
counter_test.go
Fully introduce snapshots for all metric types.
counter.go
Allow lazy instantiation of metric in GetOrRegister.
debug_test.go
Fixed #252 - fixed test to work with the actual registry metric...
debug.go
Fixed #252 - guard against multiple registrations of runtime and debug stats - gofmt code...
ewma_test.go
add race test for EWMA
ewma.go
Optimizes ewma to reduce lock contention
gauge_float64_test.go
Add parallel bench test for gauge_float64
gauge_float64.go
Update gauge_float64.go to reduce lock contention
gauge_test.go
add race test for Gauge
gauge.go
gofmt the code
graphite_test.go
gofmt pass
graphite.go
deprecate the graphite client in favor of the new separate lib
healthcheck.go
Mostly documentation and naming changes.
histogram_test.go
Fix tests broken by sample changes.
histogram.go
Alphabetize
json_test.go
Added JSON->io.Writer; consistent ordering to WriteOnce
json.go
rename "Dump" to "GetAll"
LICENSE
Updated docs, deleted Makefiles.
log.go
Fixed range syntax for backwards compatability for go 1.2 and 1.3
memory.md
Installation and usage.
meter_test.go
Replace lock with CAS in standard meter stop
meter.go
Replace lock with CAS in standard meter stop
metrics_test.go
Document how to create a threadsafe registry
metrics.go
Added JSON->io.Writer; consistent ordering to WriteOnce
opentsdb_test.go
gofmt pass
opentsdb.go
Fix #74 for OpenTSDB, too
README.md
add archival note
registry_test.go
fix gofmt for go 1.12
registry.go
fix race
runtime_cgo.go
Fixes: Won't compile on AppEngine #67
runtime_gccpufraction.go
Add support for fraction of CPU time used by GC
runtime_no_cgo.go
Fixes: Won't compile on AppEngine #67
runtime_no_gccpufraction.go
Add support for fraction of CPU time used by GC
runtime_test.go
Fixed #252 - fixed test to work with the actual registry metric...
runtime.go
Fixed #252 - guard against multiple registrations of runtime and debug stats - gofmt code...
sample_test.go
Test for #104
sample.go
Merge pull request #97 from Dieterbe/ExpDecaySample-url
syslog.go
Use time.Tick everywhere else we should, too
timer_test.go
add Stop() to Timer
timer.go
Use set of meters and update documentation
validate.sh
more go versions; add race detector to tests
writer_test.go
Merge branch 'master' of github.com:stuartcarnie/go-metrics
writer.go
Use time.Tick everywhere else we should, too

go-metrics

Go port of Coda Hale's Metrics library: https://github.com/dropwizard/metrics.

Documentation: http://godoc.org/github.com/rcrowley/go-metrics.

Archived as of April 1 2025

This repository is no longer maintained. The authors recommend you explore the following newer, more widely adopted libraries for your Go instrumentation needs:

Usage

Create and update metrics:

c := metrics.NewCounter()
metrics.Register("foo", c)
c.Inc(47)

g := metrics.NewGauge()
metrics.Register("bar", g)
g.Update(47)

r := NewRegistry()
g := metrics.NewRegisteredFunctionalGauge("cache-evictions", r, func() int64 { return cache.getEvictionsCount() })

s := metrics.NewExpDecaySample(1028, 0.015) // or metrics.NewUniformSample(1028)
h := metrics.NewHistogram(s)
metrics.Register("baz", h)
h.Update(47)

m := metrics.NewMeter()
metrics.Register("quux", m)
m.Mark(47)

t := metrics.NewTimer()
metrics.Register("bang", t)
t.Time(func() {})
t.Update(47)

Register() is not threadsafe. For threadsafe metric registration use GetOrRegister:

t := metrics.GetOrRegisterTimer("account.create.latency", nil)
t.Time(func() {})
t.Update(47)

NOTE: Be sure to unregister short-lived meters and timers otherwise they will leak memory:

// Will call Stop() on the Meter to allow for garbage collection
metrics.Unregister("quux")
// Or similarly for a Timer that embeds a Meter
metrics.Unregister("bang")

Periodically log every metric in human-readable form to standard error:

go metrics.Log(metrics.DefaultRegistry, 5 * time.Second, log.New(os.Stderr, "metrics: ", log.Lmicroseconds))

Periodically log every metric in slightly-more-parseable form to syslog:

w, _ := syslog.Dial("unixgram", "/dev/log", syslog.LOG_INFO, "metrics")
go metrics.Syslog(metrics.DefaultRegistry, 60e9, w)

Periodically emit every metric to Graphite using the Graphite client:


import "github.com/cyberdelia/go-metrics-graphite"

addr, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:2003")
go graphite.Graphite(metrics.DefaultRegistry, 10e9, "metrics", addr)

Periodically emit every metric into InfluxDB:

NOTE: this has been pulled out of the library due to constant fluctuations in the InfluxDB API. In fact, all client libraries are on their way out. see issues #121 and #124 for progress and details.

import "github.com/vrischmann/go-metrics-influxdb"

go influxdb.InfluxDB(metrics.DefaultRegistry,
  10e9, 
  "127.0.0.1:8086", 
  "database-name", 
  "username", 
  "password"
)

Periodically upload every metric to Librato using the Librato client:

Note: the client included with this repository under the librato package has been deprecated and moved to the repository linked above.

import "github.com/mihasya/go-metrics-librato"

go librato.Librato(metrics.DefaultRegistry,
    10e9,                  // interval
    "example@example.com", // account owner email address
    "token",               // Librato API token
    "hostname",            // source
    []float64{0.95},       // percentiles to send
    time.Millisecond,      // time unit
)

Periodically emit every metric to StatHat:

import "github.com/rcrowley/go-metrics/stathat"

go stathat.Stathat(metrics.DefaultRegistry, 10e9, "example@example.com")

Maintain all metrics along with expvars at /debug/metrics:

This uses the same mechanism as the official expvar but exposed under /debug/metrics, which shows a json representation of all your usual expvars as well as all your go-metrics.

import "github.com/rcrowley/go-metrics/exp"

exp.Exp(metrics.DefaultRegistry)

Installation

go get github.com/rcrowley/go-metrics

StatHat support additionally requires their Go client:

go get github.com/stathat/go

Publishing Metrics

Clients are available for the following destinations: