forked from Netflix/spectator-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathid.go
More file actions
179 lines (154 loc) · 3.81 KB
/
id.go
File metadata and controls
179 lines (154 loc) · 3.81 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package meter
import (
"fmt"
"sort"
"strings"
"sync"
)
// Id represents a meter's identifying information and dimensions (tags).
type Id struct {
name string
tags map[string]string
// keyOnce protects access to key, allowing it to be computed on demand
// without racing other readers.
keyOnce sync.Once
key string
// spectatordId is the Id formatted for spectatord line protocol
spectatordId string
}
var builderPool = &sync.Pool{
New: func() interface{} {
return &strings.Builder{}
},
}
// MapKey computes and saves a key within the struct to be used to uniquely
// identify this *Id in a map. This does use the information from within the
// *Id, so it assumes you've not accidentally double-declared this *Id.
func (id *Id) MapKey() string {
id.keyOnce.Do(func() {
// if the key was set directly during Id construction, then do not
// compute a value.
if id.key != "" {
return
}
buf := builderPool.Get().(*strings.Builder)
buf.Reset()
defer builderPool.Put(buf)
const errKey = "ERR"
id.key = func() string {
_, err := buf.WriteString(id.name)
if err != nil {
return errKey
}
keys := make([]string, 0, len(id.tags))
for k := range id.tags {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v := id.tags[k]
_, err = buf.WriteRune('|')
if err != nil {
return errKey
}
_, err = buf.WriteString(k)
if err != nil {
return errKey
}
_, err = buf.WriteRune('|')
if err != nil {
return errKey
}
_, err = buf.WriteString(v)
if err != nil {
return errKey
}
}
return buf.String()
}()
})
return id.key
}
// NewId generates a new *Id from the metric name, and the tags you want to
// include on your metric.
func NewId(name string, tags map[string]string) *Id {
myTags := make(map[string]string)
for k, v := range tags {
myTags[k] = v
}
spectatorId := toSpectatorId(name, tags)
return &Id{
name: name,
tags: myTags,
spectatordId: spectatorId,
}
}
// WithTag creates a deep copy of the *Id, adding the requested tag to the
// internal collection.
func (id *Id) WithTag(key string, value string) *Id {
newTags := make(map[string]string)
for k, v := range id.tags {
newTags[k] = v
}
newTags[key] = value
return NewId(id.name, newTags)
}
func (id *Id) String() string {
return fmt.Sprintf("Id{name=%s,tags=%v}", id.name, id.tags)
}
// Name exposes the internal metric name field.
func (id *Id) Name() string {
return id.name
}
// Tags directly exposes the internal tags map. This is not a copy of the map,
// so any modifications to it will be observed by the *Id.
func (id *Id) Tags() map[string]string {
return id.tags
}
// WithTags takes a map of tags, and returns a deep copy of *Id with the new
// tags appended to the original ones. Overlapping keys are overwritten. If the
// input to this method is empty, this does not return a deep copy of *Id.
func (id *Id) WithTags(tags map[string]string) *Id {
if len(tags) == 0 {
return id
}
newTags := make(map[string]string)
for k, v := range id.tags {
newTags[k] = v
}
for k, v := range tags {
newTags[k] = v
}
return NewId(id.name, newTags)
}
func toSpectatorId(name string, tags map[string]string) string {
var sb strings.Builder
writeSanitized(&sb, name)
// Append sanitized keys and values.
for k, v := range tags {
sb.WriteString(",")
writeSanitized(&sb, k)
sb.WriteString("=")
writeSanitized(&sb, v)
}
return sb.String()
}
func writeSanitized(sb *strings.Builder, input string) {
for _, r := range input {
if !isValidCharacter(r) {
sb.WriteRune('_')
} else {
sb.WriteRune(r)
}
}
}
func isValidCharacter(r rune) bool {
return (r >= 'a' && r <= 'z') ||
(r >= 'A' && r <= 'Z') ||
(r >= '0' && r <= '9') ||
r == '-' ||
r == '.' ||
r == '_' ||
r == '~' ||
r == '^'
}