-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommon_float64.go
91 lines (80 loc) · 2.18 KB
/
common_float64.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
package metrics
import (
"sync/atomic"
)
// commonFloat64 is an implementation of common routines through all non-aggregative float64 metrics
type commonFloat64 struct {
common
modifyCounter uint64
valuePtr AtomicFloat64Interface
}
func (m *commonFloat64) init(r *Registry, parent Metric, key string, tags AnyTags) {
value := AtomicFloat64(0)
m.valuePtr = &value
m.common.init(r, parent, key, tags, func() bool { return m.wasUseless() })
}
// Add adds (+) the value of "delta" to the internal value and returns the result
func (m *commonFloat64) Add(delta float64) float64 {
if m == nil {
return 0
}
if m.valuePtr == nil {
return 0
}
r := m.valuePtr.Add(delta)
atomic.AddUint64(&m.modifyCounter, 1)
return r
}
// Set overwrites the internal value by the value of the argument "newValue"
func (m *commonFloat64) Set(newValue float64) {
if m == nil {
return
}
if m.valuePtr == nil {
return
}
m.valuePtr.Set(newValue)
atomic.AddUint64(&m.modifyCounter, 1)
}
// Get returns the current internal value
func (m *commonFloat64) Get() float64 {
if m == nil {
return 0
}
if m.valuePtr == nil {
return 0
}
return m.valuePtr.Get()
}
// GetFloat64 returns the current internal value
//
// (the same as `Get` for float64 metrics)
func (m *commonFloat64) GetFloat64() float64 {
return m.Get()
}
// getModifyCounterDiffFlush returns the count of modifications collected since the last call
// of this method
func (m *commonFloat64) getModifyCounterDiffFlush() uint64 {
if m == nil {
return 0
}
return atomic.SwapUint64(&m.modifyCounter, 0)
}
// SetValuePointer sets another pointer to be used to store the internal value of the metric
func (w *commonFloat64) SetValuePointer(newValuePtr *float64) {
if w == nil {
return
}
w.valuePtr = &AtomicFloat64Ptr{Pointer: newValuePtr}
}
// Send initiates a sending of the internal value via the sender
func (m *commonFloat64) Send(sender Sender) {
if sender == nil {
return
}
sender.SendFloat64(m.parent, string(m.storageKey), m.Get())
}
// wasUseless returns true if the metric's value didn't change since the last call of the method ("wasUseless")
func (w *commonFloat64) wasUseless() bool {
return w.getModifyCounterDiffFlush() == 0
}