123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- package metrics
- import (
- "fmt"
- "reflect"
- "strings"
- "sync"
- )
- // DuplicateMetric is the error returned by Registry.Register when a metric
- // already exists. If you mean to Register that metric you must first
- // Unregister the existing metric.
- type DuplicateMetric string
- func (err DuplicateMetric) Error() string {
- return fmt.Sprintf("duplicate metric: %s", string(err))
- }
- // A Registry holds references to a set of metrics by name and can iterate
- // over them, calling callback functions provided by the user.
- //
- // This is an interface so as to encourage other structs to implement
- // the Registry API as appropriate.
- type Registry interface {
- // Call the given function for each registered metric.
- Each(func(string, interface{}))
- // Get the metric by the given name or nil if none is registered.
- Get(string) interface{}
- // GetAll metrics in the Registry.
- GetAll() map[string]map[string]interface{}
- // Gets an existing metric or registers the given one.
- // The interface can be the metric to register if not found in registry,
- // or a function returning the metric for lazy instantiation.
- GetOrRegister(string, interface{}) interface{}
- // Register the given metric under the given name.
- Register(string, interface{}) error
- // Run all registered healthchecks.
- RunHealthchecks()
- // Unregister the metric with the given name.
- Unregister(string)
- // Unregister all metrics. (Mostly for testing.)
- UnregisterAll()
- }
- // The standard implementation of a Registry is a mutex-protected map
- // of names to metrics.
- type StandardRegistry struct {
- metrics map[string]interface{}
- mutex sync.RWMutex
- }
- // Create a new registry.
- func NewRegistry() Registry {
- return &StandardRegistry{metrics: make(map[string]interface{})}
- }
- // Call the given function for each registered metric.
- func (r *StandardRegistry) Each(f func(string, interface{})) {
- metrics := r.registered()
- for i := range metrics {
- kv := &metrics[i]
- f(kv.name, kv.value)
- }
- }
- // Get the metric by the given name or nil if none is registered.
- func (r *StandardRegistry) Get(name string) interface{} {
- r.mutex.RLock()
- defer r.mutex.RUnlock()
- return r.metrics[name]
- }
- // Gets an existing metric or creates and registers a new one. Threadsafe
- // alternative to calling Get and Register on failure.
- // The interface can be the metric to register if not found in registry,
- // or a function returning the metric for lazy instantiation.
- func (r *StandardRegistry) GetOrRegister(name string, i interface{}) interface{} {
- // access the read lock first which should be re-entrant
- r.mutex.RLock()
- metric, ok := r.metrics[name]
- r.mutex.RUnlock()
- if ok {
- return metric
- }
- // only take the write lock if we'll be modifying the metrics map
- r.mutex.Lock()
- defer r.mutex.Unlock()
- if metric, ok := r.metrics[name]; ok {
- return metric
- }
- if v := reflect.ValueOf(i); v.Kind() == reflect.Func {
- i = v.Call(nil)[0].Interface()
- }
- r.register(name, i)
- return i
- }
- // Register the given metric under the given name. Returns a DuplicateMetric
- // if a metric by the given name is already registered.
- func (r *StandardRegistry) Register(name string, i interface{}) error {
- r.mutex.Lock()
- defer r.mutex.Unlock()
- return r.register(name, i)
- }
- // Run all registered healthchecks.
- func (r *StandardRegistry) RunHealthchecks() {
- r.mutex.RLock()
- defer r.mutex.RUnlock()
- for _, i := range r.metrics {
- if h, ok := i.(Healthcheck); ok {
- h.Check()
- }
- }
- }
- // GetAll metrics in the Registry
- func (r *StandardRegistry) GetAll() map[string]map[string]interface{} {
- data := make(map[string]map[string]interface{})
- r.Each(func(name string, i interface{}) {
- values := make(map[string]interface{})
- switch metric := i.(type) {
- case Counter:
- values["count"] = metric.Count()
- case Gauge:
- values["value"] = metric.Value()
- case GaugeFloat64:
- values["value"] = metric.Value()
- case Healthcheck:
- values["error"] = nil
- metric.Check()
- if err := metric.Error(); nil != err {
- values["error"] = metric.Error().Error()
- }
- case Histogram:
- h := metric.Snapshot()
- ps := h.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
- values["count"] = h.Count()
- values["min"] = h.Min()
- values["max"] = h.Max()
- values["mean"] = h.Mean()
- values["stddev"] = h.StdDev()
- values["median"] = ps[0]
- values["75%"] = ps[1]
- values["95%"] = ps[2]
- values["99%"] = ps[3]
- values["99.9%"] = ps[4]
- case Meter:
- m := metric.Snapshot()
- values["count"] = m.Count()
- values["1m.rate"] = m.Rate1()
- values["5m.rate"] = m.Rate5()
- values["15m.rate"] = m.Rate15()
- values["mean.rate"] = m.RateMean()
- case Timer:
- t := metric.Snapshot()
- ps := t.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
- values["count"] = t.Count()
- values["min"] = t.Min()
- values["max"] = t.Max()
- values["mean"] = t.Mean()
- values["stddev"] = t.StdDev()
- values["median"] = ps[0]
- values["75%"] = ps[1]
- values["95%"] = ps[2]
- values["99%"] = ps[3]
- values["99.9%"] = ps[4]
- values["1m.rate"] = t.Rate1()
- values["5m.rate"] = t.Rate5()
- values["15m.rate"] = t.Rate15()
- values["mean.rate"] = t.RateMean()
- }
- data[name] = values
- })
- return data
- }
- // Unregister the metric with the given name.
- func (r *StandardRegistry) Unregister(name string) {
- r.mutex.Lock()
- defer r.mutex.Unlock()
- r.stop(name)
- delete(r.metrics, name)
- }
- // Unregister all metrics. (Mostly for testing.)
- func (r *StandardRegistry) UnregisterAll() {
- r.mutex.Lock()
- defer r.mutex.Unlock()
- for name, _ := range r.metrics {
- r.stop(name)
- delete(r.metrics, name)
- }
- }
- func (r *StandardRegistry) register(name string, i interface{}) error {
- if _, ok := r.metrics[name]; ok {
- return DuplicateMetric(name)
- }
- switch i.(type) {
- case Counter, Gauge, GaugeFloat64, Healthcheck, Histogram, Meter, Timer:
- r.metrics[name] = i
- }
- return nil
- }
- type metricKV struct {
- name string
- value interface{}
- }
- func (r *StandardRegistry) registered() []metricKV {
- r.mutex.RLock()
- defer r.mutex.RUnlock()
- metrics := make([]metricKV, 0, len(r.metrics))
- for name, i := range r.metrics {
- metrics = append(metrics, metricKV{
- name: name,
- value: i,
- })
- }
- return metrics
- }
- func (r *StandardRegistry) stop(name string) {
- if i, ok := r.metrics[name]; ok {
- if s, ok := i.(Stoppable); ok {
- s.Stop()
- }
- }
- }
- // Stoppable defines the metrics which has to be stopped.
- type Stoppable interface {
- Stop()
- }
- type PrefixedRegistry struct {
- underlying Registry
- prefix string
- }
- func NewPrefixedRegistry(prefix string) Registry {
- return &PrefixedRegistry{
- underlying: NewRegistry(),
- prefix: prefix,
- }
- }
- func NewPrefixedChildRegistry(parent Registry, prefix string) Registry {
- return &PrefixedRegistry{
- underlying: parent,
- prefix: prefix,
- }
- }
- // Call the given function for each registered metric.
- func (r *PrefixedRegistry) Each(fn func(string, interface{})) {
- wrappedFn := func(prefix string) func(string, interface{}) {
- return func(name string, iface interface{}) {
- if strings.HasPrefix(name, prefix) {
- fn(name, iface)
- } else {
- return
- }
- }
- }
- baseRegistry, prefix := findPrefix(r, "")
- baseRegistry.Each(wrappedFn(prefix))
- }
- func findPrefix(registry Registry, prefix string) (Registry, string) {
- switch r := registry.(type) {
- case *PrefixedRegistry:
- return findPrefix(r.underlying, r.prefix+prefix)
- case *StandardRegistry:
- return r, prefix
- }
- return nil, ""
- }
- // Get the metric by the given name or nil if none is registered.
- func (r *PrefixedRegistry) Get(name string) interface{} {
- realName := r.prefix + name
- return r.underlying.Get(realName)
- }
- // Gets an existing metric or registers the given one.
- // The interface can be the metric to register if not found in registry,
- // or a function returning the metric for lazy instantiation.
- func (r *PrefixedRegistry) GetOrRegister(name string, metric interface{}) interface{} {
- realName := r.prefix + name
- return r.underlying.GetOrRegister(realName, metric)
- }
- // Register the given metric under the given name. The name will be prefixed.
- func (r *PrefixedRegistry) Register(name string, metric interface{}) error {
- realName := r.prefix + name
- return r.underlying.Register(realName, metric)
- }
- // Run all registered healthchecks.
- func (r *PrefixedRegistry) RunHealthchecks() {
- r.underlying.RunHealthchecks()
- }
- // GetAll metrics in the Registry
- func (r *PrefixedRegistry) GetAll() map[string]map[string]interface{} {
- return r.underlying.GetAll()
- }
- // Unregister the metric with the given name. The name will be prefixed.
- func (r *PrefixedRegistry) Unregister(name string) {
- realName := r.prefix + name
- r.underlying.Unregister(realName)
- }
- // Unregister all metrics. (Mostly for testing.)
- func (r *PrefixedRegistry) UnregisterAll() {
- r.underlying.UnregisterAll()
- }
- var DefaultRegistry Registry = NewRegistry()
- // Call the given function for each registered metric.
- func Each(f func(string, interface{})) {
- DefaultRegistry.Each(f)
- }
- // Get the metric by the given name or nil if none is registered.
- func Get(name string) interface{} {
- return DefaultRegistry.Get(name)
- }
- // Gets an existing metric or creates and registers a new one. Threadsafe
- // alternative to calling Get and Register on failure.
- func GetOrRegister(name string, i interface{}) interface{} {
- return DefaultRegistry.GetOrRegister(name, i)
- }
- // Register the given metric under the given name. Returns a DuplicateMetric
- // if a metric by the given name is already registered.
- func Register(name string, i interface{}) error {
- return DefaultRegistry.Register(name, i)
- }
- // Register the given metric under the given name. Panics if a metric by the
- // given name is already registered.
- func MustRegister(name string, i interface{}) {
- if err := Register(name, i); err != nil {
- panic(err)
- }
- }
- // Run all registered healthchecks.
- func RunHealthchecks() {
- DefaultRegistry.RunHealthchecks()
- }
- // Unregister the metric with the given name.
- func Unregister(name string) {
- DefaultRegistry.Unregister(name)
- }
|