-
Notifications
You must be signed in to change notification settings - Fork 2
/
metrics.go
82 lines (66 loc) · 1.6 KB
/
metrics.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
package metrics
import (
"sync"
"time"
)
const (
// TimerType is the type for timers
TimerType MetricType = "timer"
// CounterType is the type for timers
CounterType MetricType = "counter"
// GaugeType is the type for gauges
GaugeType MetricType = "gauge"
)
var zeroTime time.Time
// MetricType describes what type the metric is
type MetricType string
type RawMetric struct {
Name string `json:"name"`
Type MetricType `json:"type"`
Value int64 `json:"value"`
Dims DimMap `json:"dimensions"`
Timestamp time.Time `json:"timestamp"`
}
type metric struct {
RawMetric
dimlock sync.Mutex
env *Environment
}
func (m *metric) SetTimestamp(t time.Time) {
m.Timestamp = t
}
// AddDimension will add this dimension with locking
func (m *metric) AddDimension(key string, value interface{}) *metric {
m.dimlock.Lock()
defer m.dimlock.Unlock()
m.Dims[key] = value
return m
}
func (m *metric) send(instanceDims DimMap) error {
if m.env == nil {
return InitError{errString{"Environment not initialized"}}
}
metricToSend := &RawMetric{
Type: m.Type,
Value: m.Value,
Name: m.Name,
Timestamp: m.Timestamp,
Dims: DimMap{},
}
// global
m.env.dimlock.Lock()
addAll(metricToSend.Dims, m.env.globalDims)
m.env.dimlock.Unlock()
// metric
m.dimlock.Lock()
addAll(metricToSend.Dims, m.Dims)
m.dimlock.Unlock()
// instance
addAll(metricToSend.Dims, instanceDims)
if metricToSend.Timestamp == zeroTime {
metricToSend.Timestamp = time.Now()
}
return m.env.send(metricToSend)
}
// DimMap is a map of dimensions
type DimMap map[string]interface{}