...
1
2
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
14
15 type Counter struct {
16 f *expvar.Float
17 }
18
19
20
21 func NewCounter(name string) *Counter {
22 return &Counter{
23 f: expvar.NewFloat(name),
24 }
25 }
26
27
28 func (c *Counter) With(labelValues ...string) metrics.Counter { return c }
29
30
31 func (c *Counter) Add(delta float64) { c.f.Add(delta) }
32
33
34
35 type Gauge struct {
36 f *expvar.Float
37 }
38
39
40
41 func NewGauge(name string) *Gauge {
42 return &Gauge{
43 f: expvar.NewFloat(name),
44 }
45 }
46
47
48 func (g *Gauge) With(labelValues ...string) metrics.Gauge { return g }
49
50
51 func (g *Gauge) Set(value float64) { g.f.Set(value) }
52
53
54 func (g *Gauge) Add(delta float64) { g.f.Add(delta) }
55
56
57
58
59
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
70
71
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
83 func (h *Histogram) With(labelValues ...string) metrics.Histogram { return h }
84
85
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