metrics: Gauge becomes float64
Peter Bourgon
8 years ago
41 | 41 |
func (c *counter) Add(delta uint64) { c.v.Add(int64(delta)) }
|
42 | 42 |
|
43 | 43 |
type gauge struct {
|
44 | |
v *expvar.Int
|
|
44 |
v *expvar.Float
|
45 | 45 |
}
|
46 | 46 |
|
47 | 47 |
// NewGauge returns a new Gauge backed by an expvar with the given name.
|
48 | 48 |
// Fields are ignored.
|
49 | 49 |
func NewGauge(name string) metrics.Gauge {
|
50 | |
return &gauge{expvar.NewInt(name)}
|
|
50 |
return &gauge{expvar.NewFloat(name)}
|
51 | 51 |
}
|
52 | 52 |
|
53 | 53 |
func (g *gauge) With(metrics.Field) metrics.Gauge { return g }
|
54 | 54 |
|
55 | |
func (g *gauge) Add(delta int64) { g.v.Add(delta) }
|
|
55 |
func (g *gauge) Add(delta float64) { g.v.Add(delta) }
|
56 | 56 |
|
57 | |
func (g *gauge) Set(value int64) { g.v.Set(value) }
|
|
57 |
func (g *gauge) Set(value float64) { g.v.Set(value) }
|
58 | 58 |
|
59 | 59 |
type histogram struct {
|
60 | 60 |
mu sync.Mutex
|
|
99 | 99 |
}
|
100 | 100 |
|
101 | 101 |
for q, gauge := range h.gauges {
|
102 | |
gauge.Set(h.hist.Current.ValueAtQuantile(float64(q)))
|
|
102 |
gauge.Set(float64(h.hist.Current.ValueAtQuantile(float64(q))))
|
103 | 103 |
}
|
104 | 104 |
}
|
105 | 105 |
|
13 | 13 |
}
|
14 | 14 |
|
15 | 15 |
// Gauge captures instantaneous measurements of something using signed, 64-bit
|
16 | |
// integers. The value does not need to be monotonic.
|
|
16 |
// floats. The value does not need to be monotonic.
|
17 | 17 |
type Gauge interface {
|
18 | 18 |
With(Field) Gauge
|
19 | |
Set(value int64)
|
20 | |
Add(delta int64)
|
|
19 |
Set(value float64)
|
|
20 |
Add(delta float64)
|
21 | 21 |
}
|
22 | 22 |
|
23 | 23 |
// Histogram tracks the distribution of a stream of values (e.g. the number of
|
43 | 43 |
return next
|
44 | 44 |
}
|
45 | 45 |
|
46 | |
func (g multiGauge) Set(value int64) {
|
|
46 |
func (g multiGauge) Set(value float64) {
|
47 | 47 |
for _, gauge := range g {
|
48 | 48 |
gauge.Set(value)
|
49 | 49 |
}
|
50 | 50 |
}
|
51 | 51 |
|
52 | |
func (g multiGauge) Add(delta int64) {
|
|
52 |
func (g multiGauge) Add(delta float64) {
|
53 | 53 |
for _, gauge := range g {
|
54 | 54 |
gauge.Add(delta)
|
55 | 55 |
}
|
102 | 102 |
}
|
103 | 103 |
}
|
104 | 104 |
|
105 | |
func (g prometheusGauge) Set(value int64) {
|
106 | |
g.GaugeVec.With(prometheus.Labels(g.Pairs)).Set(float64(value))
|
|
105 |
func (g prometheusGauge) Set(value float64) {
|
|
106 |
g.GaugeVec.With(prometheus.Labels(g.Pairs)).Set(value)
|
107 | 107 |
}
|
108 | 108 |
|
109 | |
func (g prometheusGauge) Add(delta int64) {
|
110 | |
g.GaugeVec.With(prometheus.Labels(g.Pairs)).Add(float64(delta))
|
|
109 |
func (g prometheusGauge) Add(delta float64) {
|
|
110 |
g.GaugeVec.With(prometheus.Labels(g.Pairs)).Add(delta)
|
111 | 111 |
}
|
112 | 112 |
|
113 | 113 |
type prometheusHistogram struct {
|
60 | 60 |
|
61 | 61 |
func (g statsdGauge) With(metrics.Field) metrics.Gauge { return g }
|
62 | 62 |
|
63 | |
func (g statsdGauge) Add(delta int64) {
|
|
63 |
func (g statsdGauge) Add(delta float64) {
|
64 | 64 |
// https://github.com/etsy/statsd/blob/master/docs/metric_types.md#gauges
|
65 | 65 |
sign := "+"
|
66 | 66 |
if delta < 0 {
|
67 | 67 |
sign, delta = "-", -delta
|
68 | 68 |
}
|
69 | |
g <- fmt.Sprintf("%s%d|g", sign, delta)
|
|
69 |
g <- fmt.Sprintf("%s%f|g", sign, delta)
|
70 | 70 |
}
|
71 | 71 |
|
72 | |
func (g statsdGauge) Set(value int64) {
|
73 | |
g <- fmt.Sprintf("%d|g", value)
|
|
72 |
func (g statsdGauge) Set(value float64) {
|
|
73 |
g <- fmt.Sprintf("%f|g", value)
|
74 | 74 |
}
|
75 | 75 |
|
76 | 76 |
type statsdHistogram chan string
|
41 | 41 |
runtime.Gosched() // yield to buffer write
|
42 | 42 |
ch <- time.Now() // signal flush
|
43 | 43 |
runtime.Gosched() // yield to flush
|
44 | |
if want, have := "test_statsd_gauge:+1|g\n", buf.String(); want != have {
|
|
44 |
if want, have := "test_statsd_gauge:+1.000000|g\n", buf.String(); want != have {
|
45 | 45 |
t.Errorf("want %q, have %q", want, have)
|
46 | 46 |
}
|
47 | 47 |
|
|
51 | 51 |
runtime.Gosched()
|
52 | 52 |
ch <- time.Now()
|
53 | 53 |
runtime.Gosched()
|
54 | |
if want, have := "test_statsd_gauge:-2|g\n", buf.String(); want != have {
|
|
54 |
if want, have := "test_statsd_gauge:-2.000000|g\n", buf.String(); want != have {
|
55 | 55 |
t.Errorf("want %q, have %q", want, have)
|
56 | 56 |
}
|
57 | 57 |
|
|
61 | 61 |
runtime.Gosched()
|
62 | 62 |
ch <- time.Now()
|
63 | 63 |
runtime.Gosched()
|
64 | |
if want, have := "test_statsd_gauge:3|g\n", buf.String(); want != have {
|
|
64 |
if want, have := "test_statsd_gauge:3.000000|g\n", buf.String(); want != have {
|
65 | 65 |
t.Errorf("want %q, have %q", want, have)
|
66 | 66 |
}
|
67 | 67 |
}
|