rcrowley / go-metrics

Go port of Coda Hale's Metrics library
Other
3.43k stars 493 forks source link

Document how to create a threadsafe registry #186

Closed kevinburke closed 7 years ago

kevinburke commented 7 years ago

My first iteration of a metrics client had code like this:

// Increment a counter with the given name.
func Increment(name string) {
    mn := getWithNamespace(name)
    m := metrics.GetOrRegister(mn, metrics.NewMeter()).(metrics.Meter)
    m.Mark(1)
}

Which contained a massive resource leak - every anonymous NewMeter() call appends a new arbiter, so I had one arbiter for each function call! Instead document the right way to do this so people hopefully don't get as lost.

Initially opened as #165, but I no longer have permission to push to the origin used for that PR. The only change is I fixed the conflict (two branches added tests at the bottom of the same file)