github.com/theQRL/go-zond@v0.1.1/metrics/histogram.go (about)

     1  package metrics
     2  
     3  type HistogramSnapshot interface {
     4  	SampleSnapshot
     5  }
     6  
     7  // Histograms calculate distribution statistics from a series of int64 values.
     8  type Histogram interface {
     9  	Clear()
    10  	Update(int64)
    11  	Snapshot() HistogramSnapshot
    12  }
    13  
    14  // GetOrRegisterHistogram returns an existing Histogram or constructs and
    15  // registers a new StandardHistogram.
    16  func GetOrRegisterHistogram(name string, r Registry, s Sample) Histogram {
    17  	if nil == r {
    18  		r = DefaultRegistry
    19  	}
    20  	return r.GetOrRegister(name, func() Histogram { return NewHistogram(s) }).(Histogram)
    21  }
    22  
    23  // GetOrRegisterHistogramLazy returns an existing Histogram or constructs and
    24  // registers a new StandardHistogram.
    25  func GetOrRegisterHistogramLazy(name string, r Registry, s func() Sample) Histogram {
    26  	if nil == r {
    27  		r = DefaultRegistry
    28  	}
    29  	return r.GetOrRegister(name, func() Histogram { return NewHistogram(s()) }).(Histogram)
    30  }
    31  
    32  // NewHistogram constructs a new StandardHistogram from a Sample.
    33  func NewHistogram(s Sample) Histogram {
    34  	if !Enabled {
    35  		return NilHistogram{}
    36  	}
    37  	return &StandardHistogram{sample: s}
    38  }
    39  
    40  // NewRegisteredHistogram constructs and registers a new StandardHistogram from
    41  // a Sample.
    42  func NewRegisteredHistogram(name string, r Registry, s Sample) Histogram {
    43  	c := NewHistogram(s)
    44  	if nil == r {
    45  		r = DefaultRegistry
    46  	}
    47  	r.Register(name, c)
    48  	return c
    49  }
    50  
    51  // NilHistogram is a no-op Histogram.
    52  type NilHistogram struct{}
    53  
    54  func (NilHistogram) Clear()                      {}
    55  func (NilHistogram) Snapshot() HistogramSnapshot { return (*emptySnapshot)(nil) }
    56  func (NilHistogram) Update(v int64)              {}
    57  
    58  // StandardHistogram is the standard implementation of a Histogram and uses a
    59  // Sample to bound its memory use.
    60  type StandardHistogram struct {
    61  	sample Sample
    62  }
    63  
    64  // Clear clears the histogram and its sample.
    65  func (h *StandardHistogram) Clear() { h.sample.Clear() }
    66  
    67  // Snapshot returns a read-only copy of the histogram.
    68  func (h *StandardHistogram) Snapshot() HistogramSnapshot {
    69  	return h.sample.Snapshot()
    70  }
    71  
    72  // Update samples a new value.
    73  func (h *StandardHistogram) Update(v int64) { h.sample.Update(v) }