-
Notifications
You must be signed in to change notification settings - Fork 100
/
labels.go
204 lines (182 loc) · 5.03 KB
/
labels.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package pally
import (
"errors"
"sync"
)
// DefaultLabelValue is used in place of empty label values.
const DefaultLabelValue = "default"
var (
// Match the Prometheus error message.
errInconsistentCardinality = errors.New("inconsistent label cardinality")
_digesterPool = sync.Pool{New: func() interface{} {
return &digester{make([]byte, 0, 128)}
}}
)
// A digester creates a null-delimited byte slice from a series of variable
// label values. It's an efficient way to create map keys from metric names and
// labels.
type digester struct {
bs []byte
}
// For optimal performance, be sure to free each digester.
func newDigester() *digester {
d := _digesterPool.Get().(*digester)
d.bs = d.bs[:0]
return d
}
func (d *digester) add(s string) {
if len(d.bs) > 0 {
// separate labels with a null byte
d.bs = append(d.bs, '\x00')
}
d.bs = append(d.bs, s...)
}
func (d *digester) digest() []byte {
return d.bs
}
func (d *digester) free() {
_digesterPool.Put(d)
}
// Labels describe the dimensions of a metric.
type Labels map[string]string
// IsValidName checks whether the supplied string is a valid metric and label
// name in both Prometheus and Tally.
//
// Tally and Prometheus each allow runes that the other doesn't, so Pally can
// accept only the common subset. For simplicity, we'd also like the rules for
// metric names and label names to be the same even if that's more restrictive
// than absolutely necessary.
//
// Tally allows anything matching the regexp `^[0-9A-z_\-]+$`. Prometheus
// allows the regexp `^[A-z_:][0-9A-z_:]*$` for metric names, and
// `^[A-z_][0-9A-z_]*$` for label names.
//
// The common subset is `^[A-z_][0-9A-z_]+$`.
func IsValidName(s string) bool {
if len(s) == 0 {
return false
}
switch c := s[0]; {
case 'A' <= c && c <= 'Z':
break
case 'a' <= c && c <= 'z':
break
case c == '_':
break
default:
return false
}
// Don't incur the expense of ranging over runes, since no multibyte UTF-8
// characters are legal.
for i := 1; i < len(s); i++ {
c := s[i]
switch {
case '0' <= c && c <= '9':
continue
case 'A' <= c && c <= 'Z':
continue
case 'a' <= c && c <= 'z':
continue
case c == '_':
continue
default:
return false
}
}
return true
}
// IsValidLabelValue checks whether the supplied string is a valid label value
// in both Prometheus and Tally.
//
// Tally allows label values that match the regexp `^[0-9A-z_\-.]+$`.
// Prometheus allows any valid UTF-8 string.
func IsValidLabelValue(s string) bool {
if len(s) == 0 {
return false
}
// Don't incur the expense of ranging over runes, since no multibyte UTF-8
// characters are legal.
for i := 0; i < len(s); i++ {
c := s[i]
switch {
case '0' <= c && c <= '9':
continue
case 'A' <= c && c <= 'Z':
continue
case 'a' <= c && c <= 'z':
continue
case c == '_' || c == '-' || c == '.':
continue
default:
return false
}
}
return true
}
// ScrubLabelValue replaces any invalid runes in the input string with '-'. If
// the input is already a valid label value (in both Prometheus and Tally),
// it's returned unchanged.
func ScrubLabelValue(s string) string {
if IsValidLabelValue(s) {
return s
}
if len(s) == 0 {
return DefaultLabelValue
}
d := newDigester()
for i := 0; i < len(s); i++ {
c := s[i]
switch {
case '0' <= c && c <= '9':
d.bs = append(d.bs, c)
case 'A' <= c && c <= 'Z':
d.bs = append(d.bs, c)
case 'a' <= c && c <= 'z':
d.bs = append(d.bs, c)
case c == '_' || c == '-' || c == '.':
d.bs = append(d.bs, c)
default:
d.bs = append(d.bs, '-')
}
}
scrubbed := string(d.bs)
d.free()
return scrubbed
}
func areValidLabelValues(ss []string) bool {
for _, s := range ss {
if !IsValidLabelValue(s) {
return false
}
}
return true
}
func scrubLabelValues(ss []string) []string {
if areValidLabelValues(ss) {
return ss
}
scrubbed := make([]string, len(ss))
for i, dirty := range ss {
scrubbed[i] = ScrubLabelValue(dirty)
}
return scrubbed
}