...

Source file src/github.com/go-kit/kit/metrics/expvar/expvar.go

Documentation: github.com/go-kit/kit/metrics/expvar

     1  // Package expvar provides expvar backends for metrics.
     2  // Label values are not supported.
     3  package expvar
     4  
     5  import (
     6  	"expvar"
     7  	"sync"
     8  
     9  	"github.com/go-kit/kit/metrics"
    10  	"github.com/go-kit/kit/metrics/generic"
    11  )
    12  
    13  // Counter implements the counter metric with an expvar float.
    14  // Label values are not supported.
    15  type Counter struct {
    16  	f *expvar.Float
    17  }
    18  
    19  // NewCounter creates an expvar Float with the given name, and returns an object
    20  // that implements the Counter interface.
    21  func NewCounter(name string) *Counter {
    22  	return &Counter{
    23  		f: expvar.NewFloat(name),
    24  	}
    25  }
    26  
    27  // With is a no-op.
    28  func (c *Counter) With(labelValues ...string) metrics.Counter { return c }
    29  
    30  // Add implements Counter.
    31  func (c *Counter) Add(delta float64) { c.f.Add(delta) }
    32  
    33  // Gauge implements the gauge metric with an expvar float.
    34  // Label values are not supported.
    35  type Gauge struct {
    36  	f *expvar.Float
    37  }
    38  
    39  // NewGauge creates an expvar Float with the given name, and returns an object
    40  // that implements the Gauge interface.
    41  func NewGauge(name string) *Gauge {
    42  	return &Gauge{
    43  		f: expvar.NewFloat(name),
    44  	}
    45  }
    46  
    47  // With is a no-op.
    48  func (g *Gauge) With(labelValues ...string) metrics.Gauge { return g }
    49  
    50  // Set implements Gauge.
    51  func (g *Gauge) Set(value float64) { g.f.Set(value) }
    52  
    53  // Add implements metrics.Gauge.
    54  func (g *Gauge) Add(delta float64) { g.f.Add(delta) }
    55  
    56  // Histogram implements the histogram metric with a combination of the generic
    57  // Histogram object and several expvar Floats, one for each of the 50th, 90th,
    58  // 95th, and 99th quantiles of observed values, with the quantile attached to
    59  // the name as a suffix. Label values are not supported.
    60  type Histogram struct {
    61  	mtx sync.Mutex
    62  	h   *generic.Histogram
    63  	p50 *expvar.Float
    64  	p90 *expvar.Float
    65  	p95 *expvar.Float
    66  	p99 *expvar.Float
    67  }
    68  
    69  // NewHistogram returns a Histogram object with the given name and number of
    70  // buckets in the underlying histogram object. 50 is a good default number of
    71  // buckets.
    72  func NewHistogram(name string, buckets int) *Histogram {
    73  	return &Histogram{
    74  		h:   generic.NewHistogram(name, buckets),
    75  		p50: expvar.NewFloat(name + ".p50"),
    76  		p90: expvar.NewFloat(name + ".p90"),
    77  		p95: expvar.NewFloat(name + ".p95"),
    78  		p99: expvar.NewFloat(name + ".p99"),
    79  	}
    80  }
    81  
    82  // With is a no-op.
    83  func (h *Histogram) With(labelValues ...string) metrics.Histogram { return h }
    84  
    85  // Observe implements Histogram.
    86  func (h *Histogram) Observe(value float64) {
    87  	h.mtx.Lock()
    88  	defer h.mtx.Unlock()
    89  	h.h.Observe(value)
    90  	h.p50.Set(h.h.Quantile(0.50))
    91  	h.p90.Set(h.h.Quantile(0.90))
    92  	h.p95.Set(h.h.Quantile(0.95))
    93  	h.p99.Set(h.h.Quantile(0.99))
    94  }
    95  

View as plain text