-
Notifications
You must be signed in to change notification settings - Fork 71
/
metrics.go
152 lines (126 loc) · 2.7 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
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
package ghostferry
import (
"fmt"
"sync"
"time"
log "github.com/sirupsen/logrus"
)
var (
metrics = &Metrics{
Prefix: "ghostferry",
Sink: nil,
}
)
type MetricTag struct {
Name string
Value string
}
type MetricBase struct {
Key string
Tags []MetricTag
SampleRate float64
}
type Metrics struct {
Prefix string
DefaultTags []MetricTag
Sink chan interface{}
wg sync.WaitGroup
}
func SetGlobalMetrics(prefix string, sink chan interface{}) *Metrics {
metrics = &Metrics{
Prefix: prefix,
Sink: sink,
}
return metrics
}
type CountMetric struct {
MetricBase
Value int64
}
func (m *Metrics) Count(key string, value int64, tags []MetricTag, sampleRate float64) {
m.sendMetric(CountMetric{
MetricBase: MetricBase{
Key: m.applyPrefix(key),
Tags: m.mergeWithDefaultTags(tags),
SampleRate: sampleRate,
},
Value: value,
})
}
type GaugeMetric struct {
MetricBase
Value float64
}
func (m *Metrics) Gauge(key string, value float64, tags []MetricTag, sampleRate float64) {
m.sendMetric(GaugeMetric{
MetricBase: MetricBase{
Key: m.applyPrefix(key),
Tags: m.mergeWithDefaultTags(tags),
SampleRate: sampleRate,
},
Value: value,
})
}
type TimerMetric struct {
MetricBase
Value time.Duration
}
func (m *Metrics) Timer(key string, duration time.Duration, tags []MetricTag, sampleRate float64) {
m.sendMetric(TimerMetric{
MetricBase: MetricBase{
Key: m.applyPrefix(key),
Tags: m.mergeWithDefaultTags(tags),
SampleRate: sampleRate,
},
Value: duration,
})
}
func (m *Metrics) Measure(key string, tags []MetricTag, sampleRate float64, f func()) {
start := time.Now()
f()
m.Timer(key, time.Since(start), m.mergeWithDefaultTags(tags), sampleRate)
}
func (m *Metrics) AddConsumer() {
m.wg.Add(1)
}
func (m *Metrics) DoneConsumer() {
m.wg.Done()
}
func (m *Metrics) StopAndFlush() {
close(m.Sink)
m.wg.Wait()
}
func (m *Metrics) sendMetric(metric interface{}) {
if m.Sink == nil {
return
}
select {
case m.Sink <- metric:
default:
log.WithField("tag", "metrics").
WithField("metric", metric).
Warn("Metrics sink full, dropping metric")
}
}
func (m *Metrics) applyPrefix(key string) string {
return fmt.Sprintf("%s.%s", m.Prefix, key)
}
func (m *Metrics) mergeWithDefaultTags(tags []MetricTag) []MetricTag {
mergedTags := make([]MetricTag, 0, len(tags)+len(m.DefaultTags))
for _, tag := range tags {
mergedTags = append(mergedTags, tag)
}
for _, tag := range m.DefaultTags {
exists := false
for _, existingTag := range mergedTags {
if tag.Name == existingTag.Name {
exists = true
break
}
}
if !exists {
mergedTags = append(mergedTags, tag)
}
}
return mergedTags
}