/
gauge.go
170 lines (136 loc) · 3.89 KB
/
gauge.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package metric
import (
"sync/atomic"
"math"
)
// A client maintained gauge which is only sampled regulary without information loss
// wrt. the absolute value.
// Can be used as a client side maintained counter too.
// The default gauge type using a uint64
type GaugeUint64 struct {
name string
val uint64
}
// A gauge using an int64 - meaning it can be decremented to negaive values
type GaugeInt64 struct {
name string
val int64
}
// A go-metric compatible float64 gauge which stores its value as a uint64
// to implement Flush() fast
type GaugeFloat64 struct {
name string
val uint64
}
/*==================================================================*/
// NewGauge is alias for NewGaugeUint64
func NewGauge(name string, opts ...MOption) *GaugeUint64 {
return default_client.NewGauge(name, opts...)
}
// NewGauge is alias for NewGaugeUint64
func (c *Client) NewGauge(name string, opts ...MOption) *GaugeUint64 {
return c.NewGaugeUint64(name, opts...)
}
/*==================================================================*/
// NewGaugeUint64 returns a standard gauge.
func (c *Client) NewGaugeUint64(name string, opts ...MOption) *GaugeUint64 {
g := &GaugeUint64{name: name}
c.register(g, opts...)
return g
}
// Flush to implement Meter interface
func (g *GaugeUint64) Flush(f FlusherSink) {
val := atomic.LoadUint64(&g.val)
n := Numeric64{Type:Uint64, value: val}
f.EmitNumeric64(g.name, MeterGauge, n)
}
// Name to implement Meter interface
func (g *GaugeUint64) Name() string {
return g.name
}
// Mtype to implement Meter interface
func (g *GaugeUint64) Mtype() int {
return MeterGauge
}
// Update the gauge value
func (g *GaugeUint64) Update(val uint64) {
atomic.StoreUint64(&g.val, val)
}
// Get the gauge value
func (g *GaugeUint64) Value() uint64 {
return atomic.LoadUint64(&g.val)
}
// Increment the gauge value
func (g *GaugeUint64) Inc(i uint64) {
atomic.AddUint64(&g.val, i)
}
// Decrement the gauge value
func (g *GaugeUint64) Dec(i int64) {
atomic.AddUint64(&g.val, ^uint64(i-1))
}
/*==================================================================*/
// An Int64 Gauge. Can be used as a go-metric client side gauge or counter
func (c *Client) NewGaugeInt64(name string, opts ...MOption) *GaugeInt64 {
g := &GaugeInt64{name: name}
c.register(g, opts...)
return g
}
func (g *GaugeInt64) Flush(f FlusherSink) {
val := atomic.LoadInt64(&g.val)
n := Numeric64{Type:Int64, value: uint64(val)}
f.EmitNumeric64(g.name, MeterGauge, n)
}
func (g *GaugeInt64) Name() string {
return g.name
}
func (g *GaugeInt64) Mtype() int {
return MeterGauge
}
func (g *GaugeInt64) Update(val int64) {
atomic.StoreInt64(&g.val, val)
}
func (g *GaugeInt64) Value() int64 {
return atomic.LoadInt64(&g.val)
}
// Clear sets the counter to zero.
func (g *GaugeInt64) Clear() {
atomic.StoreInt64(&g.val, 0)
}
// Count returns the current count.
func (g *GaugeInt64) Count() int64 {
return g.Value()
}
// Dec decrements the counter by the given amount.
func (g *GaugeInt64) Dec(i int64) {
atomic.AddInt64(&g.val, -i)
}
// Inc increments the counter by the given amount.
func (g *GaugeInt64) Inc(i int64) {
atomic.AddInt64(&g.val, i)
}
/*==================================================================*/
// An Float64 Gauge.
func (c *Client) NewGaugeFloat64(name string, opts ...MOption) *GaugeFloat64 {
g := &GaugeFloat64{name: name}
c.register(g, opts...)
return g
}
func (g *GaugeFloat64) Name() string {
return g.name
}
func (g *GaugeFloat64) Mtype() int {
return MeterGauge
}
// Update updates the gauge's value.
func (g *GaugeFloat64) Update(v float64) {
atomic.StoreUint64(&g.val, math.Float64bits(v))
}
// Value returns the gauge's current value.
func (g *GaugeFloat64) Value() float64 {
return math.Float64frombits(atomic.LoadUint64(&g.val))
}
func (g *GaugeFloat64) Flush(f FlusherSink) {
val := atomic.LoadUint64(&g.val)
n := Numeric64{Type:Float64, value: val}
f.EmitNumeric64(g.name, MeterGauge, n)
}