GetOrRegisterHistogram requires us to track samples separately or risk wasteful allocations
sandipb opened this issue · comments
I usually use counters, gauges and timers in code somewhat like this:
metrics.GetOrRegisterCounter(metricName, metricsRegistry).Inc(amount)
But doing the same thing with Histograms are a problem. This is the call signature.
GetOrRegisterHistogram(name string, r Registry, s Sample) Histogram
If I want to use an UniformSample, am I supposed to use it like this?
metrics.GetOrRegisterHistogram(metricName, metricsRegistry, NewUniformSample(size)).Inc(amount)
But that would create a new sample on every call.
To prevent that, we would need to make sure we create a sample only if a histogram is not present. At present, I am doing this with a mutex protected dict, but this is annoying.
Can we turn this function into accepting a Sample generator callback instead?
type NewSampleFunc func() Sample
// GetOrRegisterHistogramWithSampleFunc(name string,
// r Registry,
// s NewSampleFunc) Histogram
metrics.GetOrRegisterHistogramWithSampleFunc(metricName,
metricsRegistry,
func(){return NewUniformSample(size)}).Inc(amount)
+1 on this change. It's otherwise awkward to efficiently use lazy sample instantiations.
+1, though I don't think this will ever land :)