-
Notifications
You must be signed in to change notification settings - Fork 0
/
metric.go
128 lines (112 loc) · 2.65 KB
/
metric.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
package metrics
import (
"log"
"reflect"
"strconv"
"sync"
client "github.com/influxdata/influxdb1-client"
"github.com/rcrowley/go-metrics"
)
const (
suffGauge = ".gauge"
suffTimer = ".timer"
suffCounter = ".count"
suffHistogram = ".histogram"
suffMeter = ".meter"
)
// Metric defines an interface to be implemented for custom metrics.
//
// For a metric to be used, it needs to be registered.
// If no registry was provided to the Reporter, it uses the default registry
// and can be registered with `metrics.Register(name, metric)`.
// If a custom registry was provided to the Reporter, either register it to
// that registry or use the Reporter.Register function to register it.
type Metric interface {
AddPoints(pts []client.Point) []client.Point
}
type metric interface {
regName() string
regIncr()
AddPoints(pts []client.Point) []client.Point
}
func newMetric(name string, options ...Option) *baseMetric {
m := &baseMetric{
name: name,
reporter: defaultReporter,
measurement: "default",
suffix: ".metric",
regMutex: &sync.Mutex{},
}
for _, option := range options {
option(m)
}
if m.reporter != nil {
m.tags = composeTags(m.reporter.Tags(), m.tags)
}
return m
}
type baseMetric struct {
name string
incr int
metric interface{}
reporter Reporter
measurement string
tags map[string]string
suffix string
regMutex *sync.Mutex
}
func (s *baseMetric) regName() string {
suffix := s.suffix
if s.incr != 0 {
suffix = strconv.Itoa(s.incr) + s.suffix
}
return s.measurement + "/" + s.name + suffix
}
func (s *baseMetric) regIncr() {
s.incr++
}
func (s *baseMetric) register(m metric) Metric {
s.regMutex.Lock()
defer s.regMutex.Unlock()
return s.reg(m)
}
func (s *baseMetric) reg(m metric) Metric {
regName := m.regName()
if s.reporter == nil {
log.Println("WARNING: no (default) metrics reporter set")
if err := metrics.Register(regName, m); err != nil {
log.Printf("default registry: metric could not be registered: %v", err)
}
return m
}
if mtrx, ok := s.reporter.Get(regName); ok {
if reflect.TypeOf(m) == reflect.TypeOf(mtrx) {
return mtrx
}
m.regIncr()
return s.reg(m)
}
if err := s.reporter.Register(regName, m); err != nil {
// this should never happen because of the lock above
log.Printf("metric could not be registered: %v", err)
}
return m
}
const (
count = "count"
max = "max"
mean = "mean"
min = "min"
p50 = "p50"
p75 = "p75"
p95 = "p95"
p99 = "p99"
p999 = "p999"
p9999 = "p9999"
stddev = "stddev"
variance = "variance"
m1 = "m1"
m5 = "m5"
m15 = "m15"
meanrate = "meanrate"
)