-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
149 lines (121 loc) · 3.45 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
package appoptics
import (
"fmt"
"github.com/rcrowley/go-metrics"
"log"
"regexp"
"sort"
"strings"
)
var metricNameRegex = regexp.MustCompile(`[^-.:_\w]`)
var tagNameRegex = regexp.MustCompile(`[^-.:_\w]`)
var tagValueRegex = regexp.MustCompile(`[^-.:_\\/\w\? ]`)
type TaggedMetric struct {
name string
tags map[string]string
sampleFunc func() metrics.Sample
}
func Metric(name string) *TaggedMetric {
return &TaggedMetric{name: sanitizeMetricName(name), tags: map[string]string{}}
}
func (t *TaggedMetric) Tag(name string, value interface{}) *TaggedMetric {
tagName := sanitizeTagName(name)
tagValue := sanitizeTagValue(fmt.Sprintf("%v", value))
if tagName == "" || tagValue == "" {
log.Printf("Empty tag name or value: name=%v value=%v", tagName, tagValue)
return t
}
t.tags[tagName] = tagValue
return t
}
func (t *TaggedMetric) WithSample(s func() metrics.Sample) *TaggedMetric {
t.sampleFunc = s
return t
}
func (t *TaggedMetric) String() string {
sb := strings.Builder{}
sb.WriteString(t.name)
if len(t.tags) > 0 {
sb.WriteString("#")
}
// Sort tag map for consistent ordering in encoded string
var keys []string
for key := range t.tags {
keys = append(keys, key)
}
sort.Strings(keys)
for i, key := range keys {
if i != 0 {
sb.WriteString(",")
}
sb.WriteString(key + "=" + t.tags[key])
}
return sb.String()
}
func (t *TaggedMetric) Counter() metrics.Counter {
return metrics.GetOrRegisterCounter(t.String(), metrics.DefaultRegistry)
}
func (t *TaggedMetric) Meter() metrics.Meter {
return metrics.GetOrRegisterMeter(t.String(), metrics.DefaultRegistry)
}
func (t *TaggedMetric) Timer() metrics.Timer {
return metrics.GetOrRegisterTimer(t.String(), metrics.DefaultRegistry)
}
func (t *TaggedMetric) Histogram() metrics.Histogram {
var sample func() metrics.Sample
if t.sampleFunc != nil {
sample = t.sampleFunc
} else {
sample = func() metrics.Sample {
return metrics.NewExpDecaySample(1028, 0.015)
}
}
return metrics.GetOrRegister(t.String(), func() metrics.Histogram {return metrics.NewHistogram(sample())}).(metrics.Histogram)
}
func (t *TaggedMetric) Gauge() metrics.Gauge {
return metrics.GetOrRegisterGauge(t.String(), metrics.DefaultRegistry)
}
func (t *TaggedMetric) Gauge64() metrics.GaugeFloat64 {
return metrics.GetOrRegisterGaugeFloat64(t.String(), metrics.DefaultRegistry)
}
// decodeMetricName decodes the metricName#a=foo,b=bar format and returns the TaggedMetric name
// as a string and the tags as a map
func decodeMetricName(encoded string) (string, map[string]string) {
split := strings.SplitN(encoded, "#", 2)
name := split[0]
if len(split) == 1 {
return name, nil
}
tagPart := split[1]
pairs := strings.Split(tagPart, ",")
tags := map[string]string{}
for _, pair := range pairs {
pairList := strings.SplitN(pair, "=", 2)
if len(pairList) != 2 {
log.Printf("Tag name `%v` is missing its value", pairList[0])
continue
}
tags[pairList[0]] = pairList[1]
}
return name, tags
}
func sanitizeTagName(value string) string {
if len(value) > 64 {
value = value[:64]
}
value = strings.ToLower(value)
return tagNameRegex.ReplaceAllString(value, "_")
}
func sanitizeTagValue(value string) string {
if len(value) > 255 {
value = value[:252] + "..."
}
value = strings.ToLower(value)
return tagValueRegex.ReplaceAllString(value, "_")
}
func sanitizeMetricName(value string) string {
if len(value) > 255 {
value = value[:252] + "..."
}
return metricNameRegex.ReplaceAllString(value, "_")
}