-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgauge.go
48 lines (41 loc) · 916 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
package metrics
import (
"sync"
"time"
)
// Gauge keeps a running measure of the value at that moment
type Gauge interface {
Increment(DimMap) error
Decrement(DimMap) error
Set(int64, DimMap) error
SetTimestamp(time.Time)
}
type gauge struct {
metric
valueLock sync.Mutex
}
func (e *Environment) NewGauge(name string, metricDims DimMap) Gauge {
m := e.newMetric(name, GaugeType, metricDims)
return &gauge{
metric: *m,
valueLock: sync.Mutex{},
}
}
func (m *gauge) Increment(instanceDims DimMap) error {
m.valueLock.Lock()
defer m.valueLock.Unlock()
m.Value++
return m.send(instanceDims)
}
func (m *gauge) Decrement(instanceDims DimMap) error {
m.valueLock.Lock()
defer m.valueLock.Unlock()
m.Value--
return m.send(instanceDims)
}
func (m *gauge) Set(val int64, instanceDims DimMap) error {
m.valueLock.Lock()
defer m.valueLock.Unlock()
m.Value = val
return m.send(instanceDims)
}