9045b79bc2
This PR modifies how the metrics library handles `Enabled`: previously, the package `init` decided whether to serve real metrics or just dummy-types. This has several drawbacks: - During pkg init, we need to determine whether metrics are enabled or not. So we first hacked in a check if certain geth-specific commandline-flags were enabled. Then we added a similar check for geth-env-vars. Then we almost added a very elaborate check for toml-config-file, plus toml parsing. - Using "real" types and dummy types interchangeably means that everything is hidden behind interfaces. This has a performance penalty, and also it just adds a lot of code. This PR removes the interface stuff, uses concrete types, and allows for the setting of Enabled to happen later. It is still assumed that `metrics.Enable()` is invoked early on. The somewhat 'heavy' operations, such as ticking meters and exp-decay, now checks the enable-flag to prevent resource leak. The change may be large, but it's mostly pretty trivial, and from the last time I gutted the metrics, I ensured that we have fairly good test coverage. --------- Co-authored-by: Felix Lange <fjl@twurst.com>
73 lines
1.9 KiB
Go
73 lines
1.9 KiB
Go
package metrics
|
|
|
|
type HistogramSnapshot interface {
|
|
Count() int64
|
|
Max() int64
|
|
Mean() float64
|
|
Min() int64
|
|
Percentile(float64) float64
|
|
Percentiles([]float64) []float64
|
|
Size() int
|
|
StdDev() float64
|
|
Sum() int64
|
|
Variance() float64
|
|
}
|
|
|
|
// Histogram calculates distribution statistics from a series of int64 values.
|
|
type Histogram interface {
|
|
Clear()
|
|
Update(int64)
|
|
Snapshot() HistogramSnapshot
|
|
}
|
|
|
|
// GetOrRegisterHistogram returns an existing Histogram or constructs and
|
|
// registers a new StandardHistogram.
|
|
func GetOrRegisterHistogram(name string, r Registry, s Sample) Histogram {
|
|
if nil == r {
|
|
r = DefaultRegistry
|
|
}
|
|
return r.GetOrRegister(name, func() Histogram { return NewHistogram(s) }).(Histogram)
|
|
}
|
|
|
|
// GetOrRegisterHistogramLazy returns an existing Histogram or constructs and
|
|
// registers a new StandardHistogram.
|
|
func GetOrRegisterHistogramLazy(name string, r Registry, s func() Sample) Histogram {
|
|
if nil == r {
|
|
r = DefaultRegistry
|
|
}
|
|
return r.GetOrRegister(name, func() Histogram { return NewHistogram(s()) }).(Histogram)
|
|
}
|
|
|
|
// NewHistogram constructs a new StandardHistogram from a Sample.
|
|
func NewHistogram(s Sample) Histogram {
|
|
return &StandardHistogram{s}
|
|
}
|
|
|
|
// NewRegisteredHistogram constructs and registers a new StandardHistogram from
|
|
// a Sample.
|
|
func NewRegisteredHistogram(name string, r Registry, s Sample) Histogram {
|
|
c := NewHistogram(s)
|
|
if nil == r {
|
|
r = DefaultRegistry
|
|
}
|
|
r.Register(name, c)
|
|
return c
|
|
}
|
|
|
|
// StandardHistogram is the standard implementation of a Histogram and uses a
|
|
// Sample to bound its memory use.
|
|
type StandardHistogram struct {
|
|
sample Sample
|
|
}
|
|
|
|
// Clear clears the histogram and its sample.
|
|
func (h *StandardHistogram) Clear() { h.sample.Clear() }
|
|
|
|
// Snapshot returns a read-only copy of the histogram.
|
|
func (h *StandardHistogram) Snapshot() HistogramSnapshot {
|
|
return h.sample.Snapshot()
|
|
}
|
|
|
|
// Update samples a new value.
|
|
func (h *StandardHistogram) Update(v int64) { h.sample.Update(v) }
|