1 // Package expvar provides expvar backends for metrics.
2 // Label values are not supported.
9 "github.com/go-kit/kit/metrics"
10 "github.com/go-kit/kit/metrics/generic"
13 // Counter implements the counter metric with an expvar float.
14 // Label values are not supported.
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 {
23 f: expvar.NewFloat(name),
28 func (c *Counter) With(labelValues ...string) metrics.Counter { return c }
30 // Add implements Counter.
31 func (c *Counter) Add(delta float64) { c.f.Add(delta) }
33 // Gauge implements the gauge metric with an expvar float.
34 // Label values are not supported.
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 {
43 f: expvar.NewFloat(name),
48 func (g *Gauge) With(labelValues ...string) metrics.Gauge { return g }
50 // Set implements Gauge.
51 func (g *Gauge) Set(value float64) { g.f.Set(value) }
53 // Add implements metrics.Gauge.
54 func (g *Gauge) Add(delta float64) { g.f.Add(delta) }
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 {
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
72 func NewHistogram(name string, buckets int) *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"),
83 func (h *Histogram) With(labelValues ...string) metrics.Histogram { return h }
85 // Observe implements Histogram.
86 func (h *Histogram) Observe(value float64) {
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))