-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
gauge.go
52 lines (42 loc) · 990 Bytes
/
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
package metrics
import (
"sync/atomic"
)
// Gauge stores an int value
type Gauge struct {
value uint64
name string
desc string
}
// NewGauge initializes Gauge.
func NewGauge(name string, desc string) *Gauge {
return &Gauge{name: name, desc: desc, value: 0}
}
// Name returns gauge name
func (g *Gauge) Name() string {
return g.name
}
// Desc returns gauge description
func (g *Gauge) Desc() string {
return g.desc
}
// Set gauge value
func (g *Gauge) Set(value int) {
atomic.StoreUint64(&g.value, uint64(value))
}
// Inc increment the current value by 1
func (g *Gauge) Inc() uint64 {
return atomic.AddUint64(&g.value, 1)
}
// Dec decrement the current value by 1
func (g *Gauge) Dec() uint64 {
return atomic.AddUint64(&g.value, ^uint64(0))
}
// Set64 sets gauge value as uint64
func (g *Gauge) Set64(value uint64) {
atomic.StoreUint64(&g.value, value)
}
// Value returns the current gauge value
func (g *Gauge) Value() uint64 {
return atomic.LoadUint64(&g.value)
}